python的logging日志模块学习 打印日志到屏幕 打印日志到文件 basicConfig format datefmt StreamHandler RotatingFileHandler
- 2015-12-13 15:14:00
- admin
- 原创 4679
一、logging日志模块功能
提供打印日志的接口和相关配置,主要模块有Handlers、Filters、Formatters :
Handlers:处理日志IO,如控制台日志、文件日志、或者网络日志
Filters :根据日志的级别,决定是否打印日志到某个IO
Formatters:格式化日志到指定格式
二、将日志输出到屏幕示例
import logging
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
输出:WARNING:root:This is warning message
默认情况下:logging将日志输出到屏幕,且日志级别为WARNING,等于或大于该级别的日志会输出。
日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG,当然也可以自己定义日志级别。
三、logging.basicConfig函数配置日志级别、输出格式、输出目的(如下输出到文件)
basicConfig函数参数:
filename 日志文件名
filemode 默认为a,即增加日志
format 日志格式
datefmt 日志时间格式
level 默认为WARNING
stream 如果同时出现filename和stream参数,则优先使用filename
示例:
# -*- coding: utf-8 -*-
import logging
FORMAT = '%(asctime)s.%(msecs)d|%(levelname)s| - %(message)s' #日志属性
DATEFMT = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(level=logging.DEBUG,
format=FORMAT, datefmt=DATEFMT, filename='myapp.log', filemode='w')
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
输出:
2015-12-13 20:19:01.280|DEBUG| - This is debug message
2015-12-13 20:19:01.280|INFO| - This is info message
2015-12-13 20:19:01.280|WARNING| - This is warning message
四、logging之同时输出日志到屏幕和文件
注意:basicConfig的日志级别设置对所有handler是有效的,handler处理的日志都经过basic过滤过。
realHandleLvl=MAX(basicConfig.LogLvl, handle.LogLvl)
# -*- coding: utf-8 -*-
import logging
FORMAT = '%(asctime)s.%(msecs)d|%(levelname)s| - %(message)s'
DATEFMT = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(level=logging.DEBUG,
format=FORMAT, datefmt=DATEFMT, filename='myapp.log', filemode='w')
#定义StreamHandler将日志发送到一个流,默认发送到屏幕。
consoleLog = logging.StreamHandler()
consoleLog.setLevel(logging.DEBUG)
consoleLog.setFormatter(logging.Formatter(FORMAT, DATEFMT))
logging.getLogger('').addHandler(consoleLog)
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
五、logging之日志文件旋转
# -*- coding: utf-8 -*-
import logging
from logging.handlers import RotatingFileHandler
FORMAT = '%(asctime)s.%(msecs)d|%(levelname)s| - %(message)s'
DATEFMT = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(level=logging.DEBUG, format=FORMAT, datefmt=DATEFMT)
#定义RotatingFileHandler,日志文件大小超过10M时进行旋转,最大5个备份日志文件。
rotateHandler = RotatingFileHandler('myapp.log', maxBytes=10*1024*1024, backupCount=5)
rotateHandler.setLevel(logging.DEBUG)
rotateHandler.setFormatter(logging.Formatter(FORMAT, DATEFMT))
logging.getLogger('').addHandler(rotateHandler)
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
六、通过logging.config模块配置日志
配置文件app.conf:
[loggers]
keys=root,stderr
[logger_root]
level=DEBUG
handlers=hand01,hand02
[logger_stderr]
level=DEBUG
handlers=hand01
qualname=stderr
propagate=0
[handlers]
keys=hand01,hand02
[handler_hand01]
class=StreamHandler
level=DEBUG
formatter=form01
args=(sys.stderr,)
[handler_hand02]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=form01
args=('myapp.log', 'a', 10*1024*1024, 5)
[formatters]
keys=form01
[formatter_form01]
format=%(asctime)s.%(msecs)d|%(levelname)s| - %(message)s
datefmt=%Y-%m-%d %H:%M:%S
示例代码:
# -*- coding: utf-8 -*-
import logging
import logging.config
logging.config.fileConfig("app.conf")
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
logging.getLogger('stderr').info('to stderr')
七、logging线程安全
1、使用线程锁串行访问logging模块共享数据。
2、每个handler使用lock串行访问IO。