实用小公举

[Python] logging包学习记录

2019-02-26  本文已影响0人  happyxhz

参考资料:

python logging模块

logging基本设置:

import logging
logging.basicConfig(level = logging.INFO,
    format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    filename = './test.log',
    filemode = 'w')

参数说明:
level:log显示等级

logging中可以选择很多消息级别,如debug、info、warning、error以及critical。

format:输出格式

format.png

filename:log记录存放文件
filemode:'w'写入

写入log信息:

logging.info(msg)

修改logger:

# set logger
import  logging
import getpass
user = getpass.getuser()
logger = logging.getLogger(user)
logger.setLevel(logging.INFO)

# add handle 1. file handle
logFormat = logging.Formatter('%(asctime)-12s %(levelname)-8s %(name)-12s %(message)-40s')

logHand = logging.FileHandler('./log')
logHand.setFormatter(logFormat)
logHand.setLevel(logging.Error)
logger.addHandler(logHand)

# 2. streamhandler
logHandSt= logging.StreamHandler()
logger.addHandler(logHandSt)
上一篇 下一篇

猜你喜欢

热点阅读