Python Logging configuration
In Python, logging is composed of four parts of Logger,handler,filter,formater, logger is a way to provide us with logging, handler is to let us choose the output place of the log, such as: console, file, mail send, etc. A logger adding multiple handler;filter is to give the user more granular control of the output of the log, Formater users to format the output log information.
There are three ways to configure logging in Python
The first type: basic configuration, Logging.basicconfig (filename= "Config.log", filemode= "W", format= "% (asctime) s-% (name) s-% (levelname) s-% ( Message) S ", Level=logging.info).
Second: Configure logging using the configuration file and use the Fileconfig (filename,defaults=none,disable_existing_loggers=ture) function to read the configuration file.
Third: Use a dictionary to write the configuration information, and then use the Dictconfig (Dict,defaults=none, disable_existing_loggers=ture) function to tile the configuration as logging.
In the Logging.basicconfig () function, the default behavior of the logging module can be changed by specific parameters, with the parameters available
FileName: Creates a filedhandler with the specified file name (the concept of handler is explained in the back) so that the log is stored in the specified file.
FileMode: File is opened by using this parameter when filename is specified, and the default value is "a" and can be specified as "W".
Format: Specifies the log display format used by handler.
DATEFMT: Specifies the date time format.
Level: Set the log levels for Rootlogger (which will explain the concepts behind)
Stream: Creates a streamhandler with the specified stream. You can specify the output to Sys.stderr,sys.stdout or to a file, and the default is Sys.stderr.
If you list both the filename and stream two parameters, the stream parameter is ignored.
formatting strings that may be used in the format parameter:
% (name) s logger name
% (Levelno) s log level in digital form
% (levelname) s log level in text form
% (pathname) s calls the full pathname of the module of the log output function and may not have
% (filename) s The file name of the module that called the log output function
% (module) s call the module name of the log output function
% (FuncName) s Call the function name of the log output function
% (Lineno) d The line of code where the statement of the log output function is called
% (created) F current time, represented by the UNIX standard floating-point number representing the time
% (relativecreated) d when the log information is output, the number of milliseconds since logger was created
% (asctime) s The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds
% (thread) d thread ID. Probably not.
% (threadname) s thread name. Probably not.
% (process) d process ID. Probably not.
% (message) s user-output message
Log level level critical > ERROR > WARNING > INFO > DEBUG > NOTSET
Propagete parameter: propagete=0, which represents the output log, but the message is not delivered; propagate=1 is the output log, and the message is delivered to a higher level. Root is the highest level.
Example:
Logging.basicconfig (filename= "Config.log", filemode= "W", format= "% (asctime) s-% (name) s-% (levelname) s-% (message) S" , Level=logging.info);
#Coding=utf-8ImportOSImportdatetimeImportLoggingImportLogging.configdefMain ():""""""Base_dir= Os.path.dirname (Os.path.abspath (__file__)) Log_dir= Os.path.join (Base_dir,"logs") if notos.path.exists (Log_dir): Os.makedirs (Log_dir)#Create PathLog_file= Datetime.datetime.now (). Strftime ("%y-%m-%d") +". Log"LOGGING= { "version": 1, "disable_existing_loggers": False,"formatters": { " Simple": { 'format':'% (asctime) s [% (name) s:% (Lineno) d] [% (levelname) s]-% (message) s' }, ' Standard': { 'format':'% (asctime) s [% (threadname) s:% (thread) d] [% (name) s:% (Lineno) d] [% (levelname) s]-% (message) s' }, }, "handlers": { "Console": { "class":"logging. Streamhandler", " Level":"DEBUG", "Formatter":" Simple", "Stream":"Ext://sys.stdout" }, "default": { "class":"Logging.handlers.RotatingFileHandler", " Level":"INFO", "Formatter":" Simple", "filename": Os.path.join (Log_dir, log_file),'Mode':'w+', "MaxBytes": 1024*1024*5,#5 MB "Backupcount": 20, "encoding":"UTF8" }, }, #"Loggers": { #"App_name": { #"Level ": "INFO", #"Handlers": ["Console"], #" Propagate": "No" # } # }, "Root": { 'handlers': ['default'], ' Level':"INFO", 'Propagate': False}} Logging.config.dictConfig (logging)defdo_something (): Log= Logging.getlogger (__file__) Print "Print A"Log.info ("Log B")if __name__=='__main__': Main () do_something ()
Log Configuration related articles:
Http://python.usyiyi.cn/python_278/library/logging.config.html
http://my.oschina.net/leejun2005/blog/126713
Http://blog.chinaunix.net/uid-26000296-id-4372063.html
Python Logging configuration