Home  >  Article  >  Backend Development  >  python logging module logging

python logging module logging

高洛峰
高洛峰Original
2017-02-22 09:23:141110browse

Module level function

logging.getLogger([name]): Returns a logger object. If no name is specified, the root logger will be returned

logging.debug(), logging.info( ), logging.warning(), logging.error(), logging.critical(): Set the log level of the root logger

logging.basicConfig(): Use the default Formatter to create a StreamHandler for the log system, set Basic configuration and added to the root logger

Logger

logging.getLogger([name])

Returns a logger instance. If name is not specified, returns the root logger.

Each program must obtain a Logger before outputting information. Logger usually corresponds to the module name of the program. For example, the graphical interface module of a chat tool can obtain its Logger like this:

LOG=logging.getLogger("chat.gui")

And the core module It can be like this:

LOG=logging.getLogger("chat.kernel")

Logger.setLevel(logging.WARNING): Specify the lowest log level, lower than The level of WARNING will be ignored

Logger.addFilter(filt), Logger.removeFilter(filt): Add or remove the specified filter

Logger.addHandler(hdlr), Logger.removeHandler(hdlr ): Add or delete the specified handler

Handlers

The handler object is responsible for sending relevant information to the specified destination. It can be a file, screen, network, socket, etc.

Handler.setLevel(lel): Specifies the level of information to be processed, information below the lel level will be ignored

Handler.setFormatter() : Select an output format for this handler

Handler.addFilter(filt), Handler.removeFilter(filt): Add or delete a filter object

The log is printed to Screen

import logging

logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')

 

返回:
WARNING:root:This is warning message

打印到屏幕

By default, logging prints logs to the screen, and the log level is WARNING;
The log level size relationship is: CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET, Of course, you can also define the log level yourself.

Format log output

Parameters of logging.basicConfig function:
filename: Specify the log file name
filemode: The same meaning as the file function, specify The opening mode of the log file, 'w' or 'a'
format: Specify the output format and content. format can output a lot of useful information, as shown in the above example:

import logging

logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt='%a, %d %b %Y %H:%M:%S',
                filename='myapp.log',
                filemode='w')
    
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')

 

./myapp.log文件中内容为:
Sun, 24 May 2009 21:48:54 demo2.py[line:11] DEBUG This is debug message
Sun, 24 May 2009 21:48:54 demo2.py[line:12] INFO This is info message
Sun, 24 May 2009 21:48:54 demo2.py[line:13] WARNING This is warning message

修改输出格式

Log format variable

%(levelno)s: 打印日志级别的数值
 %(levelname)s: 打印日志级别名称
 %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
 %(filename)s: 打印当前执行程序名
 %(funcName)s: 打印日志的当前函数
 %(lineno)d: 打印日志的当前行号
 %(asctime)s: 打印日志的时间
 %(thread)d: 打印线程ID
 %(threadName)s: 打印线程名称
 %(process)d: 打印进程ID
 %(message)s: 打印日志信息
datefmt: 指定时间格式,同time.strftime()
level: 设置日志级别,默认为logging.WARNING
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略

日志格式

logging method

logging.StreamHandler: 日志输出到流,可以是sys.stderr、sys.stdout或者文件
logging.FileHandler: 日志输出到文件

日志回滚方式,实际使用时用RotatingFileHandler和TimedRotatingFileHandler
logging.handlers.BaseRotatingHandler
logging.handlers.RotatingFileHandler
logging.handlers.TimedRotatingFileHandler

logging.handlers.SocketHandler: 远程输出日志到TCP/IP sockets
logging.handlers.DatagramHandler:  远程输出日志到UDP sockets
logging.handlers.SMTPHandler:  远程输出日志到邮件地址
logging.handlers.SysLogHandler: 日志输出到syslog
logging.handlers.NTEventLogHandler: 远程输出日志到Windows NT/2000/XP的事件日志
logging.handlers.MemoryHandler: 日志输出到内存中的制定buffer
logging.handlers.HTTPHandler: 通过"GET"或"POST"远程输出到HTTP服务器

logging方法

Since StreamHandler and FileHandler are commonly used log processing methods, they are directly included in the logging module, while other methods are included in the logging.handlers module

In Log module defined in the program

