Detailed description of Python self-built logging module and python self-built logging Module

Source: Internet
Author: User

Detailed description of Python self-built logging module and python self-built logging Module

Easy to use

At first, we used the shortest code to experience the basic functions of logging.

Import logginglogger = logging. getLogger () logging. basicConfig () logger. setLevel ('debug') logger. DEBUG ('logsomething ') # output out> DEBG: root: logsomething

Step 1: Use the logging. getLogger function to obtain a loger object, but this object is temporarily unavailable.
Step 2: Use the logging. basicConfig function to perform a series of default configurations, including format and handler.
Step 3: logger calls the setLevel function to define the log level as DEBUG. Finally, call the debug function and output a debug-level message, which is displayed on the standard output. Log Level in logging

Logging has a log-level mechanism when generating logs. By default, it has the following log levels:

CRITICAL = 50ERROR = 40WARNING = 30INFO 20DEBUG = 10NOTEST = 0

Each logger object has a log level, which only outputs logs higher than its level. If the level of a logger is INFO, logger. debug () cannot be called to output logs, while logger. warning () can.

Generally, the above six log levels fully meet our daily usage requirements.

Basic classes in logging

Logging is a basic module of python. Its source code position in python is as follows:

# Main Code/usr/lib/python2.7/logging/_ init __. py # extended handler and config/usr/lib/pyhon2.7/logging/config. py/usr/lib/python2.7/loging/handlers. py

Several Basic classes that constitute the logging trunk are in _ init _. py:

The first basic class LogRecord

A LogRecord object that corresponds to a row of data in the log. Generally, the information includes time, log level, message information, currently executed module, row number, and function name.
The LogRecord object can be considered as a large dictionary:

Class LogRecord (object): # indicates a log's class def getMessage (self): # Gets self. msg def markLogRecord (dict): # This method is very important. generate an empty LogRecord, and then update the member variable rv = LogRecord (None, None, "", 0, "", (), None, None) rv. _ dict __. update (dict) return rv

The second basic class Formatter

The Formatter object is used to define the log format. LogRecord stores a lot of information, but when printing logs, we only need a few of them. Formatter provides this function, it depends on a function of python:

# Print ('% (name) s: % (num) D' % {'name': 'My _ name', 'num ': 100}) out >>> my_name: 100
If LogRecord is the dictionary next to it, Formatter is the abstraction of the format string...

The important code is as follows:

Class Formatter (object): def _ init _ (self, fmt = None, datefmt = None): if fmt: self. _ fmt = fmt else: # default format self. _ fmt = "% (message) s" def format (self, record) # Use self. _ fmt Format s = self. _ fmt % record. _ dict _ return s

Third Basic Class: Filter and Filterer

Filter class, the function is very simple. The Filter. filter () function imports a LogRecord object and returns 1 by filtering. Otherwise, 0 is returned. As you can see from the code, it is actually a filtering of LogRecord. name.

The Filterer class contains a list of Filter objects, which are a set of Filter abstractions.

The important code is as follows:

Class Filter (object): def _ init _ (self, name = ''): self. name = name self. nlen = len (name) def filter (self, record): #1 indicates that record passes, 0 indicates that record does not pass if self. nlen = 0: return 1 elif self. name = record. name: return 1 # record. name does not start with "filter" elif record. name. find (self. name, 0, self. nlen )! = 0: return 0 # whether the last digit is return (record. name [self. nlen] = '. ') class Filterer (object): # This class actually defines a self. the list of filters = [] manages multiple filters def addFilter (self, filter): def removefilter (self, filter): def filter (self, record ): # filter by using all filters in the list. If any filter fails, 0 is returned. # example: # filter. name = 'A', filter2.name = 'a. B ', filter2.name = 'a, B, C' # record. only records like name = 'a, B, C, d' can be filtered by all filters.

Advanced classes in logging

With the above three basic classes, you can piece together some more important advanced classes, which can implement the important functions of logging.

