python logging system
官方教程:https://docs.python.org/2/library/logging.html
1. 用法1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import logging import logging.handlers LOG_FILE = 'tst.log' handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes = 1024 * 1024 , backupCount = 5 ) # 实例化handler fmt = '%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s' formatter = logging.Formatter(fmt) # 实例化formatter handler.setFormatter(formatter) # 为handler添加formatter logger = logging.getLogger( 'tst' ) # 获取名为tst的logger logger.addHandler(handler) # 为logger添加handler logger.setLevel(logging.DEBUG) logger.info( 'first info message' ) logger.debug( 'first debug message' ) |
formatter
采用的是%(<dict key>)s的形式,就是字典的关键字替换。提供的关键字包括:
Format | Description |
---|---|
%(name)s | Name of the logger (logging channel). |
%(levelno)s | Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL). |
%(levelname)s | Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'). |
%(pathname)s | Full pathname of the source file where the logging call was issued (if available). |
%(filename)s | Filename portion of pathname. |
%(module)s | Module (name portion of filename). |
%(funcName)s | Name of function containing the logging call. |
%(lineno)d | Source line number where the logging call was issued (if available). |
%(created)f | Time when the LogRecord was created (as returned by time.time()). |
%(relativeCreated)d | Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded. |
%(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). |
%(msecs)d | Millisecond portion of the time when the LogRecord was created. |
%(thread)d | Thread ID (if available). |
%(threadName)s | Thread name (if available). |
%(process)d | Process ID (if available). |
%(message)s | The logged message, computed as msg % args. |
level
The numeric values of logging levels are given in the following table. These are primarily of interest if you want to define your own levels, and need them to have specific values relative to the predefined levels. If you define a level with the same numeric value, it overwrites the predefined value; the predefined name is lost.
Level | Numeric value |
---|---|
CRITICAL | 50 |
ERROR | 40 |
WARNING | 30 |
INFO | 20 |
DEBUG | 10 |
NOTSET | 0 |
如果指定level 为INFO, 则小于它的所有message都不会打印出来。
2. 用法2
采用配置文件的方式去配置logging system, 这种方法简单方便,可以在多模块间调用而不需要传入logger对象。
现在我准备一个配置文件logging.conf如下
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | [loggers] keys = root,uploader,msgChanger # [handlers] keys = consoleHandler,uploaderHandler,msgChangerHandler [formatters] keys = fmt [logger_root] level = INFO handlers = consoleHandler qualname = root [logger_uploader] level = INFO qualname = uploader handlers = uploaderHandler [logger_msgChanger] level = INFO qualname = msgChanger handlers = msgChangerHandler [handler_consoleHandler] class = StreamHandler level = INFO formatter = fmt args = (sys.stdout,) [handler_uploaderHandler] class = logging.handlers.RotatingFileHandler level = INFO formatter = fmt args = ( 'log/uploader.log' , 'w' , 1024 * 1024 * 1024 , 5 ,) [handler_msgChangerHandler] class = logging.handlers.RotatingFileHandler level = INFO formatter = fmt args = ( 'log/msg_changer.log' , 'w' , 1024 * 1024 * 1024 , 5 ,) [formatter_fmt] format = % (asctime)s - % (name)s - % (levelname)s - % (message)s datefmt = |
called in code:
1 2 3 | '''init log module''' logging.config.fileConfig( 'config/logging.conf' ) # parse config file as above msgChanger_logger = logging.getLogger( 'msgChanger.main' ) # get a logger instance, msgChanger must be included <br> msgChanger_logger.info('test log system in main.') |
可以在任何module下写入上述代码调用logger, 只需要传入一个名字就好了,名字必须在config文件里配置了的,否则实例失败。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