Detailed description of the logging module and logging
Many programs need to record logs every day. In addition, the information contained in the logs contains normal program access logs, and may also have errors, warnings, and other information output, the python logging module provides standard log interfaces. You can store logs in various formats.debug()
,info()
,warning()
,error()
AndCritical () 5 levels,
Let's take a look at how to use it.
Simplest use
12345678 |
import logging logging.warning( "user [qiduwangjue] attempted wrong password more than 3 times" ) logging.critical( "server is down" ) # Output WARNING:root:user [qiduwangjue] attempted wrong password more than 3 times CRITICAL:root:server is down |
Let's take a look at the meanings of these log levels.
Level |
When it's used |
DEBUG |
Detailed information, typically of interest only when diagnosing problems. |
INFO |
Confirmation that things are working as expected. |
WARNING |
An 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. |
ERROR |
Due to a more serous problem, the software has not been able to perform some function. |
CRITICAL |
A serious error, indicating that the program itself may be unable to continue running. |
If you want to write logs to a file, it's easy.
123456 |
import logging logging.basicConfig(filename = 'example.log' ,level = logging.INFO) logging.debug( 'This message should go to the log file' ) logging.info( 'So should this' ) logging.warning( 'And this, too' ) |
Level = loggin in the following sentence. INFO indicates to set the log record level to INFO. That is to say, only logs with a higher level than INFO or INFO will be recorded in the file. In this example, the first log will not be recorded. If you want to record the debug log, you can change the log level to DEBUG.
1 |
logging.basicConfig(filename = 'example.log' ,level = logging.INFO) |
I feel that the above log format has forgotten the time, and the log does not know how to perform the time. I will add it below!
123456 |
import logging logging.basicConfig( format = '%(asctime)s %(message)s' , datefmt = '%m/%d/%Y %I:%M:%S %p' ) logging.warning( 'is when this event was logged.' ) # Output 12 / 12 / 2010 11 : 46 : 36 AM is when this event was logged. |
Log format
% (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 floating points of time in UNIX standard |
% (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 |
If you want to print logs on the screen and file logs at the same time, you need to know a little complicated information.
Using the logging module to record logs in Python involves four main types. It is most appropriate to use the general description in the official documentation:
Logger provides interfaces that can be directly used by applications;
Handler sends the (logger created) log records to the appropriate destination output;
Filter provides a device with fineness to determine which log records to output;
Formatter determines the final output format of the log record.
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 ")
Logger. setLevel (lel): Specify the lowest log level. If it is lower than lel, it will be ignored. Debug is the lowest built-in level, and critical is the highest
Logger. addFilter (filt) and Logger. removeFilter (filt): add or delete a specified filter.
Logger. addHandler (hdlr) and Logger. removeHandler (hdlr): add or delete a specified handler.
Logger. debug (), Logger.info (), Logger. warning (), Logger. error (), Logger. critical (): the log level that can be set
Handler
The handler object is responsible for sending relevant information to the specified destination. 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. You can add multiple handler using the addHandler () method.
Handler. setLevel (lel): Specifies the level of information to be processed. Information lower than the lel level will be ignored.
Handler. setFormatter (): select a format for the handler.
Handler. addFilter (filt), Handler. removeFilter (filt): Adds or deletes a filter object.
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 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) 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) 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) 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
12345678910111213141516171819202122232425262728293031 |
import logging #create logger logger = logging.getLogger( 'TEST-LOG' ) logger.setLevel(logging.DEBUG) # create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # create file handler and set level to warning fh = logging.FileHandler( "access.log" ) fh.setLevel(logging.WARNING) # create formatter formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) # add formatter to ch and fh ch.setFormatter(formatter) fh.setFormatter(formatter) # add ch and fh to logger logger.addHandler(ch) logger.addHandler(fh) # 'application' code logger.debug( 'debug message' ) logger.info( 'info message' ) logger.warn( 'warn message' ) logger.error( 'error message' ) logger.critical( 'critical message' ) |
Example of Automatic File Truncation
import loggingfrom logging import handlerslogger = logging.getLogger(__name__)log_file = "timelog.log"#fh = handlers.RotatingFileHandler(filename=log_file,maxBytes=10,backupCount=3)fh = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3)formatter = logging.Formatter('%(asctime)s %(module)s:%(lineno)d %(message)s')fh.setFormatter(formatter)logger.addHandler(fh)logger.warning("test1")logger.warning("test12")logger.warning("test13")
logger.warning("test14")