Day6 subprocess module, logging module, day6subprocess
Logging Module
Many programs need to record logs, and the information contained in the logs has 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:
--- Restore content end ---
Logging Module
Many programs need to record logs, and the information contained in the logs has 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:
% (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 |
Add a date to the log file as follows:
Import logging
"" BasicConfig () is used to set the date format, level is the date level, and datafmt is the date format """
Logging. basicConfig (format = '% (asctime) s % (message) s', datefmt =' % m/% d/% Y % I: % M: % s % P ', level = logging. DEBUG)
Logging. warning ('is when this event was logged .')
Logging. basicConfig (format = "% (asctime) s % (message) s", datefmt = "% m/% d/% Y % I: % M: % S % p ", level = logging. DEBUG)
Logging. debug ("I love python ")
The running result is as follows:
05/30/2017 06:40:33 AM is when this event was logged.
05/30/2017 06:40:33 am I love python
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:
(1)Logger provides interfaces that can be directly used by applications.;
(2)Handler sends the (logger created) log records to the appropriate destination output;
(3)Filter provides a device with fineness to determine which log records to output;
(4)Formatter determines the final output format of log records.