logging.ini日志配置文件内容示例:
1 [loggers] 2 keys=root,demo 3 4 [handlers] 5 keys=consoleHandler,timedRotatingFileHandler 6 7 [formatters] 8 keys=simpleFmt 9 10 [logger_root] 11 level=DEBUG 12 handlers=consoleHandler 13 14 [logger_demo] 15 level=DEBUG 16 handlers=timedRotatingFileHandler 17 qualname=demo 18 propagate=0 19 20 [handler_consoleHandler] 21 class=StreamHandler 22 level=DEBUG 23 formatter=simpleFmt 24 args=(sys.stdout,) 25 26 [handler_timedRotatingFileHandler] 27 class=handlers.TimedRotatingFileHandler 28 level=DEBUG 29 formatter=simpleFmt 30 args=("demo.log", "d", 1, 0) 31 32 [formatter_simpleFmt] 33 format=%(asctime)s - %(name)s - [%(filename)s:%(lineno)s]- %(levelname)s - %(message)s 34 datefmt=
使用示例:
1 import logging 2 import logging.config 3 4 logging.config.fileconfig("logging.ini") 5 logger = logging.getLogger("demo") 6 7 if __name__ == "__main__": 8 logger.debug('This is debug message') 9 logger.info('This is info message') 10 logger.error('This is error message')
demo.log日志文件示例:
1 2018-10-16 21:40:27,104 - demo - [log_record.py:15]- DEBUG - This is debug message 2 2018-10-16 21:40:27,105 - demo - [log_record.py:16]- INFO - This is info message 3 2018-10-16 21:40:27,105 - demo - [log_record.py:17]- ERROR - This is error message