Log: used to record user behavior or execution of the code
Purpose: "a key" control, debugging, error record, record user behavior
Five levels: the default print only error messages more info
debug: Troubleshooting Information
info: normal Information
warning: a warning
error: Error Messages
critical: serious error message
In two ways:
basicConfig:
Information does not support Chinese, support only write-in file, multiple files can write simultaneously
Create a log object:
Support Chinese, able to write files simultaneously and print in the console also be able to write multiple files simultaneously
Simple configuration:
Python's logging module logs printed by default 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 in> ERROR> WARNING> the INFO> the DEBUG ), the default log format for the log level: Logger name: user output messages.
Import the logging logging.debug ( ' being debugging ' ) logging.info ( ' open file ' ) logging.warning ( ' input error ' ) logging.error ( ' logical error ' ) logging.critical ( ' program collapse ' ) # print: the WARNING: root: input error eRROR: root: logic error CRITICAL: root: the collapse of the program
Configuration parameters: logging.basicConfig () can be changed by logging module specific parameters default behavior of the function, available parameters are: filename: Creating FiledHandler with the specified file name, such logs are stored in the file specified. filemode: File Open, this parameter is specified in the filename, the default value "a" may also be designated as "w". format: log handler specified display format. datefmt: specify the date and time format. level: set the log level rootlogger stream: StreamHandler created with the specified stream. Can specify the output to sys.stderr, sys.stdout or file (F = Open ( 'the test.log', 'W')), default sys.stderr. If both lists the filename and stream two parameters, the stream parameter is ignored. the format parameter string format may be used: % (name) is the name of S Logger % (levelno) S log level digital form % (levelname) S log level text form % (pathname) S log output function calling module the full path name, may not have % S call log output function module (filename) filename % module name (module) s call log output function % (FuncName) S calls the function log output function name % line (lineno) d log output function call statement where % (Created) F the current time, represented by a standard floating point number representing time UNIX % (relativeCreated) D since the number of milliseconds to create Logger output log information % (asctime) s current time string. The default format is "2003-07-0816: 49: 45,896 ." Milliseconds after the comma % (Thread) D thread ID. It may not have % (threadName) S thread name. You may not have % d process ID (process). May not % (message) s user output message
basicConfig: flexible configuration log level, the log format, output location
Import the logging Logger = logging.FileHandler (filename = ' log_in ' , MODE = ' A ' , encoding = ' UTF-. 8 ' ) # Create a file operator for writing the log file logger1 logging.FileHandler = (filename = ' log_in_in ' , MODE = ' A ' , encoding = ' UTF-. 8 ' ) logging.basicConfig ( the format = ( ' % (the asctime) S - [Line:% (lineno) D] -% (levelname) S -% (Module1 ) S:% (Message) S ' ), datefmt = 'Y-M-% D%%% H:% M:% S% P ' , handlers = [Logger, logger1], # can be written to a plurality of files operators, different folders Level = logging.info ) logging.debug ( ' Logger Debug the Message ' ) # only supports Chinese logging.info ( ' Logger info the Message ' ) logging.warning ( ' Logger warning the Message ' ) logging.error ( ' Logger error the Message ' ) logging.critical ( ' Logger Critical the Message ' )
Cutting logs
import time import logging from logging import handlers sh = logging.StreamHandler() rh = handlers.RotatingFileHandler('myapp.log', maxBytes=1024,backupCount=5) fh = handlers.TimedRotatingFileHandler(filename='x2.log', when='s', interval=5, encoding='utf-8') logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S %p', handlers=[fh,sh,rh], level=logging.ERROR ) for i in range(1,100000): time.sleep(1) logging.error('KeyboardInterrupt error %s'%str(i))
Create a log object:
Import the logging Logger = logging.getLogger () # create log object FH = logging.FileHandler (filename = ' log_in11 ' , MODE = ' A ' , encoding = ' UTF-. 8 ' ) # Create a handler operator for writing log file # FH1 logging.FileHandler = (filename = 'log_in111111', MODE = 'A', encoding = 'UTF-. 8') # write multiple file Formatter logging.Formatter = ( ' % (the asctime) S -% ( Module1) S -% (levelname) S - [Line:% (lineno) D:% (Message) S] ' ,) # output format, default time format% Y-% m-% d% H :% M:% S #logger.setLevel (logging.INFO) # output level, there is no limit log objects, which specify which display fh.setLevel (logging.DEBUG) # output level, designated handler operator, minimum write WARNING, WARNING following info, debug not written fh.setFormatter (Formatter) # Handler associated output format # fh1.setFormatter (Formatter) # write multiple file logger.addHandler (FH) # log object association operator. Written to the log file. Adds the specified handler to this logger. Next a FileHandler, can select only one output, while if you want to output console, method handler create StreamHandler operator # logger.addHandler (FH1) writing the plurality of files # SH = logging.StreamHandler () # Create a handler operator for output to the console sh.setLevel (logging.error) #Output level, the operator specifies handler, minimum write WARNING, or more WARNING info, debug not written sh.setFormatter (Formatter) # handler bind output format logger.addHandler (SH) # output to the console logger.debug ( ' is debugging ' ) logger.info ( ' open file ' ) logger.warning ( ' input error ' ) logger.error ( ' logical error ' ) logger.critical ( ' program collapse ' )
logging library provides a number of components: Logger , Handler , the Filter , Formatter . Logger object provides application program interfaces can be used directly, Handler send logs to the appropriate destination, the Filter provides a method of filtering log information, the Formatter specified log display format. In addition, you can: Logger.setLevel (logging.Debug) set the level, of course, you can also fh.setLevel (logging.Debug) single file stream is set to a level, but set a single-file stream level, only can display wARNING minimum level of information, even though your level is set to info or debug, it will only show information and more warning level