[Python] logging包学习记录
2019-02-26 本文已影响0人
happyxhz
参考资料:
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:输出格式
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)