Python Logging Module
Main stuff
logger, handler, formatter.
How it works
ref:
How Python logging module works | Shut Up and Ship
http://zqpythonic.qiniucdn.com/data/20161124153540/index.html
Simple example
import logging # create logger lgr = logging.getLogger('myapp') lgr.setLevel(logging.DEBUG) # add a file handler fh = logging.FileHandler('myapp.log') fh.setLevel(logging.WARNING) # create a formatter and set the formatter for the handler. frmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(frmt) # add the Handler to the logger lgr.addHandler(fh) # You can now start issuing logging statements in your code lgr.debug('debug message') # This won't print to myapp.log lgr.info('info message') # Neither will this. lgr.warning('Checkout this warning.') # This will show up in the log file. lgr.error('An error goes here.') # and so will this. lgr.critical('Something critical happened.') # and this one too.
Levels
CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET
ref:
How Python logging module works | Shut Up and Ship
http://zqpythonic.qiniucdn.com/data/20161124153540/index.html
More to understand the work flow
formatter
Currently, the useful attributes in a LogRecord are described by:
attributes | discription |
---|---|
%(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) |
%(lineno)d | Source line number where the logging call was issued (if available) |
%(funcName)s | Function name |
%(created)f | Time when the LogRecord was created (time.time() return value) |
%(asctime)s | Textual time when the LogRecord was created |
%(msecs)d | Millisecond portion of the creation time |
%(relativeCreated)d | Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded (typically at application startup time) |
%(thread)d | Thread ID (if available) |
%(threadName)s | Thread name (if available) |
%(process)d | Process ID (if available) |
%(message)s | The result of record.getMessage(), computed just as the record is emitted |
ref:
cpython: 5c4ca109af1c Lib/logging/__init__.py
https://hg.python.org/cpython/file/5c4ca109af1c/Lib/logging/__init__.py#l399
logging.conf
python 的日志logging模块学习 - dkcndk - 博客园
https://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html
16.7. logging.config — Logging configuration — Python 3.6.3 documentation
https://docs.python.org/3/library/logging.config.html
reference
Python Standard Logging in Sugar - OLPC
http://wiki.laptop.org/go/Python_Standard_Logging_in_Sugar
How Python logging module works | Shut Up and Ship
http://zqpythonic.qiniucdn.com/data/20161124153540/index.html
Python 学习入门(14)—— logging - CSDN博客
http://blog.csdn.net/ithomer/article/details/16985379
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具