Background: I added the log function to a new project and wanted to write it myself. However, by chance, I found that Python has a very powerful log module built in on google.
I added the log function to a new project and wanted to write it myself. but by chance, I found that Python has a very powerful log module: logging. After a rough research, the following are some of my experiences and notes.
Why use logs?
Tracking program running information to keep abreast of program running conditions, quickly capture program exceptions, and detect program errors in a timely manner
Introduction to the logging module
Since Python2.3, The Python Standard Library has been added to the logging module. the logging module provides a standard information output interface for running applications. A typical logging mechanism is to simply write the data to be output to a txt file. the log file writing method is a common method of logging, while the logging module provides more, which can output the output information to the objects of all class files, even TCP and UDP sockets, email server, Unix syslog system, NT series event log system, memory buffer and HTTP server, and of course there is a "real" file.
Introduce the logging module:
Import logging # import logging module # Use the 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. I want to use this simple code to describe the logging module.
Logger
Get an instance of log. this part of code separation is well done. you can enrich the features of log instances by adding different handler.
FileHandler
Specify the Log output end as a file, and specify the output file by passing in the file path strength. we can define other output ports for Log, such as StreamHandler and other complex output modes, files may be the most commonly used method. Other methods need to be explored slowly.
FormatHandler
FomartHandler specifies the output format of FileHandler. for example, I use 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 keyword about format, such as asctime and levelname, refer to the official LogRecord attributes documentation.
Level
The Logging module defines the priority of five types of log information.
LevelWhen it's used
DEBUGDetailed information, typically of interest only when diagnosing problems.
INFOConfirmation that things are working as expected.
WARNINGAn indication that something unexpected happened, or indicative of some problem in the near future (e.g. 'disk space low'). The software is still working as expected.
ERRORDue to a more serious problem, the software has not been able to perform some function.
CRITICALA serious error, indicating that the program itself may be unable to continue running.
Priority relationship:
DEBUG <INFO <WARNING <ERROR <CRITCAL
The priority of output information can be determined by self. logger. debug (msg), self.logger.info (msg), and other functions.
SetLevel
The SetLevel function defines the priority of Log instances to process log information. if the priority is info, all debug information is ignored and is not output to the output end, enter only the information with the specified priority and higher priority.