search
HomeBackend DevelopmentPython TutorialAnalysis 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

  1. #debug: Priority 10, records debugging details, only enabled during debugging

  2. info: Priority 20, log ordinary messages, report errors and warnings and wait.

  3. warning: Priority 30, records relevant warning information.

  4. error: priority level 40, record error information, program crash

  5. critical: priority level 50, record error information

If not set, the default is iwarning

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:

  1. Loggers: Provides interfaces for direct use by the program, such as related configuration settings

  2. Handlers: Transmits the logs generated by Loggers to the specified location, and sets the logs Save location;

  3. Filters: Filter the output log;

  4. Formatters: Control the output format of the log

Formatters

The Formatters object defines the log output format and has a variety of optional parameters.

##%(name)sLogger’s name%(levellno)sLog level in numerical form%(levelname)s Log level in text form%(pathname)sThe full pathname of the module that calls the log output function, may not have %(filename)sThe file name of the module that calls the log output function%(module)sThe module name for calling the log output function##%(funcName)s%(lineno)d%(created)f%(relativeCreated)d%(asctime)s%(thread)d%(threadName)s%(process)d%(message)s

Example:

import logging

#fmt:定义输出的日志信息的格式
#datefmt:定义时间信息的格式,默认为:%Y-%m-%d %H:%M:%S
#style:定义格式化输出的占位符,默认是%(name)格式,可选{}或$格式
formatter=logging.Formatter(fmt='%(asctime)s    %(levelname)s:  %(message)s'
                            ,datefmt='%Y-%m-%d %H:%M:%S',style='%')

Handlers log processor

The log processor is used to process the specific flow direction of the log, whether it is output to a file or standard output, etc. It passes Set the Formatter to control the output format and add filters to filter the logs.

There are two commonly used processors

  1. StreamHandler: used to print logs to the console

  2. FileHandler: used to print logs to log files

Other processors

Parameter Meaning
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
##RotatingHandlerlogging.handlers.RotatingHandler Log rollback method, supports the maximum number of log files and log file rollbackTimeRotatingHandlerlogging.handlers.TimeRotatingHandlerLog rollback method, Roll back log files within a certain time rangeSocketHandlerlogging.handlers.SocketHandlerRemotely output logs to TCP/IP socketsDatagramHandlerlogging.handlers.DatagramHandlerRemote output logs to UDP socketsSMTPHandlerlogging.handlers.SMTPHandlerRemotely output logs to email addressSysLogHandlerlogging.handlers.SysLogHandlerLog Output to syslogNTEventLogHandlerlogging.handlers.NTEventLogHandlerRemote output log to the event log of Windows NT/2000/xpMemoryHandlerlogging.handlers.MemoryHandlerThe log is output to the specified buffer in memoryHTTPHandlerlogging.handlers.HTTPHandlerRemote output to HTTP server through "GET" or "POST"##
from logging import Handler

#所有日志处理器的父类
handler=Handler()

print('处理日志的等级:',handler.level)
print('处理日志的名字:',handler.name)
print('处理器的日志过滤器::',handler.filters)
print('日志的格式::',handler.filters)

#一些常用方法:
handler.get_name()
handler.set_name('')
handler.createLock()#创建线程锁
handler.acquire()#获取线程锁
handler.release()#释放线程锁
handler.setLevel('info') #设置日志处理器的记录级别
handler.setFormatter(fmt='')#设置日志的输出格式
handler.addFilter('')#往处理器中添加过滤器
handler.removeFilter('')#往处理器中移除过滤器
handler.emit('')#日志记录的处理逻辑,由子类实现
Name Detailed location Description
Logger log object

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()

3. Basic use of logger

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

4. Logger log Logical calling process of recording

    Record logs by calling logger.debug and other methods;
  1. First determine whether the log level of this record is Greater than the set level, if not, pass directly and no longer execute;
  2. Use the log information as a parameter to create a LogRecord logging object
  3. Filter the LogRecord object through the logger filter. If it is filtered, pass
  4. The log record object is filtered by the filter of the Handler processor
  5. 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;
  6. Finally call the emit method of the processor to process the log record;
5. Configure logger

    Complete configuration through code, mainly through the getLogger method, but it is not easy to modify
  1. Achieved through the basicConfig method, which is fast but not hierarchical enough
  2. Through logging.config.fileConfig(filepath), file configuration
  3. Configure through the dictionary of dictConfig, which is a new configuration method introduced in py3.2 version
Use file configuration

#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
Note: You can see that both logger and Handler can set the log level, and the log output takes the highest level.

Use dictionary form configuration

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]]

6. Listening logger configuration changes

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!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.