Recently want to write their own fragmented code embedded into an application, to take into account the various aspects of the debug, the logging looked at a bit, the use of the note.
- Print the log to the screen
Import logginglogging.debug (U' debug ') logging.info (u ' run ' ) logging.warning (u' warning ')# run display:#
#默认情况下, logging prints warning levels above and prints to the screen.
#日志级别大小关系 CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET (Default), and can be customized.
- Basicconfig function Configuration Log output
#-*-coding:utf-8-*-ImportLogginglogging.basicconfig ( level=logging. DEBUG, Format=u'% (asctime) s file:% (filename) s [% (Lineno) d row] level:% (levelname) s information:% (message) s', Datefmt='%Y%b%d%h:%m:%s,%a', filename='Guapeng.log', FileMode='W') logging.debug (U'Debug') logging.info (U'Information') logging.warning (U'Warning')#The guapeng.log file appears as follows#Jul 16:22:26,mon file: testlog.py [line 10th] Level: Debug Info: Debug#Jul 16:22:26,mon file: testlog.py [line 11th] Level: info: Info#Jul 16:22:26,mon file: testlog.py [line 12th] Level: WARNING info: Warning#basicconfig parameter Description:#filename: Specify the log file name#FileMode: Open mode, ' W ' or ' a '#Format : Specifies the formatting and content of the output, with some format default parameters:#% (levelno) s: Log level values#% (levelname) s: Log level name#% (pathname) s: The path of the current executing program, equivalent to Sys.argv[0]#% (filename) s: The currently executing program name (note that this is different from the log file name)#% (funcName) S: Current execution function#% (Lineno) d: Current line number#% (asctime) s: Current Time#% (thread) d: Thread ID#% (threadname) s: Thread name#% (process) d: Process ID#% (message) s: Log information#datefmt: Specifies the time format, as with Time.strftime ()#level: Sets the print log levels by default to logging. WARNING, the above example is debug, it is larger than debug output to the log file. #Stream: Specifies how the log is output, default output to Sys.stderr (screen), stream is ignored when stream and filename are specified simultaneously
- Print Debug Info to log file, print info to screen
#-*-coding:utf-8-*-ImportLogginglogging.basicconfig ( level=logging. DEBUG, Format=u'% (asctime) s file:% (filename) s [% (Lineno) d row] level:% (levelname) s information:% (message) s', Datefmt='%Y%b%d%h:%m:%s,%a', filename='Guapeng.log', FileMode='W')#defines a streamhandler that prints info-level information to the screenconsole =logging. Streamhandler () console.setlevel (logging.info) Formatter= Logging. Formatter ('% (name) -12s:% (levelname) -8s% (message) s') Console.setformatter (formatter) Log= Logging.getlogger ("'). AddHandler (console) logging.debug (U'Debug') logging.info (U'Information') logging.warning (U'Warning')#The guapeng.log file appears as follows#Jul 16:22:26,mon file: testlog.py [line 10th] Level: Debug Info: Debug#Jul 16:22:26,mon file: testlog.py [line 11th] Level: info: Info#Jul 16:22:26,mon file: testlog.py [line 12th] Level: WARNING info: Warning#The screen is displayed as follows#Root:info Information#root:warning Warning
- With the config file, the log files are stored on a daily basis, and only one week of log is saved.
Python code
#-*-coding:utf-8-*-ImportLoggingImportLogging.config#Import configuration fileLogging.config.fileConfig ("logging.conf") #Create LoggerLoggerinfo = Logging.getlogger ("Timeinfologger") #Test CodeLoggerinfo.debug ("Debug Message") Loggerinfo.info ("Info Message") Loggerinfo.warn ("warn message") Loggerinfo.error ("error Message") loggerinfo.critical ("Critical Message")
logging.conf File Code
#define the Logger module, root is the parent class, must exist, and other classes are defined by themselves. [Loggers]keys=Root,timeinfologger#Define Handler[Handlers]keys=Timeinfohandler#Defining output formats[Formatters]keys=timeinfofmt#--------------------------------------------------#to implement the logger module defined above, it must be a form of [logger_xxxx]#--------------------------------------------------#[logger_xxxx] logger_ module name#Level with Debug, INFO, WARNING, ERROR, CRITICAL#handlers processing class, can have multiple, separated by commas#Qualname Logger Name, the application is obtained through Logging.getlogger. For names that cannot be obtained, the root module is logged. #whether the propagate inherits the log information of the parent class, 0: No 1: Yes[Logger_root]level=infohandlers=Timeinfohandler[logger_timeinfologger]level=infohandlers=timeinfohandlerpropagate=0qualname=Timeinfologger#--------------------------------------------------#Handler#--------------------------------------------------#[Handler_xxxx]#class Handler category name#level log Levels#formatter, the formatter defined above#args handler initialization function arguments[Handler_timeinfohandler]class=Logging.handlers.TimedRotatingFileHandlerlevel=Infoformatter=Timeinfofmtargs=('C:\\users\\tyanf\\desktop\\error.log','Midnight', 1, 6)#--------------------------------------------------#Log Format#--------------------------------------------------#% (Asctime) s year-month-day time-minute-second, millisecond 2013-04-26 20:10:43,745#% (filename) s file name, excluding directories#% (pathname) s directory name, full path#% (funcName) s function name#% (levelname) S-Class aliases#% (Lineno) d line number#% (module) s module name#% (message) s message body#% (name) s log Module name#% (process) d process ID#% (processName) s process name#% (thread) d thread ID#% (threadname) s thread name[Formatter_timeinfofmt]format=% (asctime) s% (levelname) s%(message) sdatefmt=class=logging. Formatter
# The test time is from July 20 to July 27, and the last file library is displayed as follows: # error.log.2015-07-21 # error.log.2015-07-22 # error.log.2015-07-23 # error.log.2015-07-24 # error.log.2015-07-25 # error.log.2015-07-26 # Error.log # where error.log.2015-07-20 because the program set Backupcount to 6, when more than 6 o'clock the earliest delete, and then add a new log file, it has been deleted.
Resources:
http://my.oschina.net/leejun2005/blog/126713
Https://docs.python.org/2/library/logging.handlers.html
Record the Python logging you use