Python Logging Module

Source: Internet
Author: User

Python Logging Module TOC
    • Objective
    • Design of logging Module
      • The logical inheritance structure of logger
      • Log output to multiple I/O devices
      • Log format and Content rich
      • The level of logging is also the classification of the log on the level dimension.
      • You can filter by the level of the Log object and specify output I/O.
      • Logger primarily responsible for the basic unit of Succession relations
      • Handler is responsible for the output direction of the basic unit
      • Formater responsible for log format
      • Filter is responsible for filtering, can be placed in logger and handler, with the inheritance propagation mechanism, flexible filtering.
    • Logging.basicconfig ()
    • Logger
    • Handler
    • Formatter
    • Filter
    • Log Recoder
Objective

Python is very flexible in logging processing and is fully functional enough to meet all of the log requirements.
So powerful, of course, is the log system design is a very good drawback.
In addition, to understand its design, to comb clear the module in the relationship between several important objects, and their respective responsible mission task!

Design of logging Module

The

can refer to the Java Log System design, this article uses the humorous language tone, starts from the demand, clarifies how the log system is designed.
I follow this article here, briefly outline the following points

    • requirements
      • log messages can be exported to multiple media
      • log message contents can be formatted
      • program code is organized by Package,module, to meet different packages, different modules of code, have their own log processing, but also to have a log classification (the log is actually a record of an event event occurred), because it is an incident triggered by the log, so the level of the event to be reflected through the log, so the log is also divided into levels.
      • According to the previous requirement, the log has a level, so it is necessary to an article on this sort of log classification. For example, different log levels are handled differently, and there is a prioritization relationship between different log levels. Depending on your priorities, you can already set how a log level is processed and how the following levels are handled. The processing of the
      • log and the output of the log two places can also be customized filtering requirements.
    • parsing requirements
      • First, there is a class to abstract the log message, which has at least two attribute timestamps, the message itself. Log messages are meant to record the occurrence of an event. Then use the LogRecord class to represent the log messages.
      • LogRecord to output to a different medium, it abstracts a handler class, different handler handles LogRecord to different mediums
      • logrecord to format the output, Then there will be a formatter class, with different formatter that can be processed for different format types.
      • to meet different packages, modules, have their own way of handling. From the perspective of the user's last use, I want to create multiple handler every time too troublesome, then also need a to integrate these objects of a processor, this is an abstract class, called logger. Each package, module can have its own logger, can use a unique name to identify the uniqueness of this logger. The
      • log message LogRecord is hierarchical, and other processing LogRecord can be handled differently depending on its level. The
      • is filtered at both the logger and handler log routing locations. That is, add a filter filtering object to logger and handler.

This is based on the need to focus on the relationship between the core classes
Logger contains handler,handler containing formatter. All deal with LogRecord. Both logger and handler can filter the LogRecord by adding a filter.

Inheritance of Logger
1.就是通过logger的名字的命名层级来查找其parent logger。使用‘.‘来表示继承关系。2.logger的继承其实是继承几个方面    2.1. level的继承:子logger写日志的时候,优先使用本身设置的level,如果没有设置,则逐层向上级父logger查询,直到查询到为止。最极端的情况是,使用rootlogger的默认日志级别——WARNING.可以使用logger.getEffectivaLeve()获取有效的等级。    2.2. handler的继承,先将日志对象传递给子logger的所有handler处理,处理完毕,如果孩子的logger的propagate属性设置的是1(TRUE),那么日志record对象会被传播给上级父logger,该父logger的handler都会进行处理,如果父logger的propagate也是1,那么会继续向上。如果子logger的propagate就是0,就不会传播,极端,如果是子logger都没有handler,且不传播,那么就会极端的给logging.lastResort这是一个内置的handler,不会和任何一个logger关联,and acts like StreamHandler which the event description message to the current value of sys.stderr.这个logging.lastResort是没有formatted的,所以我们会看到就message原始格式,输出到标准输出中。3. 总的来说,其实是逻辑上的继承,只是利用层级关系,可以来使用父logger的level和handler。
The logical inheritance structure of logger

The construction of logger is created by naming logger. The name is the only representation of the entire program.
This design allows you to combine your own logs with the logs generated by using a third-party module. Do not deliberately write in their own code to be compatible with third-party modules generated by the processing of the log. All to the logging can be integrated processing.
Representation of the inheritance relationship between Logger :

    • is through '. ' Number to indicate. such as: ' foo ' ' Foo.bar ' Foo.bar.baz ' foo ' is the father of ' foo.bar ', ' foo.bar ' is the father of ' Foo.bar.baz '. The father of ' Foo ' is actually ' root ' logger.
    • The representation of this hierarchical relationship is very similar to the method of representing hierarchical relationships between packages and modules.
