python module --logging

1. Simple application of logging module

1 import logging
2 
3 logging.debug('debug message')
4 logging.info('ingo message')
5 logging.warning('warning message')
6 logging.error('error message')
7 logging.critical('critical message')

The output is:

WARNING:root:warning message
ERROR:root:error message
CRITICAL:root:critical message

It can be seen that by default, python's logging module prints logs to the standard output, and only displays logs greater than or equal to warning level, which means that the default log level is set to warning (log level critical > error > warning > info > debug > notset ), the default log format is log level: logger name: user output message.

2. Flexible configuration of log level, log format, and output location

1  import logging
 2  logging.basicConfig(
 3      level = logging.DEBUG,     #Modified the level from the original default warning to debug 
4      filename= " logger.log " ,      #Not displayed outside, only displayed internally 5 filemode = ' w ' ,
 6 # format="%(asctime)s %(filename)s[line:%(lineno)d] %(message)s", 7 )
 8 9 logging.debug( ' debug message ' )
 10 logging.info( ' ingo message ' )
 11
     
  
 logging.warning('warning message')
12 logging.error('error message')
13 logging.critical('critical message')

output

DEBUG:root:debug message 
INFO:root:ingo message
WARNING:root:warning message
ERROR:root:error message
CRITICAL:root:critical message
can be seen in the logging.basicconfig() function, you can change the default behavior of the logging module through specific parameters , the available parameters are filename: create a filed handler with the specified file name, so that the log will be stored in the specified file.

filemode: The file is opened with this parameter. This parameter is used when filename is specified. The default value is "a" and can also be specified as "w".
format: Specify the log display format used by the handler

datefmt: Specify the date format.
level: Sets the rootlogger's log level.
stream: Create sys.stderr with the specified stream. If both filename and stream parameters are listed,
the stream parameter will be ignored.

Format strings that may be used in the format parameter:
%(name)s The name of the Logger
%(levelno)s The log level in numeric form
%(levelname)s The log level in text form
%(pathname)s Full path name, possibly no
%(filename)s Filename of the module calling the log output function
%(module)s Module name calling the log output function
%(funcName)s Function name calling the log output function
%(lineno)d call The line of code where the statement of the log output function is located
%(created)f The current time, expressed as a floating point number representing time in UNIX standard
%(relativeCreated)d When outputting log information, the number of milliseconds since the Logger was created
%(asctime)s The current time as a string, the default format is "2003-02-03 12:34:10,234". Hours and milliseconds after the comma
%(thread)d county ID, may not have
%(process)d process ID, may not have
%(threadName)s thread name, may not have
%(message)s user output message

 

【to be continued】

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325950553&siteId=291194637