I. Logging module
Python has a module logging, which allows you to log directly.
# Log level # CRITICAL 50 # ERROR 40 # WARNING 30 # INFO 20 # DEBUG 10
() function with specific parameters:
filename: The specified filename creates the FiledHandler so that the logs are stored in the specified file;
filemode: the way to open the file, use this parameter when filename is specified, default value is "w", can also be specified as "a";
format: Specifies the log display format used by the handler;
datefmt: Specifies the datetime format. , format reference strftime time formatting (below)
level: Sets the logging level of the rootlogger.
stream: create a StreamHandler with the specified stream, you can specify the output to, or a file, default is.
If both filename and stream are listed, the stream argument is ignored.
Formatting information that may be used in the format parameter:
%(name)s |
Logger's name |
%(levelno)s |
Log level in numeric form |
%(levelname)s |
Log level in text form |
%(pathname)s |
The full pathname of the module that calls the log output function, which may not have a |
%(filename)s |
Filename of the module that calls the log output function |
%(module)s |
The name of the module from which the log output function is called |
%(funcName)s |
The name of the function that calls the log output function |
%(lineno)d |
The line of code where the statement calling the log output function is located |
%(created)f |
The current time, expressed as a UNIX-standard floating-point number that represents time. |
%(relativeCreated)d |
The number of milliseconds since the logger was created when outputting log messages. |
%(asctime)s |
The current time in string form. The default format is "2003-07-08 16:49:45,896". Commas followed by milliseconds |
%(thread)d |
Thread ID. may not have |
%(threadName)s |
Thread Name. There may be no |
%(process)d |
Process ID. may not have |
%(message)s |
User Output Messages |
Second, logging module test
1. Print the log to standard output
import logging ('debug message') ('info message') ('warning message')
output result
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\ D:/pyworkpeace/ '/login'
WARNING:root:warning messageProcess finished with exit code 0
You can see that by default Python's logging module prints logs to standard output and only displays logs greater than or equal to the WARNING level. The default format of the log is:
Log Level: Logger Name: User Output Message
2. Enter the log file into the file
import os (filename=((),''),level=) ('this is a message')
Running these three lines of code will bring up a file in the directory where Python is installed with the contents of the file
DEBUG:root:this is a message
DEBUG:root:debug message
3, customized format, output log file
# -*-coding:utf-8-*- import logging def console_out(logFilename): ''''' Output log to file and console ''' # Define a Handler and set a format which output to file ( level=, # Define the level of logs to be output to the file, anything greater than this level is output. format='%(asctime)s %(filename)s : %(levelname)s %(message)s', # Define the format of the output log datefmt='%Y-%m-%d %A %H:%M:%S', # Time filename=logFilename, # log file name filemode='w') # Write mode "w" or "a" # Define a Handler and set a format which output to console console = () # Define the console handler () # Define the handler level formatter = ('%(asctime)s %(filename)s : %(levelname)s %(message)s') # Define the handler format (formatter) # Create an instance ().addHandler(console) # Instantiate adding handlers # Print information # Output log level ('logger debug message') ('logger info message') ('logger warning message') ('logger error message') ('logger critical message') if __name__ == "__main__": console_out('')
Output results:
At this point, a log file is also automatically generated, the log file and the runtime file are in the same folder with the file name
2017-10-23 Monday 11:37:59 hgghf : DEBUG logger debug message
2017-10-23 Monday 11:37:59 hgghf : INFO logger info message
2017-10-23 Monday 11:37:59 hgghf : WARNING logger warning message
2017-10-23 Monday 11:37:59 hgghf : ERROR logger error message
2017-10-23 Monday 11:37:59 hgghf : CRITICAL logger critical message
Modify the output path:
filename='/tmp/', # logfilename
When this line of code in the script is swapped, then the path address of our output log is changed to D:\tmp
The same result can be achieved in the following way
4、Customize the output position
import logging (level=, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='/tmp/', filemode='w') ('debug message') ('info message') ('warning message') ('error message') ('critical message')
Since the run script is placed under D:\pyworkpeace\, the output file is in the tmp folder on the D drive with the following contents:
Mon, 23 Oct 2017 15:00:05 [line:11] DEBUG debug message
Mon, 23 Oct 2017 15:00:05 [line:12] INFO info message
Mon, 23 Oct 2017 15:00:05 [line:13] WARNING warning message
Mon, 23 Oct 2017 15:00:05 [line:14] ERROR error message
Mon, 23 Oct 2017 15:00:05 [line:15] CRITICAL critical message
This is the whole content of this article.