Python standard log module logging and log system design

Source: Internet
Author: User

Reprint: http://www.cnblogs.com/goodhacker/p/3355660.html

The log system in the Python standard library is supported from Python2.3. As long as import logging This module can be used. If you want to Develop a log system, you need to output the log to the console and write to the log file, as long as you use:

1ImportLogging23#Create a Logger4 Logger = Logging.getlogger (‘MyLogger‘)5Logger.setlevel (logging. DEBUG)67#Create a handler to write to the log file8 fh = logging. Filehandler (‘Test.log‘)9Fh.setlevel (logging. DEBUG)1011#Create another handler for output to the consoleCH =Logging. Streamhandler ()13Ch.setlevel (logging. DEBUG)1415#Define the output format of the handlerFormatter = logging. Formatter (‘% (asctime) s-% (name) s-% (levelname) s-% (message) s ' ) 17  Fh.setformatter (formatter) 18 ch.setformatter ( Formatter) 19 20 #21  Logger.addhandler (FH) 22 logger.addhandler (CH) 23 24 #  record a log 25 logger.info (foorbar      

With the above example, we'll say a few of the most commonly used APIs:

logging. GetLogger ([name])

Returns a logger instance that returns root logger if no name is specified. As long as name is the same, the returned logger instances are the same and only one, that is, the name and the logger instance are one by one corresponding. This means that there is no need to pass logger instances in each module. As long as you know name, you can get the same logger instance.

Logger. SetLevel (lvl)

 The level of the logger is set to the following levels:

    

Level high and low order: NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL

If the Looger level is set to info, then logs less than the info level are not output, and logs that are greater than or equal to the info level are output

1 Logger.debug ("Foobar")#No output2 logger.info ( "foobar< Span style= "COLOR: #800000" > ") # output 3 logger.warning (" foobar  ") # output 4 logger.error ( "foobar") # output 5 logger.critical ( Span style= "COLOR: #800000" > "foobar") # output          

  Logger. AddHandler (hdlr)

The handler object can be used to write the contents of the log to different places. A simple streamhandler, for example, is to write a log to a file-like location. Python offers more than 10 practical handler, which are commonly used:

 1 streamhandler: Output to console 2 filehandler: Output to file 3 Span style= "COLOR: #000000" >baserotatinghandler can be written to a different log by time. For example, the log is written by day to the end of a different date file file. 4 sockethandler write log with TCP network connection 5 datagramhandler write log with UDP network connection 6 smtphandler log written in an email          

logging. basicconfig ( [ **kwargs ] ) * This function is used to configure root logger, create a streamhandler for root logger, and set the default format. * These functions: Logging.debug (), Logging.info (), logging.warning (), Logging.error (), logging.critical ()   If Root is found when calling Logger does not have any handler, will automatically call Basicconfig add a handler* if root logger already has handler, this function does nothing to configure root with Basicconfig Logger output format and level:

Import Logging2 logging.basicconfig (format='% (levelname) s:% (message) s', level=logging. DEBUG)3 logging.debug (' Thismessage should appear on the console')      

The Logger object provides a log interface directly. Formatter describes the format of the log. Handler write the logs to different places, you can save the log cost of files, you can write a log file every hour, you can also send the log through the socket to other machines.

Judging from the simplest formatter object. Formatter specifies the header information for each log record, that is, you can specify the time format, process number, file name, function name, and so on for logging. You can use this method to create a formatter object:

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

The FMT parameter specifies whether the process number, file name, function name, and other information appear as well as format, DATEFMT is a datetime format, and the default date format is accurate to microseconds, such as ' 2003-07-08 16:49:45,896 '. In FMT, you can specify multiple fields, each of which is in the format "% (<dictionary key>) s", such as the time you want to print, the log level, and the log information that you can use with the following format:

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

All the fields that can be used are the following table:

You need to define the level of logging when logging the crawler system, the higher the level, the more detailed the log is. We can use a dictionary to set different log information for different levels:

1#Save log levels in a dictionary2 Format_dict ={3 1:logging. Formatter (‘% (asctime) s-% (name) s-% (levelname) s-% (message) s‘),4 2:logging. Formatter (‘% (asctime) s-% (name) s-% (levelname) s-% (message) S "5 3:logging. Formatter ( ' % (asctime) s-% (name) s-% (levelname) s-% ( Message) S "6 4:logging. Formatter ( ' % (asctime) s-% (name) s-% (levelname) s-% ( Message) S "7 5:logging. Formatter ( ' % (asctime) s-% (name) s-% (levelname) s-% ( Message) S ' ) 8}   

Encapsulate the code at the beginning of this article in a class

1#Develop a log system that outputs logs to the console and to log files2ClassLogger ():3Def__init__(Self, logname, loglevel, logger):4‘‘‘5Specify the file path to save the log, the log level, and the calling file6Depositing a log in a specified file7‘‘‘89#Create a LoggerTen Self.logger =Logging.getlogger (Logger)11Self.logger.setLevel (logging. DEBUG)1213#Create a handler to write to the log fileFH =Logging. Filehandler (logname)15Fh.setlevel (logging. DEBUG)1617#Create another handler for output to the consoleCH =Logging. Streamhandler ()19Ch.setlevel (logging. DEBUG)2021st#Define the output format of the handler22#Formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')23 formatter = Format_dict[int (loglevel)]24  Fh.setformatter (formatter) 25  Ch.setformatter (formatter) 26 27 # add logger handler28 Span style= "COLOR: #000000" > Self.logger.addHandler (FH) 29  Self.logger.addHandler (CH) 30 31 32 def Getlog (self): 33 return Self.logger  

It is a simple log system that is called by the following method

Logger = Logger (logname='log.txt', loglevel=1, logger="Fox"). Getlog ()    

Python standard log module logging and log system design

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.