How to use logging in the python standard Log Module

Source: Internet
Author: User

I recently wrote a crawler system and needed the python logging module. So I learned about it.
The log system in the python standard library is supported from Python2.3. You only need to import the logging module. If you want to develop a log system, you need to output logs to the console and write log files as long as they are used as follows:
Copy codeThe Code is as follows:
Import logging
# Create a logger
Logger = logging. getLogger ('mylogger ')
Logger. setLevel (logging. DEBUG)
# Create a handler for writing log files
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 handler output format
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 ')

Based on the example above, we will discuss several of the most commonly used APIs:
Logging. getLogger ([name])
Returns a logger instance. If no name is specified, the root logger is returned. As long as the names are the same, the returned logger instances are the same and only one, that is, the names and logger instances correspond one to one. This means that you do not need to pass the logger instance in each module. You only need to know the name to get the same logger instance.
Logger. setLevel (lvl)
Set the logger level. The level has the following levels:
    
Level order: NOTSET <DEBUG <INFO <WARNING <ERROR <CRITICAL
If the looger level is set to INFO, no logs smaller than INFO level are output, and logs larger than or equal to INFO level are output.
Copy codeThe Code is as follows:
Logger. debug ("foobar") # No 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 log Content to different places. For example, a simple StreamHandler writes logs to similar files. Python provides more than a dozen practical handler types, which are commonly used:
Copy codeThe Code is as follows:
StreamHandler: output to the console
FileHandler: output to file
BaseRotatingHandler can be written to different logs by time. For example, you can write logs to files whose names end with different dates by day.
SocketHandler writes logs over TCP connections
Mongoramhandler writes logs over a UDP Network
SMTPHandler writes the LOG into an EMAIL and mail it out.

Logging. basicConfig ([** kwargs]) * this function is used to configure the root logger, create a StreamHandler for the root logger, and set the default format. * These functions: logging. debug (), logging.info (), and logging. warning (), logging. error (), logging. critical () if the root logger does not have any handler when it is called, it will automatically call basicConfig to add a handler * If the root logger already has a handler, this function uses basicConfig to configure the output format and level of the root logger:
Copy codeThe Code is as follows:
Import logging
Logging. basicConfig (format = '% (levelname) s: % (message) s', level = logging. DEBUG)
Logging. debug ('this message shoshould appear on the console ')

The ogger object directly provides the log interface. Formatter describes the log format. Handler writes logs to different places. You can save the logs to local files, write one log file every hour, and upload the logs to other machines through socket.
The simplest formatter object. Formatter specifies the header information of each log record, that is, you can specify the time format, process number, file name, function name, and other information of the log record. 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 are displayed and formatted. datefmt is in the date and time format. The default date format is accurate to microseconds, for example, '2017-07-08 16:49:45, 896 '. Multiple fields can be specified in fmt. The format of each field is "% (<dictionary key>) s ", for example, you can use the following format to print the time, log level, and log information:
Copy codeThe Code is as follows:
'% (Asctime) s-% (levelname) s-% (message) s'

When you record crawler system logs, You need to define the log record level. The higher the level, the more detailed the logs are. We can use a dictionary to set different log information corresponding to different levels:
Copy codeThe Code is as follows:
# Saving log levels with dictionaries
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 from this article in a class
Copy codeThe Code is as follows:
# To develop a log system, you must output logs to the console and write log files.
Class Logger ():
Def _ init _ (self, logname, loglevel, logger ):
'''
Specifies the file path, log level, and call file for saving logs.
Store logs to a specified file
'''

# Create a logger
Self. logger = logging. getLogger (logger)
Self. logger. setLevel (logging. DEBUG)

# Create a handler for writing log files
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 handler output format
# 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

The following method is used to call a log system:
Copy codeThe Code is as follows:
Logger = logger(logname+'log.txt ', loglevel = 1, logger = "fox"). getlog ()

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.