python 日志模块日志模块 日志等级设置失效的解决方案日志等级设置失效的解决方案
主要介绍了python 日志模块 日志等级设置失效的问题及解决方案,本文通过实例代码给大家介绍的非常详细,
对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
代码问题:控制台和日志的文件的等级设置要放在
logger = logging.getLogger('myloger')
实例化之后才会生效 不然就被logger默认的替代了
问题:当前日志模块的等级设置 失效
现象:
console_level = "INFO"
handler_level = "INFO"
handler_file = logging.FileHandler(filename)
handler_file.setFormatter(formatter)
handler_file.setLevel(handler_level)
handler_console = logging.StreamHandler()
handler_console.setFormatter(formatter)
handler_console.setLevel(console_level)
logger = logging.getLogger('myloger')
# logger.setLevel("INFO")
from log import logger
logger.debug("debug---------------")
logger.info("info---------------")
logger.warning("warning---------------")
logger.error("error---------------")
logger.critical("critical---------------")
>>>>> 空 没有打印
解决办法 使用这个级别设置
logger.setLevel("INFO")
console_level = "INFO"
handler_level = "INFO"
handler_file = logging.FileHandler(filename)
handler_file.setFormatter(formatter)
handler_file.setLevel(handler_level)
handler_console = logging.StreamHandler()
handler_console.setFormatter(formatter)
handler_console.setLevel(console_level)
logger = logging.getLogger('myloger')
logger.setLevel("INFO")
# 给logger添加handler
logger.addHandler(handler_file)
from log import logger
fee=3
data={}
data['trade_amount']=100
# logger.info("ddddddddddddddddddddddddddddd")
# logger.info('fee='+ str(fee) + "trade_amount"+ str(data['trade_amount']))
#logger.info('fee=' + "trade_amount"+ data['trade_amount'],)
logger.info('fee ={0}+"trade_amount"'.format(data['trade_amount']))
>>>>
import log start
2020-05-25 15:27:26,286 - log_level.py:17 - fee =100+"trade_amount"
import log end
修改代码
日志的等级打印正常
print "import log start"
import logging
import datetime
date = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d')
filename = date + '.log'
fmt = '%(asctime)s - %(filename)s:%(lineno)s - %(message)s'
formatter = logging.Formatter(fmt)
logger = logging.getLogger('myloger')
logger.setLevel(logging.DEBUG)
console_level = "INFO"
handler_level = "DEBUG"
# logging.basicConfig(level=logging.DEBUG)
评论0
最新资源