找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 5398|回复: 0

Python:日志模块logging的应用

[复制链接]
发表于 2012-2-11 19:44:31 | 显示全部楼层 |阅读模式
/************************************************************************/  通常,在商用软件中均会有完整的日志机制,之前使用C语言实现过一个《简单的分级别写日志程序》,具有以下功能和不足:
  1.   * 摘    要:此文件实现了普通WINDOWS程序中的日志功能
  2.   *           主要有以下特点:
  3.   *           1. 根据日期创建日志文件目录,每天的日志分别存放在不同的日志目录中;
  4.   *           2. 日志内容分三种类型,根据不同需要,写不同的日志类型的日志文件,
  5.   *              方便通过日志定位、分析问题;
  6.   *           3. 函数经过比较好的封装,便于复用;
  7.   *           待改进点:
  8.   *           1. 为了方便,日志内容打印时使用了time函数,其精确度较低;
  9.   *           2. 可将这些函数封装为一个日志类,或者动态库,使其更通用;
  10.   *           3. 没有考虑跨平台情景,目前只使用于WINDOWS下
  11.   *           4. 日志文件内容还可进一步改进,比如打印出当前文件名与行号,使用日志功能
  12.   *              更加实用;
  13.   *
  14.   * 当前版本:1.0
  15.   * 作    者:duanyongxing  
  16.   * 完成日期:2009年10月11日                                                                  
  17. /************************************************************************/
  18.   
  19. 在Python中,上面以实现的和已经实现的,均可以使用logging模块迅速搞定,且仅仅只需要一个配置文件,两行代码,实现过程如下(仅以输出的磁盘文件为例,命令输出只需要修改配置文件即可,具体可查API手册):
  20. 1. 定义配置文件logging.conf:
  21. [loggers]
  22. keys=root,applog
  23. [handlers]
  24. keys=rotateFileHandler
  25. [formatters]
  26. keys=applog_format
  27. [formatter_applog_format]
  28. format=[%(asctime)s - %(name)s]%(levelname)s:  %(message)s - %(filename)s:%(lineno)d
  29. [logger_root]
  30. level=NOTSET
  31. handlers=rotateFileHandler
  32. [logger_applog]
  33. level=NOTSET
  34. handlers=rotateFileHandler
  35. qualname=simple_example
  36. [handler_rotateFileHandler]
  37. class=handlers.RotatingFileHandler
  38. level=NOTSET
  39. formatter=applog_format
  40. args=('log_1.log', 'a', 10000, 9)
  41. 注意前三个[ ]中的keys,这个在后面各[
  42. ]中定义定义,section的取名格式如looger_自定义名称,
  43. handler_自定义名称,我偷懒直接使用了标准名称,其他一样,最后一个要注意的就是format,即日志文件中内容的格式,具体见后面附一。level参数是日志级别,可扩展,如果使用python自己的,有以下四个级别:
  44. Level Numeric value
  45. CRITICAL       50
  46. ERROR          40
  47. WARNING        30
  48. INFO           20
  49. DEBUG          10
  50. NOTSET          0
  51. 例如配置文件中level定义为WARN,则INFO, DEBUG,NOTSET三个级别的日志点则不会输出,很方便的做到了日志级别控制。
  52. args定义了日志方件名,写方式,最大大小,保存最多个数等属性。
  53. 2.编码,测试
  54. #!/usr/bin/env python
  55. # -*- coding: utf-8 -*-
  56. import logging
  57. import logging.config
  58. #日志初始化
  59. LOG_FILENAME = 'logging.conf'
  60. logging.config.fileConfig(LOG_FILENAME)
  61. logger = logging.getLogger("simple_log_example")
  62. #测试代码
  63. logger.debug("debug message")
  64. logger.info("info message")
  65. logger.warn("warn message")
  66. logger.error("error message")
  67. logger.critical("critical message")
  68. 运行后,查看日志文件,内容如下:
  69. [2012-02-11 14:47:05,483 - simple_log_example]DEBUG:  debug message - test_log.py:48
  70. [2012-02-11 14:47:05,483 - simple_log_example]INFO:  info message - test_log.py:49
  71. [2012-02-11 14:47:05,483 - simple_log_example]WARNING:  warn message - test_log.py:50
  72. [2012-02-11 14:47:05,483 - simple_log_example]ERROR:  error message - test_log.py:51
  73. [2012-02-11 14:47:05,483 - simple_log_example]CRITICAL:  critical message - test_log.py:52
  74. 如将日志级别设置为WARN,再次运行,查看日志:
  75. [2012-02-11 14:54:20,046 - simple_log_example]WARNING:  warn message - test_log.py:50
  76. [2012-02-11 14:54:20,092 - simple_log_example]ERROR:  error message - test_log.py:51
  77. [2012-02-11 14:54:20,092 - simple_log_example]CRITICAL:  critical message - test_log.py:52
  78. 附一:format参数格式说明:
  79. Format Description
  80. %(name)s Name of the logger (logging channel).
  81. %(levelno)s Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL).
  82. %(levelname)s Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL').
  83. %(pathname)s Full pathname of the source file where the logging call was issued (if available).
  84. %(filename)s Filename portion of pathname.
  85. %(module)s Module (name portion of filename).
  86. %(funcName)s Name of function containing the logging call.
  87. %(lineno)d Source line number where the logging call was issued (if available).
  88. %(created)f Time when the LogRecord was created (as returned by time.time()).
  89. %(relativeCreated)d Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
  90. %(asctime)s Human-readable time when the LogRecord was created. By default this is of the form “2003-07-08 16:49:45,896” (the numbers after the comma are millisecond portion of the time).
  91. %(msecs)d Millisecond portion of the time when the LogRecord was created.
  92. %(thread)d Thread ID (if available).
  93. %(threadName)s Thread name (if available).
  94. %(process)d Process ID (if available).
  95. %(message)s The logged message, computed as msg % args.
复制代码

作者:dyx1024 发表于2012-2-11 14:57:38 原文链接
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-2 17:44 , Processed in 0.019135 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表