且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python logging模块配置

更新时间:2022-10-05 20:15:09

日志级别对应值如下表所示。了解这些有助于你定义自己的日志级别的相对关系。如果你定义的级别与已有的数值相同,则原有的级别会被覆盖。

级别 数值
CRITICAL 50
ERROR 40
WARNING 30
INFO 20
DEBUG 10
NOTSET 0

 

生成log的几个方法, 这几个方法安重要程度依次排开, logger的设置可以影响一部分呢不重要的内容记录到日志当中。

  • debug(log_message, [*args[, **kwargs]])
  • info(log_message, [*args[, **kwargs]])
  • warning(log_message, [*args[, **kwargs]])
  • error(log_message, [*args[, **kwargs]])
  • critical(log_message, [*args[, **kwargs]])
  • exception(message[, *args])
  • log(log_level, log_message, [*args[, **kwargs]])

 

Handler 是处理器, 可以把log记录到文件系统中, 输出到终端设备, 或者网络sockt或者邮件等。
  • StreamHandler
  • FileHandler
  • RotatingFileHandler
  • TimedRotatingFileHandler
  • SocketHandler
  • DatagramHandler
  • SysLogHandler
  • NTEventLogHandler
  • SMTPHandler
  • MemoryHandler
  • HTTPHandler

LoggerFormatter, log的格式, 使用这个可以更容易的根据错误检错。


  1. %(name)s            Name of the logger (logging channel) 
  2. %(levelno)s         Numeric logging level for the message (DEBUG, INFO, 
  3.                     WARNING, ERROR, CRITICAL) 
  4. %(levelname)s       Text logging level for the message ("DEBUG""INFO"
  5.                     "WARNING""ERROR""CRITICAL"
  6. %(pathname)s        Full pathname of the source file where the logging 
  7.                     call was issued (if available) 
  8. %(filename)s        Filename portion of pathname 
  9. %(module)s          Module (name portion of filename) 
  10. %(lineno)d          Source line number where the logging call was issued 
  11.                     (if available) 
  12. %(created)f         Time when the LogRecord was created (time.time() 
  13.                     return value) 
  14. %(asctime)s         Textual time when the LogRecord was created 
  15. %(msecs)d           Millisecond portion of the creation time 
  16. %(relativeCreated)d Time in milliseconds when the LogRecord was created, 
  17.                     relative to the time the logging module was loaded 
  18.                     (typically at application startup time) 
  19. %(thread)d          Thread ID (if available) 
  20. %(process)d         Process ID (if available) 
  21. %(message)s         The result of record.getMessage(), computed just as 
  22.                     the record is emitted 

下面是一个简单的demo, 其中使用了两个handler分别将log记录到标准输出和文件中,因为handler是可以加多个的额, 所以你可以任意添加自己的handler。


  1. #!/usr/bin/env python 
  2.  
  3. import logging 
  4.  
  5. #create logger 
  6. logger = logging.getLogger("simple_example"
  7. logger.setLevel(logging.DEBUG) 
  8. #create console handler and set level to error 
  9. ch = logging.StreamHandler() 
  10. ch.setLevel(logging.ERROR) 
  11. #create file handler and set level to debug 
  12. fh = logging.FileHandler("spam.log"
  13. fh.setLevel(logging.DEBUG) 
  14. #create formatter 
  15. formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - 
  16.    %(message)s") 
  17. #add formatter to ch and fh 
  18. ch.setFormatter(formatter) 
  19. fh.setFormatter(formatter) 
  20. #add ch and fh to logger 
  21. logger.addHandler(ch) 
  22. logger.addHandler(fh) 
  23.  
  24. #"application" code 
  25. logger.debug("debug message"
  26. logger.info("info message"
  27. logger.warn("warn message"
  28. logger.error("error message"
  29. logger.critical("critical message"
本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1045506如需转载请自行联系原作者

lihuipeng