Analysis of logging module in Python (code example)
The content of this article is about the analysis of the logging module in Python (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Logging level
#debug: Priority 10, records debugging details, only enabled during debugging
info: Priority 20, log ordinary messages, report errors and warnings and wait.
warning: Priority 30, records relevant warning information.
error: priority level 40, record error information, program crash
critical: priority level 50, record error information
2. The main structure of the logging module
Looking at the source code of logging, we can see that there are four main classes to implement functions:
Loggers: Provides interfaces for direct use by the program, such as related configuration settings
Handlers: Transmits the logs generated by Loggers to the specified location, and sets the logs Save location;
Filters: Filter the output log;
Formatters: Control the output format of the log
Formatters
The Formatters object defines the log output format and has a variety of optional parameters.
Parameter | Meaning | |
---|---|---|
Logger’s name | ||
Log level in numerical form | ||
Log level in text form | ||
The full pathname of the module that calls the log output function, may not have | ||
The file name of the module that calls the log output function | ||
The module name for calling the log output function | ||
The function name for calling the log output function | ||
The line of code where the statement that calls the log output function is located | ||
The current time, expressed in unix notation Floating point representation of time | ||
When outputting log information, the number of milliseconds since the Logger was created | ||
The current time in string form, the default format is '2018-11-22 16:49:45,896', followed by the comma is milliseconds | ||
Thread ID, may not be available | ||
Thread name, may not be available | ||
Process ID, there may be no information output by | ||
Name | Detailed location | Description |
---|---|---|
logging.handlers.RotatingHandler | Log rollback method, supports the maximum number of log files and log file rollback | |
logging.handlers.TimeRotatingHandler | Log rollback method, Roll back log files within a certain time range | |
logging.handlers.SocketHandler | Remotely output logs to TCP/IP sockets | |
logging.handlers.DatagramHandler | Remote output logs to UDP sockets | |
logging.handlers.SMTPHandler | Remotely output logs to email address | |
logging.handlers.SysLogHandler | Log Output to syslog | |
logging.handlers.NTEventLogHandler | Remote output log to the event log of Windows NT/2000/xp | |
logging.handlers.MemoryHandler | The log is output to the specified buffer in memory | |
logging.handlers.HTTPHandler | Remote output to HTTP server through "GET" or "POST" |
Logger manages all logging methods.
from logging import error, debug, warning, info, fatal, critical, getLogger #返回一个Logger实例 #以'root'为名字的日志对象在Logger对象中只有一个实例 logger=getLogger('root') print('获取根日志对象',logger.root) print('获取manager',logger.manager) print('获取根日志对象的名字',logger.name) print('获取根日志对象记录水平',logger.level) print('获取根日志对象过滤器列表',logger.filters) print('获取根日志对象处理器列表',logger.handlers) print('获取根日志对象',logger.disabled) #设置日志记录水平 logger.setLevel('info') #输出日志信息,格式化输出 logger.info('this is %s','info',exc_info=1) #记录warning信息 logger.warning('') #记录error信息 logger.error('') #等价于logger.error('',exc_info=1) logger.exception('') #记录debug信息 logger.debug('') #记录critical信息 logger.critical('') #直接指定级别 logger.log('info','') #添加处理器 logger.addHandler() #移除处理器 logger.removeHandler() #判是否有处理器 logger.hasHandlers()
Example:
import logging import sys def my_get_logger(appname): #获取logger实例,如果参数为空则返回root logger logger=logging.getLogger(appname) #创建日志输出格式 formatter=logging.Formatter('%(asctime)s %(levelname)s: %(message)s') #指定输出的文件路径 file_handler=logging.FileHandler('test.log') # 设置文件处理器,加载处理器格式 file_handler.setFormatter(formatter) #控制台日志 console_handler=logging.StreamHandler(sys.stdout) console_handler.formatter=formatter #为logger添加的日志处理器 logger.addHandler(file_handler) logger.addHandler(console_handler) #指定日志的最低输出级别,默认为warn级别 logger.setLevel(logging.INFO) return logger if __name__ == '__main__': logger=my_get_logger('test') logger.debug('this is debug info') logger.info('this is information') logger.warning('this is warning message') logger.error('this is error message') logger.fatal('this is fatal message,it is same ad logger.critical') logger.critical('this is critical message')
Result:
2018-11-22 16:31:34,023 INFO: this is information 2018-11-22 16:31:34,023 WARNING: this is warning message 2018-11-22 16:31:34,023 ERROR: this is error message 2018-11-22 16:31:34,024 CRITICAL: this is fatal message,it is same ad logger.critical 2018-11-22 16:31:34,024 CRITICAL: this is critical message
- Record logs by calling logger.debug and other methods;
- First determine whether the log level of this record is Greater than the set level, if not, pass directly and no longer execute;
- Use the log information as a parameter to create a LogRecord logging object
- Filter the LogRecord object through the logger filter. If it is filtered, pass
- The log record object is filtered by the filter of the Handler processor
- Judgement Is the log level of this record greater than the level set by the Handler processor? If not, pass directly and no longer execute;
- Finally call the emit method of the processor to process the log record;
- Complete configuration through code, mainly through the getLogger method, but it is not easy to modify
- Achieved through the basicConfig method, which is fast but not hierarchical enough
- Through logging.config.fileConfig(filepath), file configuration
- Configure through the dictionary of dictConfig, which is a new configuration method introduced in py3.2 version
#logging.cong
[loggers]
#定义日志的对象名称是什么,注意必须定义root,否则报错
keys=root,main
[handlers]
#定义处理器的名字是什么,可以有多个,用逗号隔开
keys=consoleHandler
[formatters]
#定义输出格式对象的名字,可以有多个,用逗号隔开
keys=simpleFormatter
[logger_root]
#配置root对象的日志记录级别和使用的处理器
level=INFO
handlers=consoleHandler
[logger_main]
#配置main对象的日志记录级别和使用的处理器,qualname值得就是日志对象的名字
level=INFO
handlers=consoleHandler
qualname=main
#logger对象把日志传递给所有相关的handler的时候,会逐级向上寻找这个logger和它所有的父logger的全部handler,
#propagate=1表示会继续向上搜寻;
#propagate=0表示停止搜寻,这个参数涉及重复打印的坑。
propagate=0
[handler_consoleHandler]
#配置处理器consoleHandler
class=StreamHandler
level=WARNING
formatter=simpleFormatter
args=(sys,)
[formatter_simpleFormatter]
#配置输出格式过滤器simpleFormatter
format=%(asctime)-%(name)s-%(levelname)s-%(message)s
Dictionary form configuration is more powerful and flexible. Through the dictConfig function, we can convert configuration files in other formats into dictionaries, such as json, YAML, etc.
Example:
import yaml from logging.config import dictConfig import os filename=os.path.dirname(os.path.abspath(__file__)) with open(filename+'/logging.yaml','r') as f: log=yaml.load(f.read()) dictConfig(log)
#logging.yaml #注意:yaml格式严格,:后面一定要带空格 version: 1 formatters: simple: format: '%(asctime)s-%(name)s-%(levelname)s-%(message)s' handlers: console: class: logging.StreamHandler level: DEBUG formatter: simple stream: ext://sys.stdout console_err: class: logging.StreamHandler level: DEBUG formatter: simple stream: ext://sys.stderr loggers: simpleExample: level: DEBUG handlers: [console] propagate: no root: level: DEBUG handlers: [console_err]]
The logging.config.listen(port) function allows English programs to listen to new loggers on a socket Configuration information to achieve the purpose of changing the configuration at runtime without restarting the application. import logging.config
import logging
logging.config.fileConfig("logging.conf")
logger=logging.getLogger('test.listen')
#监听端口号9999
t=logging.config.listen(9999)
t.setDaemon(True)
t.start()
The above is the detailed content of Analysis of logging module in Python (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

Getting started with Python: Hourglass Graphic Drawing and Input Verification This article will solve the variable definition problem encountered by a Python novice in the hourglass Graphic Drawing Program. Code...

Choice of Python Cross-platform desktop application development library Many Python developers want to develop desktop applications that can run on both Windows and Linux systems...

Many developers rely on PyPI (PythonPackageIndex)...

Data Conversion and Statistics: Efficient Processing of Large Data Sets This article will introduce in detail how to convert a data list containing product information to another containing...

How to handle high resolution images in Python to find white areas? Processing a high-resolution picture of 9000x7000 pixels, how to accurately find two of the picture...

When using Python to connect to an FTP server, you may encounter encoding problems when obtaining files in the specified directory and downloading them, especially text on the FTP server...
