One, logging module
Function: Manually add log function in logic error-prone location, record error message to file to be wrong
Features: Non-automatic recording, specify the location for manual, specify the error message content
Critical
The default output starts from warning, and can be based on demand
Adjust default Write file is append a mode
Two modes of operation of logging module
The second way is to use the four components of the logging log system, the object's mode operation (tall on the version, high coupling, personality without limit)
1 #first, normal log record, cannot print and write to log simultaneously2 impot Logging3Logging.basicconfig (level=logging. DEBUG,4format='% (asctime) s% (filename) s [line:% (Lineno) d]% (levelname) s% (message) s',5Filename='Log2018.log')#write file with filename configured6Logging.debug ('message')7Logging.info ('message')8Logging.warning ('message')9Logging.error ('message')TenLogging.critical ('message') OneLogging.log (logging. ERROR,'This is a error test') A #custom levels and information: Logging.log (level,mes)
1 #Second, custom configuration log2 #Print to console and write files (create Filehandler and Streamhandler, respectively)3 #Custom splits log content into multiple files at the log level (create multiple files handler)4 #Custom Write file format, content, print format and content (create different formatter formatting for each handler)5 #Customize the log level for each file and print to the screen (different logging.setlevel (* *) for each handler)6 The process is as follows:71, creating a Logger Object Logging.getlogger ()82, specify logger log level Logging.setlevel ()93, create handler logging. Filehandler ()Ten4, specify the log level for handler (must be higher than logger level) Handler.setlevel () One5. Create handler log Format Custom_format =logging. Formatter () A6, specifying the handler log format handler.setformatter (Custom_format) -7, logger logger calls handler processor Obj.addhandler (handler) -8, configuring logging at each log level message Longging.debug (msg) the - " " - Import Logging - # 1, instantiate the object through the logger logger, call Method GetLogger () + log_obj = Logging.getlogger (__name__) - + # 2, set the logger logger that is the global log level (can be omitted, default starting from warning, if you want to set must be higher than its level is info start) A log_obj.setlevel (logging.info) at - # 3. Create a handler handle for each requirement (in this case three, a file that writes the error message, a file written to all logs, a print to the console) - ERROR_FH = logging. Filehandler (' Err.log ', ' a ', encoding= ' UTF-8 ') # Default append, encoding by current default encoding - ALL_FH = logging. Filehandler (' All.log ') - stream_sh = logging. Streamhandler () # Print out no designated destination one says - in # 4, configure the logging level for each handler handle (if you do not set the log level of the default inheritance logger logger) - Error_fh.setlevel (logging. ERROR) to All_fh.setlevel (logging. WARNING) + Stream_sh.setlevel (logging. WARNING) - the # 5, configure the log format for each handler handle (you can configure one, the other one, or each configuration) * Err_format = logging. Formatter ('% (asctime) s-% (filename) s-[line:% (Lineno) d]-% (levelname) s-% (message) s ') $ Stream_format = logging. Formatter ('% (asctime) s-[line:% (Lineno) d]-% (levelname) s-% (message) s ')Panax Notoginseng - # 6, configure the log format for each handler handle the Error_fh.setformatter (Err_format) + All_fh.setformatter (Err_format) A Stream_sh.setformatter (Stream_format) the + # 7, Logger logger call processor handler, combined - Log_obj.addhandler (ERROR_FH) $ Log_obj.addhandler (ALL_FH) $ Log_obj.addhandler (stream_sh) - - # 8. Set log information for each log level the logging.debug (' debug:message2018 ') - logging.info (' info:message2019 ')Wuyi logging.warning (' waring:message2020 ') the logging.error (' error:message2088 ') - logging.critical (' critical:message2099 ')
For more information see: https://www.cnblogs.com/sunxiuwen/articles/9285938.html
Python Learning _ Log module: Logging