https://blog.csdn.net/lxc521wjh/article/details/53212729
通过logging.basicConfig函数对日志的输出格式及方式做相关配置。logging.basicConfig(**kwargs) 该函数必须在main函数线程除外的子线程启动之前调用,否则可能会造成日志重复记录
import logging
fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s: %(message)s' logging.basicConfig(level=logging.DEBUG, format=fmt, filename='D:\Python\logs.txt', filemode='w', datefmt='%a, %d %b %Y %H:%M:%S' ) logging.debug('this is a debug level message') logging.info("this is a info level message") logging.warning("this is a warning level message") logging.error("this is a error level message") logging.critical("this is a critical level message") |
运行:
'D:\Python\logs.txt',
将日志同时输出到文件和屏幕
import logging
fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s: %(message)s' logging.basicConfig(level=logging.DEBUG, format=fmt, filename='D:\Python\logs.txt', filemode='w+', datefmt='%a, %d %b %Y %H:%M:%S' )
console = logging.StreamHandler() console.setLevel(logging.DEBUG) #设置控制台日志输出的级别。如果设置为logging.INFO,就不会输出DEBUG日志信息
formatter = logging.Formatter(fmt) console.setFormatter(formatter) logging.getLogger().addHandler(console)
logging.debug('this is a debug level message') logging.info("this is a info level message") logging.warning("this is a warning level message") logging.error("this is a error level message") logging.critical("this is a critical level message") |
运行:
D:\Python\logs.txt:
RotatingFileHander模块,用于自动切换日志写入文件,保证单个日志文件不会太大
import logging from logging.handlers import RotatingFileHandler
fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s: %(message)s' logging.basicConfig(level=logging.DEBUG, format=fmt, filename='D:\Python\logs.txt', filemode='w+', datefmt='%a, %d %b %Y %H:%M:%S' )
console = logging.StreamHandler() console.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt) console.setFormatter(formatter) logging.getLogger().addHandler(console)
rt_file_handler = RotatingFileHandler('d:\Python\logs1.txt',maxBytes = 100,backupCount=5) rt_file_handler.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s') rt_file_handler.setFormatter(formatter)
logger = logging.getLogger() logger.addHandler(rt_file_handler)
logging.debug('this is a debug level message') logging.info("this is a info level message") logging.warning("this is a warning level message") logging.error("this is a error level message") logging.critical("this is a critical level message") |
运行:
通过logging.config模块配置日志
注意:配置文件必须包含[loggers], [handlers],[formatters], 且每个节点下的logger, handler, formatter都必须有对应的节点,且格式正确:[loggers_logger], [handlers_handler], [handlers_formatter]
D:/log.conf文件如下: [loggers] keys=root,eg01,eg02
#注:[logger_root]为root logger专用 [logger_root] level=NOTSET handlers=handler01, handler02
[logger_eg01] handlers=handler01 qualname=eg01 propagate=0 #propagete=0,表示输出日志,但消息不传递;propagate=1是输出日志,同时消息往更高级别的地方传递。若这里的参数progagate=1,那么将会看到重复的消息记录
[logger_eg02] handlers=handler02 qualname=eg02 #qualname指定logger的名称 propagate=0
[handlers] keys=handler01,handler02
[handler_handler01] class=StreamHandler #指定handler的类型 level=WARNING formatter=form01 args=(sys.stdout,) #根据class的不同而不同,即handler类型的初始化参数。详情可参考官方logging.conf模块。
[handler_handler02] class=FileHandler level=ERROR formatter=form02 args=('d:/logs.txt', 'a')
[formatters] keys=form01,form02
[formatter_form01] format=%(name)s: %(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s datefmt=%a, %d %b %Y %H:%M:%S
[formatter_form02] format=%(name)-12s: %(levelname)-8s %(message)s datefmt=
|
说明:当[logger_logname]和[handler_handlername]中同时指定了level值时,使用[handler_handlername]中设置的level。