Background
In a new project to join the log function, want to write a, but a chance, through Google found Python built a very powerful log module: logging. A cursory study, here are some of my notes.
Why use Logs
Trace some of the operation information of the program, in order to achieve the time to understand the status of the program, quickly capture program anomalies, timely detection of the purpose of the program error
Logging Module Introduction
From Python2.3 onwards, Python's standard library joins the logging module. The logging module provides a standard information output interface for running applications. The typical implementation of the logging mechanism is to simply write the data to a TXT file. The way to write log files is a common way of playing log , and the logging module provides more, it can output the output information to all class files of the object to go, even TCP and UDP sockets,email server, UNIX syslog system, NT series of event log system, memory buffer and HTTP server And, of course, the "real" file.
Introducing the Logging module:
Import Logging #import logging module# Use logging module: Class CLog: #------------------------------------------------ ---------------------------- def __init__ (self): Self.logger = Logging.getlogger () Filehandler = Logging. Filehandler (log_file_path) Formathandler = logging. Formatter ('% (asctime) s% (levelname) s:% (message) s ') Filehandler.setformatter (Formathandler) Self.logger.addHandler (Filehandler) self.logger.setLevel (logging. NOTSET) #---------------------------------------------------------------------------- def debugmessage ( SELF,MSG): self.logger.debug (msg) pass oclog = CLog ()
A simple log module is defined above, and I want to use this simple code to describe the logging module
Logger
Get an instance of log, this part of the code is well-separated, you can enrich the features of the log instance by adding different handler
Filehandler
Specifies that the output of log is a file, by passing in the file path to specify the output file, we can define the other output for log, such as Streamhandler, and other complex output methods, the file is probably the most common way, other methods need to be explored slowly
Formathandler
FOMARTHANDLER specifies the output format of the Filehandler, for example, I used the following format: ('% (asctime) s% (levelname) s:% (message) s '), the output text format is:
2013-07-25 08:20:01,525 Info:goodbye [127.0.0.1]:60442
For a keyword for format, such as Asctime,levelname, refer to LogRecord attributes official documentation
Level
The logging module defines the priority of 5 log messages
Levelwhen it ' s used
Debugdetailed information, typically of interest only when diagnosing problems.
Infoconfirmation that things is working as expected.
Warningan indication that something unexpected happened, or indicative of some problem in the near future (e.g. ' Disk SPAC e low '). The software is still working as expected.
Errordue to a more serious problem, the software have not been able to perform some function.
Criticala serious error, indicating that the program itself is unable to continue running.
Priority relationship:
DEBUG < INFO < WARNING < ERROR < critcal
The priority of the output information can be determined based on Self.logger.debug (msg), Self.logger.info (msg), and other functions.
SetLevel
The Setlevel function defines the priority of the log instance for handling log information, and if the defined priority is info, all debug information is ignored, output to the output, only the set priority and the priority level is set.