Python logging module and pythonlogging Module
Python logging Module 1. Simply print logs to the screen
ImportLogging
Logging. debug ('This is debug message')
Logging.info ('This is info message')
Logging. warning ('This is warning message')
The output result is WARNING: root: This is warning message.
By default, logging prints logs to the screen and the log level is WARNING. The log level relationship is: CRITICAL> ERROR> WARNING> INFO> DEBUG> NOTSET, you can also customize the log level.
2. Use the logging. basicConfig function to configure the log output format and method.
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='myapp.log',
filemode='w')
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
The result is that a file named "myapp. log" is generated in the directory where the program is located. The file content is: Sun, 29 Oct 2017 18:00:04 test2.py [line: 26] DEBUG This is debug message
Sun, 29 Oct 2017 18:00:04 test2.py[line:27] INFO This is info message
Sun, 29 Oct 2017 18:00:04 test2.py[line:28] WARNING This is warning message
If you run the program again, the contents in myapp. log will be overwritten. To add more content to the file, change filemode = 'w' to filemode = 'A '.
Parameters of the logging. basicConfig function:
Filename: Specifies the log file name.
Filemode: Same as the file function. It specifies the log file opening mode, 'w' or 'A'
Format: Specify the output format and content. format can output a lot of useful information, as shown in the preceding example:
% (Levelno) s: print the log-level value
% (Levelname) s: print the Log Level name
% (Pathname) s: print the path of the current execution program, which is actually sys. argv [0]
% (Filename) s: print the name of the currently executed Program
% (FuncName) s: current function for printing logs
% (Lineno) d: print the current line number of the log
% (Asctime) s: log printing time
% (Thread) d: Print thread ID
% (ThreadName) s: print the thread name
% (Process) d: print the process ID
% (Message) s: Print log information
Datefmt: specifies the time format, which is the same as time. strftime ()
Level: Set the log level. The default value is logging. WARNING.
Stream: Specifies the output stream of logs. You can specify the output stream to sys. stderr, sys. stdout or file, which is output to sys by default. stderr. When stream and filename are both specified, stream is ignored.
3. Output logs to files and screens at the same time
import logging
Logger = logging. getLogger ()#CreateLoggerObject
Fh = logging. FileHandler ('Test. Log')#File output stream object
Ch = logging. StreamHandler ()#Screen output stream object
Formatter = logging. Formatter ('% (Asctime) s % (filename) s [line: % (lineno) d] % (levelname) s % (message) s')#Create output format
Fh. setFormatter (formatter)#Format of file output reference Creation
Ch. setFormatter (formatter)#Format created by screen output reference
Logger. addHandler (fh)#File output
Logger. addHandler (ch)#Screen output
Logger. setLevel (logging. DEBUG)#Modify output Log Level
logger.debug('This is debug message')
logger.info('This is info message')
logger.warning('This is warning message')
The result is that a file named "test. log" is generated in the directory where the program is located. The file content is 18:50:09, 717 test2.py [line: 29] DEBUG This is debug message
2017-10-29 18:50:09,717 test2.py[line:30] INFO This is info message
2017-10-29 18:50:09,718 test2.py[line:31] WARNING This is warning message
; The screen displays 18:50:09, 717 test2.py [line: 29] DEBUG This is debug message
2017-10-29 18:50:09,717 test2.py[line:30] INFO This is info message
2017-10-29 18:50:09,718 test2.py[line:31] WARNING This is warning message
When creating an output format, the following parameters are used :.
If the output log level is not modified, the system outputs logs at or above the WARNING log level by default.
The essence of this method is as follows: