Python Logging Module Usage Tutorials

Source: Internet
Author: User
Tags remove filter string format

Simple to use
#!/usr/local/bin/python# -*- coding:utf-8 -*-import logginglogging.debug(‘debug message‘)logging.info(‘info message‘)logging.warn(‘warn message‘)logging.error(‘error message‘)logging.critical(‘critical message‘) 

Output:

WARNING:root:warn message
ERROR:root:error message
CRITICAL:root:critical message

By default, the logging module prints the log to the screen (stdout) with a log level of warning (that is, only log information that is higher than warning at the log level is output), as shown in the log format:

Default_logging_format.png

Here's the problem.

  1. What are the log level levels and settings?
  2. How do I set the output of a log? For example, output to a log file?
Simple Configuration Log level
level When to use
DEBUG Detailed information that is typically interesting when debugging a problem.
INFO Prove that things work as expected.
WARNING Indicates that something has happened, or that a problem will occur in the near future (such as ' Disk full '). The software is still working properly.
ERROR Because of the more serious problem, the software has been unable to perform some functions.
CRITICAL A critical error indicates that the software cannot continue running.
Simple configuration
#!/usr/local/bin/python# -*- coding:utf-8 -*-import logging# 通过下面的方式进行简单配置输出方式与日志级别logging.basicConfig(filename=‘logger.log‘, level=logging.INFO)logging.debug(‘debug message‘)logging.info(‘info message‘)logging.warn(‘warn message‘)logging.error(‘error message‘)logging.critical(‘critical message‘)

Output:
The standard output (screen) does not display any information and finds that Logger.log is generated under the current working directory as follows:

INFO:root:info message
WARNING:root:warn message
ERROR:root:error message
CRITICAL:root:critical message

Because the log level is info set through level=logging.info , all log information is output.

Here comes the question again.

  1. Can you configure that information with the above configuration method?

Before you solve the above problems, you need to understand a few of the more important concepts,Logger,Handler,Formatter,Filter

A few important concepts
    • The Logger recorder exposes an interface that the application code can use directly.
    • Handler processor that sends (logger-generated) log records to the appropriate destination.
    • Filter filters provide better granularity control, which determines which log records are output.
    • The Formatter formatter, which indicates the layout of the log records in the final output.
Logger Recorder

Logger is a tree-level structure, you must create a logger instance before using interface debug,info,warn,error,critical, that is, create a logger, and if you do not create it explicitly, create a root logger by default. and apply the default log level (WARN), the processor handler (Streamhandler, the log information printout on the standard output), and the formatter formatter (the default format is the format of the output in the first simple-to-use program).

To create a method:logger = logging.getLogger(logger_name)

After you create the logger instance, you can use the following methods to set the log level to increase the processor handler.

    • Logger.setlevel (logging. Error) # Set the log level to error, that is, only logs with a log level greater than or equal to error will be output
    • Logger.addhandler (handler_name) # Add a processor to the logger instance
    • Logger.removehandler (handler_name) # Remove a processor for the logger instance
Handler processor

There are many kinds of handler processor types, more commonly used are three,Streamhandler,filehandler,nullhandler, details can be accessed Python Logging.handlers

After you create Streamhandler, you can set the log level by using the following methods, set the formatter formatter, and add or remove filter filters.

    • Ch.setlevel (logging. WARN) # Specifies the log level, and logs below the WARN level are ignored
    • Ch.setformatter (formatter_name) # set a formatter formatter
    • Ch.addfilter (filter_name) # Adds a filter that can add multiple
    • Ch.removefilter (filter_name) # Delete a filter
Streamhandler

To create a method:sh = logging.StreamHandler(stream=None)

Filehandler

To create a method:fh = logging.FileHandler(filename, mode=‘a‘, encoding=None, delay=False)

Nullhandler

The Nullhandler class is located in the core logging package and does not do any formatting or output.
It is essentially a "do-nothing" handler, which is used by library developers.

Formatter Formatter