Logging.basicconfig ()

The module-level function is set to root logger. If the root logger already has handler, then the call function is not useful, the following is the source:

def basicConfig(**kwargs):    _acquireLock()    try:        if len(root.handlers) == 0:            filename = kwargs.get("filename")            if filename:                mode = kwargs.get("filemode", ‘a‘)                hdlr = FileHandler(filename, mode)            else:                stream = kwargs.get("stream")                hdlr = StreamHandler(stream)            fs = kwargs.get("format", BASIC_FORMAT)            dfs = kwargs.get("datefmt", None)            fmt = Formatter(fs, dfs)            hdlr.setFormatter(fmt)            root.addHandler(hdlr)            level = kwargs.get("level")            if level is not None:                root.setLevel(level)    finally:        _releaseLock()
Logger Objects
  • Loggers is never instantiated directly,but always through the Module-level function Logging.getlogger (name).
  • Logger objects cannot be passed directly through the Logger class instance, and are generated through a module-level function Logging.getlogger (name) call.
  • Mutiple calls to GetLogger () with the same name would always return a refernece to the same Logger object.
    • The inheritance of logger in addition to some things like level (Geteffectivelevel ()) that can inherit from the parent. You can also propagate logrecord up through an inheritance relationship.
      Logger frequently Used methods:
Method usage Notes
Logger.info ()
Logger.debug ()
Logger.setlevel
Logger.geteffectivelevel ()
Logger.getchild (suffix) Return a child logger, the name of the child logger will be the parent logger name plus suffix
Logger.addfilter (filt)
Logger.removefilter (filt)
Logger.addhandler (HD)
Logger.removehandler (HD)
Handler

Handler is a basic class, and other different types of handler are inherited from it. Similarly, handler does not instantiate an object directly, but instead uses its subclasses.
Common Type Handler

handler note
Streamhandler Logging
Filehandler Logging
Nullhandler Loggging
Baserotatinghandler Logging.handlers
Rotatingfilehandler Logging.handlers
Timedrotatingfilehandler Logging.handlers
Sockethandler Logging.handlers

Wait a minute...

Handler Common methods:

    • Handler.addfilter ()
    • Handler.removefilter ()
    • Handler.setformatter ()
    • Handler.setlevel ()
Formatter

The class object is used to format the LogRecord.
The initialization of the Formtter object requires specifying the format of the log string, specifying the time format, and the style used by the formatted string.
The Fomatter object is added to the handler object.

Filter

The Filter object initialization requires the logger name to be specified, and only the LogRecord generated by the specified logger name will pass through the filter, otherwise it will be filtered out. Logger name filtering means that the specified logger name and its sub-logger are not filtered.

LogRecord

The carrier of the log content.
LogRecord instances are all instances of the Logger object's method.
The main classification of LogRecord is the classification at the log level, divided into debug,info,warning,error,critical

Put some practice code on it.
ImportLogginglogging.basicconfig (filename=' Logging_train.log ', Level=Logging. DEBUG) Logger1=Logging.getlogger (' 1 ') Logger2=Logger1.getchild (' 2 ')# Logger2 inherits from Logger1: that will be 1.2Logger2.propagate= True  # Whether the LogRecord object is passed upclassMyfilter (logging. Filter):# define your own filter class    def __init__( Self, s):# Filter All logs that do not contain the specified string         Self. filter_string=Sdef Filter( Self, red):# Logger and handler will call this method to determine the filter result.         if  Self. filter_stringinchRed.getmessage ():return True        Else:return FalseFilter1=Myfilter (' World ') handler1=Logging. Filehandler (' Logging_train1.log ') Handler2=Logging. Streamhandler () Formatter1=Logging. Formatter ('% (asctime) s % (name) s % (threadname) s % (process) s % (levelname) s % (message) s') Handler2.setformatter (Formatter1) Logger1.addhandler (handler1) Logger2.addhandler (handler2) Logger2.addfilter ( Filter1) Logger2.setlevel (logging. CRITICAL) Logger2.error ("Hello world!") Logger2.critical ("Python is the best all over the world!")

Python Logging Module

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.