Recently wrote a crawler system, need to use the Python logging module, so they learned a bit.
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:
Copy CodeThe code is as follows:
Import logging
# Create a Logger
Logger = Logging.getlogger (' MyLogger ')
Logger.setlevel (logging. DEBUG)
# Create a handler to write to the log file
FH = logging. Filehandler (' Test.log ')
Fh.setlevel (logging. DEBUG)
# Create another handler for output to the console
ch = logging. Streamhandler ()
Ch.setlevel (logging. DEBUG)
# define 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 ')
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
Copy CodeThe code is 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 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:
Copy CodeThe code is as follows:
Streamhandler: Output to console
Filehandler: Output to File
Baserotatinghandler can be written to different logs by time. For example, the log is written by day to the end of a different date file file.
Sockethandler write log with TCP network connection
Datagramhandler write log with UDP network connection
Smtphandler the log as 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 the call finds root 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:
Copy CodeThe code is 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 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:
Copy CodeThe code is as follows:
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:
Copy CodeThe code is as follows:
'% (asctime) s-% (levelname) s-% (message) s '
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:
Copy CodeThe code is 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 CodeThe code is as follows:
#开发一个日志系统, both output the log to the console and write the log file
Class Logger ():
def __init__ (self, logname, loglevel, logger):
‘‘‘
Specify the file path to save the log, the log level, and the calling file
Depositing a log in a specified file
‘‘‘
# Create a Logger
Self.logger = Logging.getlogger (Logger)
Self.logger.setLevel (logging. DEBUG)
# Create a handler to write to the log file
FH = logging. Filehandler (logname)
Fh.setlevel (logging. DEBUG)
# Create another handler for output to the console
ch = logging. Streamhandler ()
Ch.setlevel (logging. DEBUG)
# define 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 by the following method
Copy CodeThe code is as follows:
Logger = Logger (logname= ' Log.txt ', loglevel=1, logger= "Fox"). Getlog ()
How to use the Python standard log module logging