Use the formatter object to set the last rule, structure, and content of the log information, and the default time format is%y-%m-%d%h:%m:%s.

To create a method:formatter = logging.Formatter(fmt=None, datefmt=None)

Where FMT is a formatted string of messages, Datefmt is a date string. If the FMT is not specified, the '% (message) s ' will be used. If you do not specify DATEFMT, the ISO8601 date format will be used.

Filter filters

Handlers and loggers can use filters to accomplish more complex filtering than levels. The filter base class only allows events below a specific logger level. For example, the filter initialized with ' a.b ' allows logger ' a.b ', ' a.b.c ', ' a.b.c.d ', ' A.B.D ' and other recorded events, logger ' a.bb ', ' b.a.b ' and so on. If initialized with an empty string, all events are accepted.

To create a method:filter = logging.Filter(name=‘‘)

Here is a summary of related concepts:

With these concepts in hand, it is important to be clear that logger is a tree-shaped hierarchy;
Logger can contain one or more handler and filter, i.e. logger is a one-to-many relationship with handler or Fitler;
A logger instance can add multiple handler, a handler can add multiple formatters or multiple filters, and the log level will inherit.

Element_relation.jpglogging Workflow Logging Module use process
    1. The first time you import the logging module or use the Reload function to re-import the logging module, the code in the logging module will be executed, which will result in the default configuration of the logging log system.
    2. Custom configuration (optional). The logging standard module supports three configuration methods: Dictconfig,fileconfig,listen. Where Dictconfig is configured through a dictionary logger,handler,filter,formatter;fileconfig is configured by a file, while listen listens on a network port and configures it by receiving network data. Of course, in addition to the above collectivization configuration, you can also directly invoke the methods in the Logger,handler and other objects in the code to explicitly configure.
    3. Use the GetLogger function in the global scope of the logging module to get an instance of the Logger object (the argument is a string representing the name of the Logger object instance, which is the name to get the corresponding Logger object instance).
    4. Log information is logged using methods such as debug,info,error,warn,critical in the Logger object.
Logging Module Processing Flow logging_flow.png
    1. Determine whether the level of the log is greater than the level of the Logger object, or, if it is greater than, execute down, otherwise the process ends.
    2. Generate logs. The first step is to determine if there is an exception, and if so, add the exception information. The second step is to handle the placeholder in the logging method (such as debug,info, etc.), which is a generic string formatting process.
    3. Filter by using filters registered in the Logger object. If there are multiple filters, the filter is filtered in turn, and if a filter returns false, the filter ends and the log information is discarded, no longer processed, and the process is ended. Otherwise, the processing process is executed down.
    4. Finds handlers in the current logger object and, if no handler is found, looks up to the logger object's parent logger and, if one or more handler is found, uses handler to process the log information. However, in each handler process log information, the log information will first determine whether the level is greater than the level of handler, if greater than the execution (by the Logger object into the handler object), otherwise, the processing process ends.
    5. Executes the filter method in the Handler object, which executes the filter that is registered to the handler object in turn. If there is a filter that determines that the log information is false, then all the filter is no longer executed, and the log information is discarded directly, and the processing process ends.
    6. Use the formatter class to format the final output. Note: Formatter with the 2nd step of the string format, it will add additional information, such as the time the log was generated, the source of the origin of the log sources of files, and so on.
    7. Really output log information (to network, file, terminal, mail, etc.). As to which destination to export to, it is determined by the kind of handler.

Note: The above content excerpt from the third article, the content of a slight change, reproduced hereby declare.

And look at the log configuration configuration method
    • Explicitly create logger logger, processor handler, and formatter formatter, and make relevant settings;
    • Configure it in a simple way, using the Basicconfig () function to configure it directly;
    • Configuration files are configured using the Fileconfig () function to read the configuration file;
    • Configuration by configuration dictionary, using the Dictconfig () function to read configuration information;
    • Configuration over the network, using the Listen () function for network configuration.
