Python Logging-Advanced

Source: Internet
Author: User
Tags glob

Reference:

Advanced Logging Tutorial:https://docs.python.org/2.7/howto/logging.html#advanced-logging-tutorial

15.7. logging-logging facility for python:https://docs.python.org/2.7/library/logging.html?highlight=logging# Module-logging

15.8. Logging.config-logging configuration:https://docs.python.org/2.7/library/logging.config.html

15.9. Logging.handlers-logging handlers:https://docs.python.org/2.7/library/logging.handlers.html


####################################################################


For Python's logging module, you can view the article first:

Python Logging-Beginner: http://blog.csdn.net/u012005313/article/details/51581442


The Logging module has 4 most critical components: Logging,handler,filter and Formatter

The official explanations are as follows:

    • Loggers expose the interface that application code directly uses.
    • Handlers send the log records (created by loggers) to the appropriate destination.
    • Filters provide a finer grained facility for determining which log records to output.
    • Formatters specify the layout of log records in the final output.

When using the logging module, you first need to get an instance of the logger class called the Logger (logger). Each logger has a name, and the logging module manages these loggers by name, and the two loggers can be identified by a hierarchy of namespaces (each instance have a name, and they is conceptually Arranged in a namespace hierarchy using dots (periods) as separators). For example, the recorder named ' One ' is the father of the recorder whose name is ' One.two ', ' one.three ', ' one.four ', that is, using the dot ('. ') ) to determine the parent-child relationship . The name of the recorder does not have any requirements, you can choose what you like, but the best name can indicate the role of the logger in the program.


You can use the GetLogger function to get a logger, commonly named as a module name or a file name:

Logger = Logging.getlogger (__name__)


The parent of all loggers (logger) is the root logger, whose name is "root".


By default, the information is output to the console, and the default output format is logging. Basic_format:

Severity:logger Name:message


These can be modified by the function Basicconfig:

Basicconfig (**kwargs)
function function: Basic configuration for the entire logging system (do basic configuration for the logging system)

If the root logger already has a processor (handler) configuration (the processor will be explained below), then function Basicconfig will not work.


You can use the following keyword parameters:



If you use the Baskcconfig function in multiple threads, the function must be used in the main thread.


####################################################

The flowchart is as follows:




################################################################## #33

The following explains the use of Logger,handler and formatter


Logger

The logger has three parts of the function:

1. It provides some methods (debug (), info (), warning (), error (), critical (), etc.) for developers to use

2. The logger determines whether to generate information (message) based on its own rank or filter

3. The logger sends information to all processors bound to itself (handler)


The most commonly used method on a logger can be divided into two sections: Configuration and information delivery


The following are the most common configuration methods:

1. Logger.setlevel (): Specifies the minimum level (he lowest-severity) that the logger can handle the information. In the logging module, debug is the lowest level and the critical is the highest level. For example, to set the logger level to info, the logger can process info,warning,error,critical information and ignore the debug level.

2. Logger.addhandler () and Logger.removehandler (): Bind processor (handler) to the specified logger, and unbind

3. Logger.addfilter () and Logger.removefilter (): Bound filter (filter) to the specified filter, and unbound


When the logger configuration is dismissed, the following methods can process the log information:

Logger.debug (), Logger.info (), logger.warning (), Logger.error (), and logger.critical () functions: used to process information while setting the level or Severity), so that the logger can determine whether to process the information


The GetLogger function returns a reference to a logger instance that is used more than once to refer to a logger instance that references the same name to return the same logger.

Each logger must be set to a level or severity. But if it is not explicitly set, it will be set according to its parent's rank, and if the parent is not explicitly set, then continue to look for the father's parents, and so on-find all the fathers until an explicit level is found. The root logger is the parent of all loggers, and its rank defaults to warning.


