Reprint: http://blog.csdn.net/liuchunming033/article/details/39080457
Rotatingfilehandler Method Reference http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html
Import logging
From logging.handlers import Rotatingfilehandler
#################################################################################################
#定义一个RotatingFileHandler, backup up to 5 log files, maximum 10M per log file
Rthandler = Rotatingfilehandler (' myapp.log ', maxbytes=10*1024*1024,backupcount=5)
Rthandler.setlevel (logging.info)
Formatter = Logging. Formatter ('% (name) -12s:% (levelname) -8s% (message) s ')
Rthandler.setformatter (formatter)
Logging.getlogger ("). AddHandler (rthandler)
################################################################################################
1. Log Level
The log is divided into 5 levels, from low to high, respectively: debuginfo WARNING ERROR CRITICAL.
DEBUG: detailed information, usually appearing only on diagnostic issues
INFO: Make sure everything works as expected
WARNING: a sign that some unexpected things have happened, or indicate some problems in the near future (for Example. Low disk space "). The software will work as Expected.
ERROR: a more serious problem, the software does not perform some functions
CRITICAL: a serious error that indicates that the program itself may not continue to run
These 5 levels, also correspond to 5 kinds of methods to hit the Log: debug, info, warning, error, critical. The default is warning, which is tracked only when it is warning or above.
2. Log Output
There are two ways to record a trace, an output console, and a record to a file, such as a log file.
2.1. output the log to the console
For example, write a file called log.py, as Follows:
[python] View Plain Copy
# Coding=utf-8
__author__ = ' liu.chunming '
Import Logging
Logging.basicconfig (level=logging. WARNING, format= '% (asctime) s-% (filename) s[line:% (lineno) d]-% (levelname) s:% (message) s ')
# Use Logging
Logging.info (' This is a loggging info message ')
Logging.debug (' This is a loggging debug message ')
Logging.warning (' This is loggging a warning message ')
Logging.error (' This was an loggging error message ')
Logging.critical (' This is a loggging critical message ')
Executing the above code will output the following information in the Console:
C:\Python27\Python. exec:/users/liu.chunming/pycharmprojects/myproject/log.py
2015-05-21 17:25:22,572-log.py[line:10]-warning:this is loggging a warningmessage
2015-05-21 17:25:22,572-log.py[line:11]-error:this is an loggging errormessage
2015-05-21 17:25:22,572-log.py[line:12]-critical:this is a logggingcritical message
Resolution
Through the Logging.basicconfig function to the output format and configuration of the log, the above code set the log output level is warning level, meaning that the warning level above the log will be Output. In addition, the format of the log output is also Established.
2.2. output the log to a file
We can also output the log to a file, just set the output File's filename and write the file mode in the Logging.basicconfig Function.
[python] View Plain Copy
# Coding=utf-8
__author__ = ' liu.chunming '
Import Logging
Logging.basicconfig (level=logging. WARNING,
Filename= './log/log.txt ',
Filemode= ' W ',
format= '% (asctime) s-% (filename) s[line:% (lineno) d]-% (levelname) s:% (message) s ')
# Use Logging
Logging.info (' This is a loggging info message ')
Logging.debug (' This is a loggging debug message ')
Logging.warning (' This is loggging a warning message ')
Logging.error (' This was an loggging error message ')
Logging.critical (' This is a loggging critical message ')
After running, open the file./log/log.txt, the effect is as Follows:
2015-05-2117:30:20,282-log.py[line:12]-warning:this is loggging a WARNING message
2015-05-21 17:30:20,282-log.py[line:13]-error:this is an loggging errormessage
2015-05-21 17:30:20,282-log.py[line:14]-critical:this is a logggingcritical message
2.3, both the log output to the console, but also to write log files
This requires an object called logger to help, he will be described in detail below, and now here to learn how to implement the log output to the console and output to the file Function.
[python] View Plain Copy
# Coding=utf-8
__author__ = ' liu.chunming '
Import Logging
# First step, Create a logger
Logger = Logging.getlogger ()
Logger.setlevel (logging.info) # Log level master switch
# step two, Create a handler to write to the log file
LogFile = './log/logger.txt '
FH = Logging. Filehandler (logfile, mode= ' W ')
Fh.setlevel (logging. DEBUG) # Output to file log level switch
# step three, Then create a handler for output to the console
CH = logging. Streamhandler ()
Ch.setlevel (logging. WARNING) # switch output to the log level of the console
# Fourth step, define the output format of the handler
Formatter = Logging. Formatter ("% (asctime) s-% (filename) s[line:% (lineno) d]-% (levelname) s:% (message) S")
Fh.setformatter (formatter)
Ch.setformatter (formatter)
# Fifth step, Add logger to Handler
Logger.addhandler (fh)
Logger.addhandler (ch)
# log
Logger.debug (' This is a logger debug message ')
Logger.info (' This is a logger info message ')
Logger.warning (' This is a logger warning message ')
Logger.error (' This is a logger error message ')
Logger.critical (' This is a logger critical message ')
After executing this code, in the console, you can see:
c:\python27\python.exec:/users/liu.chunming/pycharmprojects/myproject/log.py
2015-05-21 17:47:50,292-log.py[line:30]-warning:this is a logger warningmessage
2015-05-21 17:47:50,292-log.py[line:31]-error:this is a logger errormessage
2015-05-21 17:47:50,293-log.py[line:32]-critical:this is a logger criticalmessage
In logger.txt, you can see:
2015-05-2117:47:50,292-log.py[line:29]-info:this is a logger INFO message
2015-05-21 17:47:50,292-log.py[line:30]-warning:this is a logger warningmessage
2015-05-21 17:47:50,292-log.py[line:31]-error:this is a logger ERROR message
2015-05-21 17:47:50,293-log.py[line:32]-critical:this is a logger criticalmessage
Resolution
It can be found that the implementation of this function is divided into 5 steps:
The first step is to create a logger; the second step is to create a handler for writing to the log file; the third step is to create a handler for output to the console, fourth, define handler output format, and fifth step to add logger to Handler. There are many concepts mentioned in this code, including: logger,handler,formatter. These concepts are explained in the Following.
3. Sequence of log output in multiple modules
Often we have multiple modules in our work that require output logs. so, What is the order of the log output of the gate between the modules that have the call relationship? Let's take a demonstration: suppose there are two files, namely util.py:
[python] View Plain Copy
# util.py
__author__ = ' liu.chunming '
Import Logging
def Fun ():
Logging.info (' This was a log in util module ')
and main.py
[python] View Plain Copy
# main.py
# Coding=utf-8
__author__ = ' liu.chunming '
Import Logging
Import Util
Logging.basicconfig (level=logging.info,
Filename= './log/log.txt ',
Filemode= ' W ',
format= '% (asctime) s-% (filename) s[line:% (lineno) d]-% (levelname) s:% (message) s ')
def Main ():
Logging.info (' Main module start ')
Util.fun ()
Logging.info (' Main module Stop ')
if __name__ = = ' __main__ ':
Main ()
After running open log.txt, the results are as follows:
2015-05-21 18:10:34,684-main.py[line:11]-info:main Module Start
2015-05-21 18:10:34,684-util.py[line:5]-info:this is a log in util module
2015-05-21 18:10:34,684-main.py[line:13]-info:main Module Stop
Resolution
As you can see, the output order of the logs is the order in which the modules are Executed.
Python's Logging Module