Python logging Module

Source: Internet
Author: User

Introduction to the python logging Module

Use python to write programs, and sometimes 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
Logger = logging. getlogger ('application name ')
Logger. setlevel (logging. Debug)
Console = logging. streamhandler ()
Console. setlevel (logging. info)
Logger. addhandler (console)
Logger. debug ('debug information ')
Logger.info ('useful information ')
Logger. Warning ('Warning information ')
Logger. Error ('error information ')
Logger. 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.


Core log components of Python:

1. "logger ". Each program obtains a logger before outputting information. Logger usually corresponds to the module name of the program,

For example, the graphical interface module of the chat tool can obtain its logger as follows:

logger=logging.getLogger(”chat.gui”)

The core module can be as follows:

logger=logging.getLogger(”chat.kernel”)

Next we can see the purpose of using this naming method. There are also direct use:

Logger = logging. getlogger ()
# Or
Logger = logging. basicconfig (** kwargs)

The default log name is root. logs are recorded on the root layer by using functions such as info and debug of the logging module.

 

2. "formatter ":

Log formatting

The log output format is set through logging. formatter. The initialization parameters are as follows:

Logger = logging. getlogger ("chat. Gui ")
Console = logging. streamhandler ()
Formatter = logging. formatter ("% (asctime) S-% (name) S-% (levelname) S-% (Message) s ")
Console. setformatter (formatter)
Logger. addhandler (console)
# Or
Logging. basicconfig (filename = OS. Path. Join (OS. getcwd (), 'log.txt '),/
Level = logging. Warn, filemode = 'w', format = '% (asctime) S -/
% (Levelname) S: % (Message) s ')
Logging. debug ('debug') # ignored
Logging.info ('info') # ignored
Logging. Warning ('warn ')
Logging. Error ('error ')
# ----- Result
#21:42:15, 592-warning: warn
#21:42:15, 640-error: Error

3. "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:

A. Set the output format. For example, set the output information to include the time and level information:

   logger=logging.getLogger(”chat.gui”)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter(’%(asctime)s - %(levelname)s - %(message)s’)
console.setFormatter(formatter)
logger.addHandler(console)
         

B. Set the 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.

Log objects provide different functions for output of different levels of information, such as Info (), error (), and debug. When logs are written, information smaller than the specified level is ignored. Therefore, you must set this parameter to output the desired log level. Here I set it to notset (the value is 0), that is, to output all information. The default log levels are critical, error, warning, info, debug, and notset. For example, if the output information is critical but the log level is debug, this information will be ignored.

C. Set the filter.

When you call logging. getlogger (), the parameter format is similar to a. B .C ". This format is used to configure the filter. Take a look at this Code:

logger=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)
logger.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:

  • Logging. streamhandler
  • With this handler, You can output information to any file object 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.

  • 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, and 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.

  • 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.

  • 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 M minutes H hours d days w every week (interval = 0 times table Monday) midnight every morning

  • Logging. Handlers. sockethandler
  • Logging. Handlers. datagramhandler
  • Similar to the preceding two handler, log information is sent to the network. The difference is that the former uses the TCP protocol, and the latter uses the UDP protocol. Their constructor is: handler (host, Port) where host is the host name, port is the port name

  • Logging. Handlers. sysloghandler
  • Logging. Handlers. nteventloghandler
  • Logging. Handlers. smtphandler
  • Logging. Handlers. memoryhandler
  • Logging. Handlers. httphandler

Learning Materials: http://onlamp.com/pub/a/python/2005/06/02/logging.html

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.