How to use the Python standard log module logging _python

Source: Internet
Author: User
Tags system log

The recent writing of a reptile system required the use of Python's logging module, so I learned it.
The log system in the Python standard library is supported from Python2.3. As long as the import logging this module can be used. If you want to develop a log system, both output the log to the console and write to the log file, as long as this is used:

Copy Code code as follows:

Import logging
# Create a Logger
Logger = Logging.getlogger (' MyLogger ')
Logger.setlevel (logging. DEBUG)
# Create a handler for writing to the log file
FH = logging. Filehandler (' Test.log ')
Fh.setlevel (logging. DEBUG)
# and create a handler for output to the console
ch = logging. Streamhandler ()
Ch.setlevel (logging. DEBUG)
# defines the output format of the handler
Formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')
Fh.setformatter (Formatter)
Ch.setformatter (Formatter)
# Add Handler to Logger
Logger.addhandler (FH)
Logger.addhandler (CH)
# Record a log
Logger.info (' Foorbar ')

In combination with the above example, let's say a few of the most commonly used APIs:
Logging.getlogger ([name])
Returns a logger instance that returns root logger if name is not specified. As long as name is the same, the returned logger instance is the same and has only one, that is, the name and logger instances are one by one corresponding. This means that there is no need to pass the logger instance in each module. As long as you know name, you can get the same logger instance.
Logger.setlevel (LVL)
Set the level of the logger at the following levels:
    
Level order: NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL
If the level of Looger 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
Copy Code code as follows:

Logger.debug ("Foobar") # does not output
Logger.info ("Foobar") # Output
Logger.warning ("Foobar") # Output
Logger.error ("Foobar") # Output
Logger.critical ("Foobar") # Output

Logger.addhandler (HDLR)
The handler object allows you to write the contents of the log to different places. For example, a simple streamhandler is the place where the log is written to a similar file. Python offers more than 10 kinds of practical handler, which are more commonly used:
Copy Code code as follows:

Streamhandler: Output to console
Filehandler: Output to File
Baserotatinghandler can be written to a different log in time. For example, a file that writes a log to a different date at the end of the day.
Sockethandler write log with TCP network connection
Datagramhandler write log with UDP network connection
Smtphandler wrote the log as an email and mailed it.

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 the call is found root Logger does not have any handler, will automatically invoke Basicconfig add a handler* if root logger already have handler, this function does nothing to configure root with Basicconfig Logger output format and level:
Copy Code code as follows:

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

The Ogger object provides a log interface directly. Formatter describes the format of the log. Handler write the log to different places, you can save the log cost files, you can write a log file every hour, but also to the log through the socket to other machines.
Look at the simplest formatter objects. Formatter specifies the header information for each log record, which means that you can specify the time format, process number, filename, function name, etc. of the log record. You can use this method to create a formatter object:
Copy Code code as follows:

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

The FMT parameter specifies whether the process number, file name, function name, and so on appear and format, DATEFMT is a datetime format, and the default date format is accurate to microseconds, such as ' 2003-07-08 16:49:45,896 '. You can specify more than one field in FMT, each field in the format of "% (<dictionary key>) S", for example, you want to print time, log level, log information can be used in the following format:
Copy Code code as follows:

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

When recording the crawler system log, you need to define the level of logging, the higher the level, the more detailed the log. We can use a dictionary to set different levels of the corresponding log information:
Copy Code code as follows:

#用字典保存日志级别
Format_dict = {
1:logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s '),
2:logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s '),
3:logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s '),
4:logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s '),
5:logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')
}

Encapsulate the code at the beginning of this article in a class
Copy Code code as follows:

#开发一个日志系统, both the log output to the console and the log files are written
Class Logger ():
def __init__ (self, logname, loglevel, logger):
'''
Specifies the file path to save the log, the log level, and the calling file
Save Log to specified file
'''

# Create a Logger
Self.logger = Logging.getlogger (Logger)
Self.logger.setLevel (logging. DEBUG)

# Create a handler for writing to the log file
FH = logging. Filehandler (logname)
Fh.setlevel (logging. DEBUG)

# and create a handler for output to the console
ch = logging. Streamhandler ()
Ch.setlevel (logging. DEBUG)

# defines the output format of the handler
#formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')
Formatter = Format_dict[int (loglevel)]
Fh.setformatter (Formatter)
Ch.setformatter (Formatter)

# Add Handler to Logger
Self.logger.addHandler (FH)
Self.logger.addHandler (CH)


def getlog (self):
Return Self.logger

It is a simple log system that is called again by the following ways
Copy Code code as follows:

Logger = Logger (logname= ' Log.txt ', loglevel=1, logger= "Fox"). Getlog ()
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.