Python--logging Module

Source: Internet
Author: User

First, Introduction

Python's logging module provides a common logging system that can be easily used by third-party modules or applications. This module provides different log levels and can record logs in different ways, such as files, HTTP get/post,smtp,socket, etc., and can even implement specific logging methods on their own.

The logging module is the same as the log4j mechanism, but the specific implementation details are different. module provides logger,handler,filter,formatter.

Logger: Provides a log interface for use in code. There are two types of logger for the longest operation: configuring and sending log messages. The Logger object can be obtained by Logging.getlogger (name), and if you do not specify name, the root object is returned, and multiple times using the same name calls the GetLogger method to return the same logger object.

Handler: sends a log record to the appropriate destination (destination), such as a file, socket, etc. A Logger object can add 0 to multiple handler through the AddHandler method, and each handler can also define different log levels for the log rating filtering display.

Filter: Provides an elegant way to determine whether a log record is sent to handler.

Formatter: Specifies the specific format of the logging output. The formatter method requires two parameters: the format string of the message and the date string, both of which are optional.

Similar to log4j, calls to Logger,handler and log messages can have specific log levels (level), only at levels greater than logger and handler for log messages.

Logging logs can be divided into 5 levels: Debug (), info (), warning (), error (), and Critical ().

Second, usage

2.1 Initial Knowledge

Logging 5 Log level debug lowest, critical high, the lower the log level output information is more detailed.

Import Logginglogging.debug ("This was debug info") Logging.info ("This is Info infomation") logging.warning ("User [Bigberg ] attempted wrong password more than 3 times ") Logging.error (" Here is the error info ") logging.critical (" Server is Down ") #输出日志WA RNING:root:user [Bigberg] attempted wrong password more than 3 TimesERROR:root:This was error infoCRITICAL:root:server is D Own

You can see that the default log output level in Python is warning (), which is output below the warning () level.

2.2 Log levels

2.3 Writing a log to a file

Import Logginglogging.basicconfig (filename= ". /log/test.log ", level=logging. DEBUG) # Defines the file and log level Logging.debug ("This is Debug Info") Logging.info ("This is Info infomation") logging.warning ("User [ Bigberg] attempted wrong password more than 3 times ") Logging.error (" Here is the error info ") logging.critical (" Server is Down ")

You can see that the log has been written to the Test.log file, we choose the day level is debug, so all the days information will be saved to the log file. If you choose to select the warning level, the following level log information is not written. And the log is written in an appended form to the Test.log file.

2.4 Add time to log information

Import Logginglogging.basicconfig (filename= ". /log/test.log ",                    level=logging.info,                    format= '% (asctime) s% (levelname) s:% (message) s ',                    datefmt= '%y/%m/ %d%h:%m:%s ') Logging.debug ("This was debug info") Logging.info ("This is Info infomation") logging.warning ("User [Bigberg] Attempted wrong password more than 3 times ") Logging.error (" This is the error info ") logging.critical (" Server is Down ") #% (ascti Me) s       set time format #% (levelname) s    log level #% (message) s      

2.5 format of the format log

We can add the functions we need in format according to our own needs, such as adding the wrong file name and the number of rows in the log output.

Three, complex log output

Python uses the logging module to log logs involving four main classes, which are best used in the official documentation:

    • Logger provides an interface that the application can use directly;
    • Handler sends the log record (created by logger) to the appropriate destination output;
    • Filter provides a fine-grained device to determine which log record to output;
    • Formatter determines the final output format of the log record.

3.1 Logger

    1. Each program obtains a logger before outputting the information. Logger usually corresponds to the module name of the program, such as a payment module log = Logging.getlogger ("App.pay")
    2. Logger.setlevel (LEL): Specifies the lowest day level, and levels below Lel are ignored. Debug is the lowest built-in level, critical is the highest
    3. Logger.addfilter (filt), Logger.removefilter (filt): Add or remove the specified filter
    4. Logger.addhandler (HDLR), Logger.removehandler (HDLR): Add or remove the specified handler
    5. Logger.debug (), Logger.info (), logger.warning (), Logger.error (), logger.critical (): Log levels you can set

3.2 Handler

The handler object is responsible for sending relevant information to the specified destination. Python's log system can be used in a variety of handler. Some handler can output information to the console, some logger can output the information to a file, and some handler can send information to the network. If you feel that it is not enough, you can write your own handler. You can add more than one handler by using the AddHandler () method.

Multiple handler can be attached to each logger:

1) logging. Streamhandler: Screen Output

Using this handler, you can output information to any file object, such as Sys.stdout or Sys.stderr. Its constructor is:
Streamhandler ([STRM])
Where the STRM parameter is a file object. Default is Sys.stderr

2) logging. Filehandler: Writing Files
Similar to Streamhandler, used to output log information to a file. But Filehandler will open this file for you. Its constructor is:
Filehandler (Filename[,mode])
FileName is a file name and you must specify a file name.
Mode is how the file is opened. See the use of the Python built-in function open (). The default is ' a ', which is added to the end of the file.

Also output logs to screen and file separately:

import Logginglogger = Logging.getlogger ("Test-log") Logger.setLevel ( Logging. DEBUG) # Streamhandlerch = logging. Streamhandler () Ch.setlevel (logging. WARNING) # You can set the specific day level # FILEHANDLERFH = logging in handler. Filehandler (".. /log/handler.log ", encoding=" Utf-8 ") fh.setlevel (logging. ERROR) # Formatterch_formatter = logging. Formatter ("% (asctime) s% (filename) s-% (Lineno) d% (levelname) s:% (message) S", datefmt= '%y/%m/ %d%h:%m:%s ') Fh_formatter = logging. Formatter ('% (asctime) s% (filename) s-% (Lineno) d% (levelname) s:% (message) s ', datefmt= '%y/%m /%d%h:%m:%s ') ch.setformatter (ch_formatter) fh.setformatter (fh_formatter) logger.addhandler (CH) logger.addHandler ( FH) Logger.debug ("This was debug info") Logger.info ("This is Info infomation") logger.warning ("User [Bigberg] attempted Wrong password more than 3 times ") Logger.error (" This is the error info ") logger.critical (" Server is Down ") 

Screen output:

File output:

3.3 Log file truncation

1. Truncate the log by file size

Logging.handler.RotatingFileHandler

This handler is similar to the filehandler above, but it can manage the file size. When the file reaches a certain size, it automatically renames the current log file and then creates a new log file with the same name to continue the output. For example, the log file is Chat.log. When the chat.log reaches the specified size, Rotatingfilehandler automatically renames the file to Chat.log.1. However, if chat.log.1 already exists, it will first rename chat.log.1 to chat.log.2 ... Finally, re-create the Chat.log and continue to output the log information. Its constructor is:
Rotatingfilehandler (filename[, mode[, maxbytes[, Backupcount]])
where filename and mode two parameters are the same as Filehandler.
The maxbytes is used to specify the maximum file size for the log file. If MaxBytes is 0, it means that the log file can be infinitely large, and the renaming process described above does not occur.
The backupcount is used to specify the number of reserved backup files. For example, if you specify 2, when the renaming process described above occurs, the original chat.log.2 is not renamed, but is deleted.

Import loggingfrom Logging Import Handlerslogger = Logging.getlogger ("Test-log") Log_file = "... /log/rotaing.log "# set log file size truncated at 100B # keep up to 3 log files FH = handlers. Rotatingfilehandler (Filename=log_file, maxbytes=100, backupcount=3, encoding= "Utf-8") formatter = logging. Formatter ('% (asctime) s% (filename) s-% (Lineno) d% (levelname) s:% (message) s ',                              datefmt= '%y/%m/%d%h:%m:%s ') Fh.setformatter (Formatter) logger.addhandler (FH) logger.warning ("Test1") logger.warning ("Test2") logger.warning (" Test3 ") logger.warning (" Test4 ") logger.warning (" Test5 ")

Log file for output:

2. Truncation by date

Logging.handlers.TimedRotatingFileHandler

This handler is similar to Rotatingfilehandler, however, it does not determine when to recreate the log file by judging the file size, but instead automatically creates a new log file at a time interval. The process of renaming is similar to Rotatingfilehandler, but the new file is not an appended number but the current time. Its constructor is:
Timedrotatingfilehandler (filename [, when [, interval [, Backupcount]])
where the filename parameter and the Backupcount parameter and the Rotatingfilehandler have the same meaning. The
interval is a time interval. The
when parameter is a string. A unit that represents a time interval, not case-sensitive. It has the following values:
S seconds
M minutes
H hours
D days
W per week (Interval==0 on behalf of Monday)
Midnight early morning

Import timeimport loggingfrom logging Import Handlerslogger = Logging.getlogger ("Test-log") Log_file = "... /log/time_rotating.log "# s stands for seconds # interval interval # keep the file 3 fh = handlers. Timedrotatingfilehandler (Filename=log_file, when= "S", Interval=3, Backupcount=3, encoding= "Utf-8") Fh.setLevel ( Logging. WARNING) formatter = logging. Formatter ('% (asctime) s% (filename) s-% (Lineno) d% (levelname) s:% (message) s ', datefmt= '%y/%m/%d%h:%m:%s ') Fh.setformatter (Formatter) logger.addhandler (FH) logger.warning ("Test1") logger.warning ("test12") Time.sleep (3) Logger.warning ("test13") logger.warning ("test14")

Log file output:

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.