Basicconfig keyword Parameters
Key Words Description
FileName Creates a filehandler, using the specified file name instead of using Streamhandler.
FileMode If the file name is indicated, indicate the mode of the open files (default is ' a ' if no FileMode is indicated).
Format Handler uses the specified format string.
Datefmt Use the specified date/time format.
Level Indicates the level of the root logger.
Stream Initializes the streamhandler using the specified stream. This parameter is not compatible with ' filename ', and if two have one, ' stream ' is ignored.
Useful format formats
format Description
% (Levelno) s Print the value of the log level
% (LevelName) s Print Log level name
% (pathname) s To print the path of the current executing program
% (filename) s Print the current name of the executing program
% (FuncName) s Print the current function of the log
% (Lineno) d Print the current line number of the log
% (Asctime) s Time to print the log
% (thread) d Print thread ID
% (ThreadName) s Print Thread Name
% (process) d Print Process ID
% (message) s Print log information
Useful DATEFMT formats

Reference Time.strftime

Configuration Example Explicit configuration

Use the program logger.py as follows:

#-*-Encoding:utf-8-*-import Logging# Create Loggerlogger_name ="Example" logger = Logging.getlogger (logger_name) logger.setlevel (logging. DEBUG)# Create File Handlerlog_path ="./log.log" FH = logging. Filehandler (Log_path) fh.setlevel (logging. WARN)# Create FORMATTERFMT ="% (asctime) -15s% (levelname) s% (filename) s% (Lineno) d% (process) d% (message) s" DATEFMT ="%a%d%b%Y%h:%m:%s" formatter = logging. Formatter (FMT, DATEFMT)# Add handler and formatter to Loggerfh.setformatter (formatter) Logger.addhandler (FH)# Print Log Infologger.debug (' Debug message ') Logger.info (' Info message ') Logger.warn (' Warn message ') Logger.error (' Error message ') logger.critical (' Critical message ') '##### file Configuration profile logging.conf as follows: "' [loggers]keys=root,example01[logger_root]level=debughandlers=hand01,hand02[ Logger_example01]handlers=hand01,hand02qualname=example01propagate=0[handlers]keys=hand01,hand02[handler_ Hand01]class=streamhandlerlevel=infoformatter=form02args= (Sys.stderr,) [handler_hand02]class=filehandlerlevel= Debugformatter=form01args= (' Log.log ',' A ') [formatters]keys=form01,form02[formatter_form01]format=% (asctime) s% (filename) s[line:% (lineno) d]% (levelname ) s% (message) s ' ' Use the program logger.py as follows: ' '#!/usr/bin/python#-*-Encoding:utf-8-*-import loggingimport logging.configlogging.config.fileConfig ("./logging.conf")# Create Loggerlogger_name ="Example" logger = Logging.getlogger (logger_name) Logger.debug (' Debug message ') Logger.info (' Info message ') Logger.warn (' Warn message ') Logger.error ( ' error message ') logger.critical ( ' critical message ') ' ##### dictionary configuration interested child boots can be written using ' logging.config.dictConfig (config) ' to write an example program sent to me to provide me with a perfect article. ##### Monitor configuration interested child boots can use ' Logging.config.listen (port=default_logging_config_port) ' Write a sample program sent to me to provide me with a perfect article. For more detailed information [logging.config log Configuration] (http://python.usyiyi.cn/python_278/library/logging.config.html  #module-logging.config) ### reference * [English python logging HOWTO] (https://docs.python.org/2/howto/logging.html #logging-basic-tutorial) * [Chinese python log HOWTO] (http://python.usyiyi.cn/python_278/howto/ Logging.html #logging-basic-tutorial) * [python Log system logging] (http://www.52ij.com/jishu/ 666.html) * [Logging Module Learning Note: Basicconfig profile] (http://www.cnblogs.com/bjdxy/archive/2013/04/12/3016820.html) * Some other predecessors blog related articles        

Python logging module Usage tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.