Python-python 的logging模块的日志级别设置不生效
import logging
from logging.handlers import RotatingFileHandler
log_file = "log/log"
datetime_format = "%Y-%m-%d %H:%M:%S"
per_log_file_max_size = 10240
backup_count = 3
log_format = "%(asctime)s %(filename)s[line:%(lineno)d][%(levelname)s] %(message)s"
rotate_file_handler = RotatingFileHandler(log_file, maxBytes=per_log_file_max_size,
backupCount=backup_count)
rotate_file_handler.setLevel(logging.NOTSET)
formatter = logging.Formatter(log_format)
rotate_file_handler.setFormatter(formatter)
logging.getLogger('').addHandler(rotate_file_handler)
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
logging.error('error')
输出:
2017-02-10 18:00:26,408 test.py[line:32][WARNING] This is warning message
2017-02-10 18:00:26,408 test.py[line:33][ERROR] error
2017-02-10 18:00:26,408 test.py[line:34][CRITICAL] critical
设置的日志级别是NOSET,实际上是WARNING
求解释:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
import logging
from logging.handlers import RotatingFileHandler
log_file = "log/log"
datetime_format = "%Y-%m-%d %H:%M:%S"
per_log_file_max_size = 10240
backup_count = 3
log_format = "%(asctime)s %(filename)s[line:%(lineno)d][%(levelname)s] %(message)s"
rotate_file_handler = RotatingFileHandler(log_file, maxBytes=per_log_file_max_size,
backupCount=backup_count)
#rotate_file_handler.setLevel(logging.NOTSET)
formatter = logging.Formatter(log_format)
rotate_file_handler.setFormatter(formatter)
logging.getLogger('').addHandler(rotate_file_handler)
#在这里设置日志级别
logging.getLogger('').setLevel(logging.NOTSET)
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
logging.error('error')