以前写代码的时候,所有信息包括调试信息全部输出在屏幕上,有的时候会看起来乱糟糟的,这时就需要logging模块来记录信息或生成日志文件。
1. 简介
logging是一个用来记录日志信息的模块,它可以输出信息到stdout或者自定的日志文件中。
2. 基本
输出信息:
1 2 3 4 5 logging.debug("A debug message" ) logging.info("A info message" ) logging.warn("A warning message" ) logging.error("A error message" ) logging.critical("A critical message" )
日志级别:
logging默认的日志级别为WARNING,在当前级别下,只有warning及以上的日志可以被记录。logging的级别可通过logging.basicConfig
或logging.setLevel
来修改。级别大小关系为CRITICAL(50) > ERROR(40) > WARNING(30) > INFO(20) > DEBUG(10) > NOTSET(0), 需要注意的是,一旦记录信息,logging的日志级别就不可再更改。
1 2 3 4 5 >>> logging.basicConfig(level=logging.INFO)>>> logging.info("Info" )INFO:root:Info >>> logging.basicConfig(level=logging.DEBUG)>>> logging.debug("Debug" )
3. 日志格式设置及日志文件操作
1 2 3 4 5 6 7 8 9 10 11 12 13 import loggingimport timelogging.basicConfig(format ="%(asctime)s %(levelname)s %(message)s" , datefmt="%Y %b %d %H:%M:%S" , filename="./log.log" , filemode="w" , level=logging.INFO) while True : for i in range (6 ): logging.log(i*10 , "a log" ) time.sleep(1 )
log.log输出(可以用tail -f命令实时查看):
1 2 3 4 5 6 7 8 2015 Oct 21 14:41:36 INFO a log 2015 Oct 21 14:41:37 WARNING a log 2015 Oct 21 14:41:38 ERROR a log 2015 Oct 21 14:41:41 CRITICAL a log 2015 Oct 21 14:41:42 INFO a log 2015 Oct 21 14:41:43 WARNING a log 2015 Oct 21 14:41:44 ERROR a log 2015 Oct 21 14:41:45 CRITICAL a log
4. 将日志输出到多个流中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import loggingimport sysconsole = logging.StreamHandler(stream=sys.stdout) console.setLevel(logging.INFO) formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s' ) console.setFormatter(formatter) logging.getLogger().addHandler(console) files = logging.FileHandler("log2.log" , mode="a" , encoding="utf-8" ) files.setLevel(logging.WARNING) formatter = logging.Formatter("%(levelname)s %(message)s" ) files.setFormatter(formatter) logging.getLogger().addHandler(files) for i in range (1 , 6 ): logging.log(i*10 , logging.getLevelName(i*10 ).lower())
5. 设置多个logger以记录不同信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import loggingimport syslog1 = logging.Logger("0.0" ) console = logging.StreamHandler(sys.stdout) console.setLevel(logging.INFO) formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s' ) console.setFormatter(formatter) log1.addHandler(console) log2 = logging.Logger("-.-" ) files = logging.FileHandler("log2.log" , mode="a" , encoding="utf-8" ) files.setLevel(logging.WARNING) formatter = logging.Formatter("%(name)s %(levelname)s %(message)s" ) files.setFormatter(formatter) log2.addHandler(files) for i in range (1 , 6 ): log1.log(i*10 , logging.getLevelName(i*10 ).lower()) log2.log(i*10 , logging.getLevelName(i*10 ).lower()) logging.critical("AHHH! I'm the root logger but you forget me!" ) logging.getLogger("root" ).info("Of course not!" )
5. 参考文档
Logging HOWTO
python 的日志logging模块学习
6. 后记
又填完一个坑,最近要填的坑好多…