Import logginglogging.debug (' Debug message ') logging.info (' Info message ') logging.warning (' Warning message ') logging.error (' Error message ') logging.critical (' critical message ') output: E:\Python >python lianxi.pywarning:root:warning messageerror:root:error messagecritical:root:critical message# by default, Python's logging module outputs the log output and only outputs logs that are greater than or equal to warning levels. (Log level level Critical>error>warning>info>debug) ( critical > Error > Warning > Information > Debug > Not set), the default output log format is Log Level: Logger name: User output message. #设置日志的输出格式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 ', #r ' xxx ' R followed by a string indicating that the escape character is not parsed filename=r ' E:\Python\test.log ', filemodE= ' W ') logging.debug (' Debug message ') logging.info (' Info message ') logging.warning (' warning Message ') logging.error (' Error message ') logging.critical (' critical message ') file content is:2016-05-15 10:05:24,843 lianxi.py[line:7] debug debug message2016-05-15 10:05:24,843 lianxi.py[line:8] info info message2016-05-15 10:05:24,843 lianxi.py[line:9] Warning warning message2016-05-15 10:05:24,843 lianxi.py[line:10] error error message2016-05-15 10:05:24,843 lianxi.py[line:11] critical critical message# Visible in the Logging.basicconfig () function, you can change the default behavior of the logging module through specific parameters, the available parameters are: #ilename: Create Filehandler with the specified file name so that the log is stored in the specified files. #filemode: The way the file is opened, use this parameter when filename is specified, and default to ' a '. #format: Specify handler to use the date format. #datefmt: Specifies the date format. #level: Set the Rootlogger log level. #stream: creates a streamhandler with the specified stream. When the filename and stream are two arguments, the stream parameter is ignored. #format参数中可能用到的格式化串: #% (name) S&NBsp Logger's name #% (levelno) s the log level in digital form #% (levelname) s the log level in text Form #% (pathname) s The full pathname of the module that called the log output function, possibly without #% ( FileName) s The name of the module that called the log output function #% (module) s the module name of the call log output function #% (funcName) s call the function name of the log output function #% (Lineno) d The Code line #% (created) f the current time of the statement that invokes the log output function, and the floating points of the UNIX standard representing the time #% (relativecreated) d Output The log information, are created from logger to The number of milliseconds #% (asctime) s the current time in the string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by the millisecond #% (thread) d thread ID. There may not be a #% (threadname) s thread name. There may not be a #% (process) d process ID. There may not be an instance application process for #% (message) s user output messages #logging ..... import logging #1, Application logging module logger = logging.getlogger ("My_log") #2, creating a logging logging.getlogger (name), name is not Logger.setlverl (logging.info) #3, set the level filelog = logging of the logging log display. Filehandler (LogFileName) #4, creating a log file handle logfilename is a log filePath to streamlog = logging. Streamhandler () #4, create a handle to a log output fmatter = Logging. Formatter ("% (asctime) s - % (filename) s - % (levelname) -8s: % (message) S", datefmt= '%y-%m-%d %i:%m:%s %p ') #5, set the format of log output to file smatter = logging. Formatter ("% (asctime) s % (filename) s % (levelname) s:% (message) S", datefmt= '%y-%m-%d %i:%m:%s ') #5, set the format of the log output Filelog.setformatter (fmatter) #6, load the log file format STREAMLOG.SETFORMATTRT ( Fmatter) #6, load the output log format filelog.setlevel (logging.info) #7, sets the level of the log file, determines the output message Stream.setlevel (logging.info) #7, sets the level of the log output Logger.addhandler (filelog) #8, set the output direction of the file Logger.addhandler (Stream) logger.info ("msg") #9, set the output of the log, and critical, error, Warning, info, debug level # The log output level set above determines where you need to define that level of log logger.removehandler (FileLog) #10, destroy the handle Logger.removehandler (Streamlog)
This article is from the "Creative Pilgrim" blog, so be sure to keep this source http://dearch.blog.51cto.com/10423918/1775695
Python-->logging .... Instance Application