The logging module sets up a good method by using a hierarchical namespace to manage all loggers. The child logger will transmit the information to the processor (handler) that is bound to the parent logger, so you do not need to configure the processor for all loggers, just bind the processor to the parent logger, Then the message from the child logger can be processed by the processor (you can turn off propagation by setting the logger's propagate property to False, which is true by default).



################################################

Handler


The processor is responsible for sending the information to the specified location. A logger can bind 0 or more processors (via the AddHandler method). For example, we need to save all the log information in the program to the file, send the information of level error and above to the console, send all the critical level information to email, then we just need to define 3 different processors and specify these 3 destinations on the processor.


Usually we do not use the handler instance directly, but instead refer to its subclasses. Handler defines some basic functions as a base class, and subclasses extend additional functionality. The commonly used handler subclasses are Streamhandler and Filehandler, and we will also refer to Rotatingfilehandler (detailed handler subclass).


We don't have much of a way of using handler. Commonly used in the configuration phase:

1. SetLevel: Specifies the lowest level the processor can handle

2. Setformatter: Binds a formatter to the processor

3. AddFilter and Removefilter


The following is an introduction to the commonly used handler subclasses:

Class logging. Streamhandler (Stream=none)
Output log information to the console

Class logging. Filehandler (filename, mode= ' A ', Encoding=none, Delay=false)
Output log information to a file. The usual way to open a file is to add information.

Class Logging.handlers.RotatingFileHandler (filename, mode= ' A ', maxbytes=0, backupcount=0, Encoding=none, delay=0)

Output log information to a file. You need to set the maximum number of bytes (maxbytes) and file Backups (Backupcount) that the file occupies. The current input file is always the filename file, for example, filename is ' he.log ', so when the file takes up the maximum byte, Save the He.log file as a he.log.1 file, and if the He.log.1 file already exists, save it as a he.log.2 file, and so on until the number of file backups is reached, for example, backupcount= 5, if the he.log.5 file already exists, it is deleted.


If all loggers (logger) do not have a binding processor (handler), the root logger generates a handler output to the console by default.


################################ #3

Formatter


The formatter object configures the structure, order, and content of the final log. It has two optional parameters:

Logging. Formatter.__init__ (Fmt=none, Datefmt=none)

The default FMT format is to use the original information. The default time format is:

%y-%m-%d%h:%m:%s

################################ #3

You can use numbers when you set the level or severity. Some constants are built into the logging module, as shown in the following table:




#################################

Here are some uses of logging:


1. Using the logging module in multiple threads

The logging module is thread-safe, so you can work with the same logging instance in multiple threads. However, the logging module is not process-safe and may be problematic for use in multiple processes.


2. Using Logging in multiple modules

module_a.py:

#!/usr/bin/env python#-*-coding:utf-8-*-import loggingimport module_b# Create logger instance logger = Logging.getlogger (' Module_ A ') Logger.setlevel (logging. DEBUG) # Create stream Processor sh = logging. Streamhandler () Sh.setlevel (logging. DEBUG) # Create Formatterformatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ') # Formatter bound on Handler Sh.setformatter (Formatter) # Handler binding on Logger Logger.addhandler (SH) logger.debug ("This was Debug") Logger.info ("This is Info") Module_b.hello () Logger.warn ("This was warn") logger.error ("This is the error") Modu = Module_b.clas () Modu.hi () logger.critical ("This is Critical ")

module_b.py:

#!/usr/bin/env python#-*-coding:utf-8-*-import logging# Create Loggerlogg = Logging.getlogger (' Module_a.module_b ') Logg.setlevel (logging. DEBUG) class Clas:def __init__ (self): Logg.info ("Class:clas") def Hi (self): Logg.debug ("Clas-hi") def hello (): Logg.error ("Hello World")

Results:



As shown, the child logger (' module_a.module_b ') can not set the processor itself, and the processor that invokes the parent logger binding


The following changes can make the results more intuitive and understandable:

# Create Formatterformatter = logging. Formatter ('% (asctime) s-% (name) 20s-% (levelname) 8s-% (message) s ')

Or:

# Create Formatterformatter = logging. Formatter ('% (asctime) s-% (name) -20s-% (levelname) -8s-% (message) s ')


3. Using multiple processors and formatter

multiple_handlers_and_formatters.py:

Binds two processors, one processor outputs all information to the console window in the form of:

'% (asctime) s% (name) -6s:% (levelname) -8s:% (message) s '

The other processor outputs the error level and above information to the file Logging2.log with the following information format:

'% (levelname) -8s-% (message) s '

#!/usr/bin/env python#-*-coding:utf-8-*-import logging# Create Loggerlogger = Logging.getlogger (__name__) Logger.setlevel (logging. DEBUG) # Set Streamhandlersh = logging. Streamhandler () Sh.setlevel (logging. DEBUG) # Set Formatterformatter1 = logging. Formatter ('% (asctime) s% (name) -6s:% (levelname) -8s:% (message) s ') Sh.setformatter (formatter1) # Set FILEHANDLERFH = Logging. Filehandler ("Multi.log") fh.setlevel (logging. ERROR) # Set Formatterformatter2 = logging. Formatter ('% (levelname) -8s-% (message) s ') Fh.setformatter (formatter2) # Add handlers to Loggerlogger.addhandler (SH) Logger.addhandler (FH) # logginglogger.debug (' Debug Message ') Logger.info (' info message ') Logger.warn (' warnning Message ') Logger.error (' Error message ') logger.critical (' Critical message ')


Results:

Console window:



Multi.log:

ERROR   -error messagecritical-critical Message

There is also a more convenient way to use the Basicconfig function:

#!/usr/bin/env python#-*-coding:utf-8-*-import logginglogging.basicconfig (level=logging. DEBUG, format= '% (asctime) s% (name) -6s:% (levelname) -8s:% (message) s ') # Create Loggerlogger = Logging.getlogger (__name_ _) Logger.setlevel (logging. DEBUG) # Set filehandlerfh = logging. Filehandler ("Multi.log") fh.setlevel (logging. ERROR) # Set formatterformatter = logging. Formatter ('% (levelname) -8s-% (message) s ') Fh.setformatter (Formatter) # Add handlers to Loggerlogger.addhandler (FH) # Logginglogger.debug (' Debug Message ') Logger.info (' info message ') Logger.warn (' warnning message ') Logger.error (' Error Message ') logger.critical (' Critical message ')

can achieve the same effect


4. Using the Filerotationhandler processor

The code is as follows:

#!/usr/bin/env python#-*-coding:utf-8-*-import globimport loggingimport logging.handlerslog_filename = ' rotation/ Logging_rotatingfile_example.out ' # set up a specific logger and our desired output Levelmy_logger = Logging.getlogger (' My Logger ') My_logger.setlevel (logging. DEBUG) # Add the log message handler to the Loggerhandler = Logging.handlers.RotatingFileHandler (Log_filename, maxbytes=20 , backupcount=5) My_logger.addhandler (handler) # Log some messagesfor I in range: my_logger.debug (' i =%d '%i) # See what F Iles is createdlogfiles = Glob.glob ('%s* '%log_filename) for FILENAME in Logfiles:print filename

Set the maximum capacity of a single file here to 20 bytes, set 5 backup files (so a total of 6 files)

In the Rotation folder:


Python Logging-Advanced

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.