配置文件名 log4p.conf
[loggers]
keys=root
[handlers]
keys=TimeHandler_info,TimeHandler_debug,TimeHandler_error
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=TimeHandler_info,TimeHandler_debug,TimeHandler_error
[handler_TimeHandler_info]
class=logging.handlers.TimedRotatingFileHandler
level=INFO
formatter=simpleFormatter
args=('/tmp/workengine/logs/workengine-info.log','H',6, 'w')
[handler_TimeHandler_debug]
class=logging.handlers.TimedRotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=('/tmp/workengine/logs/workengine-debug.log','H',6 ,'w')
[handler_TimeHandler_error]
class=logging.handlers.TimedRotatingFileHandler
level=ERROR
formatter=simpleFormatter
args=('/tmp/workengine/logs/workengine-error.log','D',1, 'w')
[formatter_simpleFormatter]
format=%(asctime)s %(levelname)-5s %(lineno)d %(message)s
加载配置:
注意:在项目进程入口的地方加入这一句就可以了,如果系统有命令行的方式调用py脚本,要在被调脚本开始的地方重新加入本行
logging.config.fileConfig("log4p.conf")
使用:
logger=logging.getLogger()
logger.info("info")
logger.error("error")
logger.debug("debug")