Logging
A module for easy logging tangent thread security
Vim log_test.py
Import logginglogging.basicconfig (filename= ' Log.log ', format= '% (asctime) s-% (name) s-% (LevelName) s-% (module) s : % (message) s ', datefmt= '%y-%m-%d%h:%m:%s%p ', level=logging. Debug) logging.debug (' Debug ') logging.info (' info ') logging.warning (' Warning ') logging.error (' ERROR ') Logging.critical (' critical ') logging.log (' log ')
Run the build log file Log.log
Simulate a script that generates an error log
Import logginglogging.basicconfig (filename= ' Log.log ', format= '% (asctime) s-% (name) s-% (LevelName) s-% (module) s : % (message) s ', datefmt= '%y-%m-%d%h:%m:%s%p ', level=logging. DEBUG) while True: option = raw_input ("Input a digit:") if Option.isdigit (): print "hehe", option Logging.info (' option correct ') else: logging.error (' must input a digit! ')
Executes if the input is a number, write the info log if it is not written as the error log
Ps:level=logging. DEBUG is representative of the minimum record basic if you change to warning you will not record info,debug information even if it is set
It's written in a file.
Display the log to the screen with output to a file
Import Logginglogger = Logging.getlogger ("Simple_example") logger.setlevel (logging. DEBUG) #on Screench = logging. Streamhandler () Ch.setlevel (logging. WARNING) #into Filefh = logging. Filehandler ("Log2.log") fh.setlevel (logging. DEBUG) formatter = logging. Formatter ("% (asctime) s-% (name) s-% (LevelName) s-% (module) s: % (message) S") Ch.setformatter (Formatter) Fh.setformatter (Formatter) logger.addhandler (CH) logger.addhandler (FH) logger.debug ("Debug msg ...") Logger.info ("Info msg ...") Logger.warn ("Warn msg ...") Logger.error ("Error msg ...") Logger.critical ("Critical msg ...")
The screen does not output debug information
Python's logging log module