Python's logging module one, simple to print the log to the screen
Import Logging
Logging.debug (' This is debug message ')
Logging.info (' This is Info message ')
Logging.warning (' This is warning message ')
The output is WARNING:root:This is warning message.
By default, logging prints the log to the screen with a log level of WARNING and a log-level size relationship: CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET, Of course, you can define the log level yourself.
Second, through the Logging.basicconfig function to the output format of the log and the way to do the relevant configuration
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 ')
As a result, a file named "Myapp.log" is generated in the directory where the program is located, with the contents of: Sun, Oct 18:00:04 test2.py[line:26] Debug this is debug message
Sun, Oct 18:00:04 test2.py[line:27] Info this is info message
Sun, Oct 18:00:04 test2.py[line:28] WARNING This is WARNING message
If you run the change program again, the contents of the file Myapp.log will be overwritten. If you want to continue adding content to the file, you will need to change filemode= ' W ' to filemode= ' a '.
Parameters of the Logging.basicconfig function:
FileName: Specify the log file name
FileMode: Same as file function, specify open mode of log file, ' W ' or ' a '
Format: Specifies the formats and contents of the output, format can output a lot of useful information, as in the example above:
% (Levelno) S: Print the value of the log level
% (levelname) S: Print log level name
% (pathname) s: Prints the path of the currently executing program, which is actually sys.argv[0]
% (filename) s: Prints the current name of the executing program
% (funcName) s: Print the current function of the log
% (Lineno) d: Print the current line number of the log
% (asctime) s: Time to print logs
% (thread) d: Print thread ID
% (threadname) s: Print thread name
% (process) d: Print process ID
% (message) s: Print log information
DATEFMT: Specify time format, same as Time.strftime ()
Level: Sets the log levels by default to logging. WARNING
Stream: Specifies the output stream that will log, can specify output to Sys.stderr,sys.stdout or file, default output to Sys.stderr, stream is ignored when stream and filename are specified simultaneously
Three, the log output to the file and screen simultaneously
Logging
# Create a Logger Object
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
# format for file output reference creation
# screen output reference the format created
# for file output
# Perform screen output
# modifying the output log level
Logger.debug (' This is debug message ')
Logger.info (' This is Info message ')
Logger.warning (' This is warning message ')
As a result, a file named "Test.log" is generated under the directory where the program is located and the contents of the file are 2017-10-29 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
Screen display 2017-10-29 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 the output format, the parameters of the Logging.basicconfig function are used:.
If you do not modify the output log level, the default is the output warning log level above and its own log content.
The method has the following substantive ideas:
Python's logging module