Python Log Module (article 1st)

Source: Internet
Author: User
[Reprint] the original address http://blog.sina.com.cn/s/blog_681315fa0100imtk.html

If you use python to write a relatively large program, you will certainly use the log system. Especially for dynamic languages like python, many errors can only be found during running. A good log system is very important to Python programs. The simplest solution is to directly use print to output the running information. However, this is too simple and there is no grading function. If you want to remove the running information for debugging during release, you have to find all the print statements for modification. In addition, print can only be output to the console. If you want to output data to a file or send it to other places by email, a print statement cannot be solved.
You can solve the above problems by using the python log system.
First, let's take a look at this example:
Import Logging
Log = logging. getlogger ('application name ')
Console = logging. streamhandler ()
Console. setlevel (logging. info)
Log. addhandler (console)
Log. debug ('debug information ')
Log.info ('useful information ')
Log. Warning ('Warning information ')
Log. Error ('error information ')
Log. Critical ('critical error information ')
The above code outputs five error messages on the console. There are five levels from low to high, from debug to critical. In addition, we also specify the program output level. Only information above the info level will be output.
This is the most commonly used log system method. There are two concepts in this code that can help us further use the python log system:
1.      "Logger ". Each program obtains a logger before outputting information. Logger usually corresponds to the program module name. For example, the graphic interface module of the chat tool can obtain its logger as follows:
Log = logging. getlogger ("chat. Gui ")
The core module can be as follows:
Log = logging. getlogger ("chat. kernel ")
Next we can see the purpose of using this naming method.
2.      "Handler ". Used for processing program output. Python log systems can be used by handler. Some handler can output the information to the console, some logger can output the information to the file, and some handler can send the information to the network. If you think it is not enough, you can write your own handler.
All handler support three operations:
1.    Set the output format. For example, set the output information to include the time and level information:
Log = logging. getlogger ("chat. Gui ")
Console = logging. streamhandler ()
Console. setlevel (logging. info)
Formatter = logging. formatter ('% (asctime) S % (levelname) S % (Message) s ')
Console. setformatter (formatter)
Log. addhandler (console)
If you are familiar with python, you may find that the parameters of logging. formatter are only commonly used "%" strings in Python. It uses "% (name) s" to represent placeholders. The following is a complete table showing the information that the log system can output:

% (Name) S

Logger name

% (Levelno) S

Log Level in digital form

% (Levelname) S

Log Level in text format

% (Pathname) S

The complete path name of the module that calls the log output function.

% (Filename) S

File Name of the module that calls the log output function

% (Module) S

The name of the module that calls the log output function.

% (Funcname) S

Name of the function that calls the log output function

% (Lineno) d

The code line of the statement that calls the log output function

% (Created) f

Current Time, represented by UNIX standard floating point of time

% (Relativecreated) d

Number of milliseconds since logger was created when log information is output

% (Asctime) S

The current time in string format. The default format is "16:49:45, 896 ". The comma is followed by a millisecond

% (Thread) d

Thread ID. Not possible

% (Threadname) S

Thread name. Not possible

% (Process) d

Process ID. Not possible

% (Message) S

User Output Message

2.    Set output level
We have demonstrated how to set the output level. In addition to the five built-in Python levels, we can also customize the output level.
Todo sub-definition output level
3.    Set Filter
Careful friends will surely find that the parameter format when calling logging. getlogger () is similar to "A. B .C ". This format is used to configure the filter. Take a look at this Code:
Log = logging. getlogger ("chat. Gui. Statistic ")
Console = logging. streamhandler ()
Console. setlevel (logging. info)
Formatter = logging. formatter ('% (asctime) S % (levelname) S % (Message) s ')
Console. setformatter (formatter)
Filter = logging. Filter ("chat. Gui ")
Console. addfilter (filter)
Log. addhandler (console)
Unlike the previous one, we added a filter on handler. Now, when we output the log information, it will be processed by the filter. The filter named "A. B" only allows logger output information with the prefix "A. B. You can add multiple filters. As long as one filter is rejected, log information will not be output. You can also add a filter to logger.
Each logger can append multiple handler. Next we will introduce some commonly used handler:
1)      Logging. streamhandler
With this handler, You can output information to any file object (fileobject) similar to SYS. stdout or SYS. stderr. Its constructor is:
Streamhandler ([STRM])
The STRM parameter is a file object. The default value is sys. stderr.
2)  2. Logging. filehandler
Similar to streamhandler, it is used to output log information to a file. But filehandler will help you open this file. Its constructor is:
Filehandler (filename [, mode])
Filename is the file name. A file name must be specified.
Mode is the file opening method. See the usage of the python built-in function open. The default value is 'A', which is added to the end of the file.
3)  3. Logging. Handlers. rotatingfilehandler
This handler is similar to the filehandler above, but it can manage the file size. When the file size reaches a certain value, it will automatically rename the current log file, and then create a new log file with the same name to continue output. For example, the log file is chat. log. When chat. log reaches the specified size, rotatingfilehandler automatically changes the file to chat. log.1. However, if chat. log.1 already exists, it will first rename chat. log.1 to chat. log.2... At last, re-create chat. log and continue to output log information. Its constructor is:
Rotatingfilehandler (filename [, mode [, maxbytes [, backupcount])
The filename and mode parameters are the same as those of filehandler.
Maxbytes is used to specify the maximum file size of a log file. If maxbytes is 0, it means that the log file can be infinitely large, and the renaming process described above will not happen.
Backupcount is used to specify the number of backup files to be retained. For example, if 2 is specified, the original chat. log.2 will not be renamed but will be deleted when the renaming process described above occurs.
4)  4. Logging. Handlers. timedrotatingfilehandler
This handler is similar to rotatingfilehandler. However, it does not determine when to re-create a log file by determining the file size, but automatically creates a new log file at a certain interval. The rename process is similar to rotatingfilehandler, but the new file is not appended with a number, but the current time. Its constructor is:
Timedrotatingfilehandler(Filename [, when [, interval [, backupcount])
The filename parameter and backupcount parameter have the same meaning as rotatingfilehandler.
Interval is the time interval.
The when parameter is a string. The unit of the time interval, case insensitive. It has the following values:
S seconds
MB
H hour
D days
W every week (interval = 0 times table Monday)
Midnight every morning
5)  5. Logging. Handlers. sockethandler
6)  […]

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.