Handler -- abstracts the output process of log. The Handler class inherits from Filterer. The abstraction of the process output by log in Handler class.
At the same time, the Handler class has a member variable self. level. The log level mechanism discussed in section 2 is implemented in Handler.
Handler has an emit (record) function, which outputs logs and must be implemented in the subclass of Handler.

The important code is as follows:

Class Handler (Filterer): def _ init _ (self, level = NOTEST) # handler must have the level Attribute self. level = _ checkLevel (level) def format (self, record): # Use self. formatter, formattercord def handler (self, record): # If the filter is used, the emit log rv = self. filter (record) self. emit (record) def emit (self, record): # Wait for the subclass to implement

Next, let's take a look at two simple handler subclasses. In the logging source code, there is a handler. py specifically defines a lot of complicated handler, some can cache logs in the memory, and some can perform log rotation.

StreamHandler
The simplest handler implementation writes logs to a stream. The default stream is sys. stderr.

The important code is as follows:

Class StreamHandler (Handler): def _ init _ (self, stream = None): if stream is None: stream = sys. stderr self. stream = stream def emit (self, record ): # write record information into the stream # handle some encoding exceptions fs = '% s \ n' # Each log has a line feed stream = self. stream. write (fs % msg)

FileHandler

Output logs to the file handler, inheriting StreamHandler

The important code is as follows:

Class FileHandler (StreamHandler): def _ init _ (self, filename, mode = 'A') # open a file StreamHandler in append mode. _ init _ (self, self. _ open () def emit (self, record): # consistent with streamhandler StreamHandler. emit (self, record)

Logger-an independent log Pipeline

What is logger?

+ The logger class is inherited from Filterer,

+ The logger object has a logger. level log level.

+ The logger object controls multiple handler: logger. handlers = []

+ There is a blessing relationship between logger objects.

Simply put, the logger class integrates all the above LogRecord, Filter, Formatter, and handler classes. First, logger generates a LogRecord read/write according to the input. After filtering and Formatter, the logger sends the log through all handler in the self. handlers list.

A logger may have multiple handler, which can place a log in any location.

Class Logger (Filterer): def _ init _ (self, name, level = NOTEST) # handler list self. handlers = [] self. level = _ checklevel (level) def addHandler (self, hdlr): def removeHandler (self, hdlr): def _ log (self, level, msg, args, exc_info = None, extra = None): # create a LogRecord Object record = self in the _ log function. makeRecord (self. name, level, fn, lno, msg, args, exc_info, func, extra) # hand to handle function self. handle (record) def handle (self, reord): # filter, and then call callHandlers if (not self. disabled) and self. filter (record): self. callHandlers (record) def callHandlers (self, record): # from the current logger to all the parent logger, recursive handl incoming record c = self while c: for hdlr in c. handlers: hdlr. handle (record) # enter the emit function of handler to send log .... c = c. parent

LoggerAdapter -- an extension of the standard logger

Many member variables are provided in the LogRecord dictionary. However, if you still want to include more information you want to see when outputting logs, such as when you generate this log, if you call some functions to obtain other information, you can add the information to the Logger. The LoggerAdapter class plays this role.

The LoggerAdapter class is very interesting. if no changes are made, there is no difference between the LoggerAdapter class and Logger. LoggerAdapter only wraps the Logger class.

The usage of LoggerAdapter is described in the comment of its member function process:

def process(self, msg, kwargs): ''' Normally,you'll only need to overwrite this one method in a LoggerAdapter subclass for your specific needs. '''

That is to say, rewrite the process function. The following is an example:

Import loggingimport randomL = logging. getLogger ('name') # define a function and generate 0 ~ 1000 random number def func (): return random. randint (1,1000) class myLogger (logging. loggerAdapter): # inherit LoggerAdapter, override process, generate a random number, and add it to the front of msg def process (self, msg, kwargs): return '(% d), % s' % (self. extra ['name'] (), msg), kwargs # input LA = myLogger (L, {'name': func}) to the function object in the dictionary # now, do some loggingLA. debug ('some _ loging_messsage') out> DEBUG: name :( 167), some_loging_messsage

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.