Python Logging Module

本文详细介绍了Python的日志模块logging的使用方法,包括如何创建logger、handler和formatter,以及设置不同的日志级别。通过一个简单的例子展示了不同级别的日志记录在文件中的行为,并解释了LogRecord的各种属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

attributesdiscription
%(name)sName of the logger (logging channel)
%(levelno)sNumeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL)
%(levelname)sText logging level for the message (“DEBUG”, “INFO”, “WARNING”, “ERROR”, “CRITICAL”)
%(pathname)sFull pathname of the source file where the logging call was issued (if available)
%(filename)sFilename portion of pathname
%(module)sModule (name portion of filename)
%(lineno)dSource line number where the logging call was issued (if available)
%(funcName)sFunction name
%(created)fTime when the LogRecord was created (time.time() return value)
%(asctime)sTextual time when the LogRecord was created
%(msecs)dMillisecond portion of the creation time
%(relativeCreated)dTime in milliseconds when the LogRecord was created, relative to the time the logging module was loaded (typically at application startup time)
%(thread)dThread ID (if available)
%(threadName)sThread name (if available)
%(process)dProcess ID (if available)
%(message)sThe 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 - 优快云博客
http://blog.youkuaiyun.com/ithomer/article/details/16985379

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值