python logging模块配置

简介:

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

级别 数值
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
相关文章
|
15天前
|
人工智能 自然语言处理 Shell
[oeasy]python070_如何导入模块_导入模块的作用_hello_dunder_双下划线
本文介绍了如何在Python中导入模块及其作用,重点讲解了`__hello__`模块的导入与使用。通过`import`命令可以将外部模块引入当前环境,增强代码功能。例如,导入`__hello__`模块后可输出“Hello world!”。此外,还演示了如何使用`help()`和`dir()`函数查询模块信息,并展示了导入多个模块的方法。最后,通过一个实例,介绍了如何利用`jieba`、`WordCloud`和`matplotlib`模块生成词云图。总结来说,模块是封装好的功能部件,能够简化编程任务并提高效率。未来将探讨如何创建自定义模块。
35 8
|
13天前
|
缓存 Shell 开发工具
[oeasy]python071_我可以自己做一个模块吗_自定义模块_引入模块_import_diy
本文介绍了 Python 中模块的导入与自定义模块的创建。首先,我们回忆了模块的概念,即封装好功能的部件,并通过导入 `__hello__` 模块实现了输出 "hello world!" 的功能。接着,尝试创建并编辑自己的模块 `my_file.py`,引入 `time` 模块以获取当前时间,并在其中添加自定义输出。
21 4
|
3月前
|
Python
Python Internet 模块
Python Internet 模块。
140 74
|
2月前
|
安全 Linux 网络安全
利用Python脚本自动备份网络设备配置
通过本文的介绍,我们了解了如何利用Python脚本自动备份网络设备配置。该脚本使用 `paramiko`库通过SSH连接到设备,获取并保存配置文件。通过定时任务调度,可以实现定期自动备份,确保网络设备配置的安全和可用。希望这些内容能够帮助你在实际工作中实现网络设备的自动化备份。
80 14
|
4月前
|
算法 数据安全/隐私保护 开发者
马特赛特旋转算法:Python的随机模块背后的力量
马特赛特旋转算法是Python `random`模块的核心,由松本真和西村拓士于1997年提出。它基于线性反馈移位寄存器,具有超长周期和高维均匀性,适用于模拟、密码学等领域。Python中通过设置种子值初始化状态数组,经状态更新和输出提取生成随机数,代码简单高效。
153 63
|
4月前
|
数据可视化 Python
如何在Python中解决模块和包的依赖冲突?
解决模块和包的依赖冲突需要综合运用多种方法,并且需要团队成员的共同努力和协作。通过合理的管理和解决冲突,可以提高项目的稳定性和可扩展性
|
4月前
|
测试技术 Python
手动解决Python模块和包依赖冲突的具体步骤是什么?
需要注意的是,手动解决依赖冲突可能需要一定的时间和经验,并且需要谨慎操作,避免引入新的问题。在实际操作中,还可以结合使用其他方法,如虚拟环境等,来更好地管理和解决依赖冲突😉。
|
4月前
|
持续交付 Python
如何在Python中自动解决模块和包的依赖冲突?
完全自动解决所有依赖冲突可能并不总是可行,特别是在复杂的项目中。有时候仍然需要人工干预和判断。自动解决的方法主要是提供辅助和便捷,但不能完全替代人工的分析和决策😉。
|
2月前
|
Python
[oeasy]python057_如何删除print函数_dunder_builtins_系统内建模块
本文介绍了如何删除Python中的`print`函数,并探讨了系统内建模块`__builtins__`的作用。主要内容包括: 1. **回忆上次内容**:上次提到使用下划线避免命名冲突。 2. **双下划线变量**:解释了双下划线(如`__name__`、`__doc__`、`__builtins__`)是系统定义的标识符,具有特殊含义。
41 3
|
2月前
|
人工智能 编译器 Python
python已经安装有其他用途如何用hbuilerx配置环境-附带实例demo-python开发入门之hbuilderx编译器如何配置python环境—hbuilderx配置python环境优雅草央千澈
python已经安装有其他用途如何用hbuilerx配置环境-附带实例demo-python开发入门之hbuilderx编译器如何配置python环境—hbuilderx配置python环境优雅草央千澈
60 0
python已经安装有其他用途如何用hbuilerx配置环境-附带实例demo-python开发入门之hbuilderx编译器如何配置python环境—hbuilderx配置python环境优雅草央千澈

热门文章

最新文章