A log consists of four parts
1. logger logger
program, a logger records log information during program execution
log information, may be used to monitor the running state of operation and maintenance.
Log Handler processor 2.
When the log information recorded by the recording, the processor needs to process.
StreamHandler generally used in console output
may also be used to HTTPHandler server log files to the log (Log The Flume Distributed Services)
course, the use FileHandler writes log information to a file
used in the processor:
stream processor: StreamHandler
file handler: FileHandler
network request processor: HTTPHandler uploads the log information to a log server (Flask / Flume)
mail box processor: SMTPHandler
3. formatting formatter log
records log records, according to a certain format to the processor
format comprising:
the asctime time of occurrence log
pathname
filename
funcName
Process Process ID
Thread thread ID
Message log message
name levelname log level
line number occur lineno log
records the name name filter
4. Filter log filter
filters recorder for recording information
two, log level
in the logging module, points 6 levels
- cRITICAL 50 serious errors
- eRROR 40 error
- wARNING 30 wARNING
- INFO 20 general message
- dEBUG 10 debug
- nOSET 0 default is not set
three step application
. 1) Import the logging
Import TimeRotatingFileHandler, the HTTPHandler, SMTPHandler
2) creating or get the logger default name for the root logger
= logging.getLogger Logger ( 'django.request')
# Set logger level
Logger.setLevel (logging.info)
root_logger logging.getLogger = ()
. 3) to create formatter formatting objects, and add to the processor
Time level
F = the Formatter (FMT = '% (the asctime) S% (levelname) S:% (Message) S',
datefmt = '')
. 4) creating processor Handler
Handler The StreamHandler = ()
handler.setFormatter (F) set # formatting processor
level # setting processor
handler.setLevel (logging.info)
. 5) to add processors to a logger object
logger.addHandler (Handler)
. 6) at a certain feature point of the program, by logging is to record important information
logger.info ( 'xxxx') # level of information is recorded iNFO
logger.error ( 'xxxx') # class information is recorded ERROR
logger.debug ( '.....') # level information is recorded DEBUG
Log Log
Guess you like
Origin blog.csdn.net/weixin_44111412/article/details/91968972
Recommended
Ranking