Output logs directly to the screen
ImportLogginglogging.basicconfig ( level=logging.info, Format='% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s', Datefmt='%a,%d%b%Y%h:%m:%s',) Logging.debug ('This is debug message') Logging.info ('This is info message') logging.warning ('This is warn message')
Output screen results:
Sat, June 10:17:25 Log_m.py[line:16] Info this is info message
Sat, June 10:17:25 Log_m.py[line:17] WARNING This is warn message
Definition to print the log to a file
ImportLogginglogging.basicconfig ( level=logging.info, 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.info ('This is info message')
Output file Results:
Sat, June 10:30:51 Log_m.py[line:18] Info this is info message
Sat, June 10:30:51 Log_m.py[line:19] WARNING This is warn message
The meaning of various parameters of Basicconfig
logging.basicconfig function Parameters: FileName: Specifies the log file name FileMode: The same as the file function, specify the open mode of the log files,'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 current execution program, which is actually sys.argv[0]%(filename) S: Print 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 the log%(thread) d: Print thread ID%(threadname) s: Print thread name%(process) d: Print process ID%(message) s: Print log information datefmt: Specifies the time format, same as Time.strftime () level: Sets the log levels by default to logging. Warningstream: 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
Log advanced features, log components include: Loggers, Handlers,filters,formatters.
The Logger object plays a triple role. First, it exposes several methods to apply so that the application can write log at run time. Second, the Logger object determines how the log information is handled (the default filtering function), depending on the severity of the log information or on the filter object. Finally, Logger is also responsible for transmitting the log information to the relevant loghandlers.
The handler object is responsible for assigning the appropriate log information (based on the severity of the log information) to the destination specified by handler. The Logger object can add 0 or more handler objects to itself by using the AddHandler () method. A common scenario is that an application might want to send all the log information to a log file, All log messages above the error level are sent to stdout, and all critical log messages are sent via email. This scenario requires three different handler handles, each handler responsible for sending a specific log message to a specific location.
Filter: Fine-grained, select which log output
Define a Streamhandler, print the info level or higher log information to a standard error, and add it to the current log processing object
ImportLoggingImportOslogging.basicconfig ( level=logging.info, 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') Console=logging. Streamhandler () Console.setlevel (logging. DEBUG) Formatter= Logging. Formatter ('% (filename) -12s-% (asctime)-S-% (Lineno) d-% (message) s') Console.setformatter (formatter) Logging.getlogger ("'). AddHandler (console) Logging.info ('This is info message') logging.warning ('This is warning message')
Screen Output results:
log_ad.py-2016-06-04 11:35:27,732-17-this is info message
log_ad.py-2016-06-04 11:35:27,732-18-this is warning message
Log file Output results:
Sat, June 11:35:27 Log_ad.py[line:17] Info this is info message
Sat, June 11:35:27 Log_ad.py[line:18] WARNING This is WARNING message
Log rollback for logging
ImportLogging fromLogging.handlersImportRotatingfilehandlerrthandler= Rotatingfilehandler ('Myapp.log', maxbytes=1,backupcount=5The//maxbytes Unit is kbrthandler.setlevel (logging.info) Formatter= Logging. Formatter ('% (name) -12s:% (levelname) -8s% (message) s') Rthandler.setformatter (formatter) Logging.getlogger ("'). AddHandler (Rthandler) logging.info ('This is info message') logging.warning ('This is warning message')
Output Result:
Executes and eventually generates six files, respectively, for myapp.log,myapp.log.1,myapp.log.2,myapp.log.3,myapp.log.4,myapp.log.5
Several handle methods of the
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 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
http://my.oschina.net/leejun2005/blog/126713
Python Logging Module