1. Simply print the log to the screen
Import logginglogging.debug ('This isdebug message') logging.info ( 'this wasinfo message') logging.warning ('This iswarning message ' is warning message
By default, logging prints the log to the screen with a log level of warning;
Log level size relationships are: CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET, and of course you can define the log level yourself.
2. Configure the output format and mode of the log by Logging.basicconfig function
Import Logginglogging.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='Myapp.log', FileMode='W') Logging.debug ('This is debug message') Logging.info ('This is info message') logging.warning ('This is warning message') ./The contents of the Myapp.log file are: Sun, -May the +: -: WuDemo2.py[line: One] DEBUG This isDebug Messagesun, -May the +: -: WuDemo2.py[line: A] INFO This isInfo Messagesun, -May the +: -: WuDemo2.py[line: -] WARNING This isWarning message
Parameters of the Logging.basicconfig function:
FileName: Specify the log file name
FileMode: Same as file function, specify open mode of log file, ' W ' or ' a '
Format: Specifies the formats and contents of the output, format can output a lot of useful information, as in the example above:
% (Levelno) S: Print the value of the log level
% (levelname) S: Print log level name
% (pathname) s: Prints the path of the currently executing program, which is actually sys.argv[0]
% (filename) s: Prints the current name of the executing program
% (funcName) s: Print the current function of the log
% (Lineno) d: Print the current line number of the log
% (asctime) s: Time to print logs
% (thread) d: Print thread ID
% (threadname) s: Print thread name
% (process) d: Print process ID
% (message) s: Print log information
DATEFMT: Specify time format, same as Time.strftime ()
Level: Sets the log levels by default to logging. WARNING
Stream: Specifies the output stream that will log, can specify output to Sys.stderr,sys.stdout or file, default output to Sys.stderr, stream is ignored when stream and filename are specified simultaneously
3. Output logs to both file and screen at the same time
Import Logginglogging.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='Myapp.log', FileMode='W') ################################################################################################# #定义一个StreamHandler , print info-level or higher log information to a standard error and add it to the current log processing object #console=logging. Streamhandler () console.setlevel (logging.info) Formatter= Logging. Formatter ('% (name) -12s:% (levelname) -8s% (message) s') Console.setformatter (formatter) Logging.getlogger ("'). AddHandler (console) ##################################################################################### ########### #logging. Debug ('This is debug message') Logging.info ('This is info message') logging.warning ('This is warning message') on-screen printing: Root:info this isInfo messageroot:warning This iswarning message./The contents of the Myapp.log file are: Sun, -May the +: -: WuDemo2.py[line: One] DEBUG This isDebug Messagesun, -May the +: -: WuDemo2.py[line: A] INFO This isInfo Messagesun, -May the +: -: WuDemo2.py[line: -] WARNING This isWarning message
4.logging Log Rollback
Import Logging fromlogging.handlers Import rotatingfilehandler################################################################# ################################ #定义一个RotatingFileHandler, backup up to 5 log files, maximum 10MRthandler per log file= Rotatingfilehandler ('Myapp.log', maxbytes=Ten*1024x768*1024x768, backupcount=5) Rthandler.setlevel (logging.info) Formatter= Logging. Formatter ('% (name) -12s:% (levelname) -8s% (message) s') Rthandler.setformatter (formatter) Logging.getlogger ("'). AddHandler (Rthandler) ################################################################################### #############
As can be seen from the above example and in this example, logging has a main object of log processing, and other processing methods are added through AddHandler.
Several handle methods of logging are as follows:
logging. Streamhandler: Log output to stream, can be sys.stderr, sys.stdout or file Logging.filehandler: Log output to file log rollback mode, Use Rotatingfilehandler and TimedRotatingFileHandlerlogging.handlers.BaseRotatingHandlerlogging.handlers.RotatingFileHandle in real time Rlogging.handlers.TimedRotatingFileHandlerlogging.handlers.SocketHandler: Remote output log to TCP / ip socketslogging.handlers.DatagramHandler: Remote output log to UDP Socketslogging.handlers.SMTPHandler: Remote output log to e-mail address logging.handlers.SysLogHandler: Log output to Sysloglogging.handlers.NTEventLogHandler: remote output log to Windows NT /2000 /xp Event log Logging.handlers.MemoryHandler: Log output to in-memory development bufferlogging.handlers.HTTPHandler: through get " or " post " remote output to HTTP server
Because Streamhandler and filehandler are common log processing methods, they are included directly in the logging module, while others are included in the Logging.handlers module.
Please refer to the python2.5 manual for the use of the other processing methods described above.
5. Configuring logs via the Logging.config module
#logger. Conf###############################################[loggers]keys=Root,example01,example02[logger_root]level=debughandlers=hand01,hand02[logger_example01]handlers=Hand01,hand02qualname=example01propagate=0[Logger_example02]handlers=Hand01,hand03qualname=example02propagate=0###############################################[handlers]keys=HAND01,HAND02,HAND03[HANDLER_HAND01]class=Streamhandlerlevel=Infoformatter=Form02args=(Sys.stderr,) [HANDLER_HAND02]class=Filehandlerlevel=Debugformatter=Form01args=('Myapp.log','a') [HANDLER_HAND03]class=handlers. Rotatingfilehandlerlevel=Infoformatter=Form02args=('Myapp.log','a',Ten*1024x768*1024x768,5) ###############################################[formatters]keys=Form01,form02[formatter_form01]format=% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s%(message) sdatefmt=%a,%d%b%Y%h:%m:%S[formatter_form02]format=% (name) -12s:% (levelname) -8s%(message) sdatefmt=
In Example 3:
import loggingimport logging.configlogging.config.fileConfig ( " logger.conf " Span style= "COLOR: #000000" >) Logger = Logging.getlogger ( " example01 " ) logger.debug ( " this is debug message " ) logger.info ( " this is info message " ) logger.warning ( " This is warning message ")
In Example 4:
Import Loggingimport logging.configlogging.config.fileConfig ("logger.conf" = Logging.getlogger ("example02") logger.debug ('this is Debug message') logger.info ('This isinfo message' ) ) logger.warning ('This iswarning message')
6.logging is thread-safe
from:http://blog.csdn.net/yatere/article/details/6655445
Python (38): Log Logging Module Learning