1. Simply print the log to the screen
Import logginglogging.debug ('This isdebug message') logging.info (' This is info message ' ) logging.warning ('This iswarning message') on-screen printing : 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
ImportLogginglogging.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, 21:48:54 Demo2.py[line:11] Debug This is debug Messagesun, the May 21:48:54 Demo2.py[line:12] INFO Thi S is Info Messagesun, 21:48:54 demo2.py[line:13] WARNING The IS WARNING 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
ImportLogginglogging.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')##################################################################################################define a 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 Thisis info messageroot:warning The contents of this is WARNING Message./myapp.log file are: Sun, may 21:48:54 Demo2.py[line : [One] debug this is debug Messagesun, 21:48:54 Demo2.py[line:12] Info This is info Messagesun, 2009 21:4 8:54 Demo2.py[line:13] WARNING This is WARNING message
Reference: http://blog.csdn.net/langb2014/article/details/53397307
Python's logging log module