import logging
# create logger
def logger(log_type):
logger = logging.getLogger(log_type) # 创建Logger对象,类型是'TEST-LOG'
logger.setLevel(logging.WARNING) # 设置最低的日志级别,此级别覆盖ch and fh的级别
# 创建输出到控制台处理程序,并设置级别为DEBUG
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG) # 控制输出到屏幕的级别
# 创建输出到文件的处理程序,并设置级别
fh = logging.FileHandler("access.log")
fh.setLevel(logging.WARNING)
# 创建日志格式
formatter_Stream = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
formatter_File = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch and fh
ch.setFormatter(formatter_Stream)
fh.setFormatter(formatter_File)
# add ch and fh to logger
logger.addHandler(ch)
logger.addHandler(fh)
return logger
eee = logger('EEE')
rrr = logger('RRR')
rrr.debug('debug message')
rrr.info('info message')
rrr.warning('warn message')
eee.error('error message')
eee.critical('critical message')

代码

Screen display content

RRR - INFO - info message

RRR - WARNING - warn message

EEE - ERROR - error message

EEE - CRITICAL - critical message

Contents in the file

2017-02-21 21:35:05,700 - RRR - WARNING - warn message

2017-02-21 21:35:05,700 - EEE - ERROR - error message

2017-02-21 21:35:05,700 - EEE - CRITICAL - critical message


Filter

The format of the parameters when calling logging.getLogger() is similar to "A.B.C". This format is used to configure the filter. After adding a filter, the log will be output after being processed by the filter.

The filter "AAA.BBB" only allows loggers whose names start with "AAA.BBB" to output information.

Multiple filters can be added, as long as one filter rejects, the log will not be output

import logging
def logger(log_type):
logger = logging.getLogger(log_type) # 创建Logger对象,类型是'TEST-LOG'
logger.setLevel(logging.DEBUG) # 此级别覆盖ch and fh的级别
# 创建输出到控制台处理程序,并设置级别为DEBUG
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG) # 控制输出到屏幕的级别
 
# 设置过滤器
filter = logging.Filter('AAA.BBB.CCC')
ch.addFilter(filter)
formatter_Stream = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter_Stream)
logger.addHandler(ch) # ch加入logger
return logger
eee = logger('AAA.BBB.CCC')
rrr = logger('AAA.BBB.DDD')
rrr.error('debug message')
rrr.error('info message')
eee.error('critical message')
eee.error('critical message')

代码

Cutting log

Cut by size

import logging
from logging import handlers
# create logger
def logger(log_type):
logger = logging.getLogger(log_type)
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler("access.log")
fh.setLevel(logging.WARNING)
# 按时间切割日志文件
fh = handlers.TimedRotatingFileHandler(filename='access.log', when='s', interval=10 )
formatter_File = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter_File)
logger.addHandler(fh)
return logger
rrr = logger('AAA.BBB.DDD')
rrr.error('debug message')
rrr.error('info message')
rrr.error('warn message')

代码

interval: time interval

when: Time unit S seconds M minutes H hours D days W every week (interval==0 represents Monday) midnight every morning

About the root logger and the parent-child relationship of the logger

I mentioned the root logger many times before. In fact, there is a parent-child relationship between logger instances. The root logger is at the top level. logger, which is the ancestor of all loggers. As shown below: root logger is the default logger. If you do not create a logger instance and directly call the functions such as logging.debug(), logging.info(), logging.warning(), logging.error(), and logging.critical(), then use The logger is the root logger, which can be created automatically and is also a single instance.

python日志模块 logging


How to get the root logger Get the root logger instance through logging.getLogger() or logging.getLogger("").
The default level of the levelroot logger is logging.WARNING
How to express the parent-child relationship. The naming method of the logger name can express the parent-child relationship between loggers. For example: parent_logger = logging.getLogger('foo')child_logger = logging.getLogger('foo.bar')
What is effective level? Logger has a concept called effective level. If a logger does not set level explicitly, it uses the parent's level. If the father does not set the level explicitly, the level of the father's father will be used, and so on... Finally, when we reach the root logger, the level must have been set. The default is logging.WARNING. After child loggers get the message, it not only distributes the message to its handler for processing, but also passes it to all ancestor loggers for processing.

Please pay attention to more python log module logging related articles PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn