Python logging module is built standard modules, mainly for outputting the operation log, the log output level can be set, save the log path, the log file rollback the like;
2 logging module
2.1 Basic use
First install third-party module logging
pip logging
The logging basic settings, and then outputs the log in the console
import logging
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
WARNING:root:warning message
ERROR:root:error message
CRITICAL:root:critical message
Visible default python case where the logging module logs printed to the standard output, and only shows a greater than or equal WARNING level log, indicating that the default log level WARNING (log level Level CRITICAL> ERROR> WARNING> INFO> DEBUG> NOTSET),
The default log format is: log level: Logger Name: User output messages .
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='/tmp/test.log', fileMode = ' a ' ) logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')
Visible logging module may change the default behavior of the specific parameters logging.basicConfig () function, the available parameters - filename: Creating FiledHandler with the specified file name (will explain in detail the concepts behind the handler), so logs are stored in the file specified. - - the fileMode: File Open, this parameter is specified in the filename, the default value "a" may also be designated as "w". - format: Specify the log handler using the display format. - - datefmt: specify the date and time format. - Level: Set the log level
2.3 handlers use
log handler determines your position eventually, your log information is output directly to the terminal (StreamHandler) or saved to a log file (FileHandler)
A typical logging step is such that:
- Create a logger
- Create a handler (streamhandler and fileHandler)
- Defined formatter
- Adding to the handler formatter
- Adding to the logger handler
import logging logger = logging.getLogger() Logger.setLevel (Level = logging.info)
# establish a FileHandler handler = logging.FileHandler("log.txt",encoding='utf8') handler.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter)
# Create a streamHandler console = logging.StreamHandler() console.setLevel(logging.INFO) logger.addHandler(handler) logger.addHandler(console) logger.info("Start print log") logger.debug("Do something") logger.warning("Something maybe fail.") logger.info("Finish")
Appendix: Log files package
# Package log DEF get_log (Self): # Create a Logger Logger = logging.getLogger () logger.setLevel(logging.INFO) # Set the log storage path, the log file name # Get the local time, into a format set RQ = The time.strftime ( ' % D the Y% m%%% H M ' , time.localtime (the time.time ())) # passage through the log storage all_log_path = ' logs / All_Logs / ' error_log_path = ' logs / Error_Logs / ' # set the log file name all_log_name all_log_path = RQ + + ' .log ' error_log_name = RQ + + error_log_path ' .log ' # Create a handler # Create a log handler writes all FH = logging.FileHandler (all_log_name, encoding = ' utf8 ' ) fh.setLevel(logging.INFO) # Create a handler written to the error log EH = logging.FileHandler (error_log_name, encoding = " utf8 " ) eh.setLevel(logging.ERROR) # Create a handler to the console CH = logging.StreamHandler () ch.setLevel(logging.INFO) # Define the log output format # time - display format of the content log - log name - log level all_log_formatter logging.Formatter = ( ' % (the asctime) S -% (name) S -% (levelname) S -% (Message) S ' ) # time - name of the log - log level - file name - function line number - content error error_log_formatter = logging.Formatter ( ' % (the asctime) S -% (name) S -% (levelname) S -% ( Module1) S -% (lineno) S -% (Message) S ' ) # will be added to the output format defined Handler fh.setFormatter (all_log_formatter) ch.setFormatter(all_log_formatter) eh.setFormatter(error_log_formatter) # 给logs添加trades logger.addHandler (fh) logger.addHandler(eh) logger.addHandler(ch) return logger