python learning -logging


"" "
# Set the output of the contents of the log format
FMT = '% (the asctime) S% (filename) S% (funcName) S [Line:% (lineno) D]% (levelname) S% (Message) S'
datefmt = '% A,% B% D% the Y% H:% M:% S'
"" "
# datetime ===

# Logging operation
# .log file form
# log level, time, information display
effect # log: record execution. What program did, do anything.

# Standard library - logging
Import logging

logging.debug ( "I am the one, debug level log !!")
logging.info ( "I am a, info level log !!")
logging.warning ( "I am the one, warning level log !!" )
logging.error ( "I am a, error level log !!")
logging.critical ( "I am the one, critical level log !!")

More # info level, will be printed. and debug info is not printed.
# Set the log level which show! ! ! --- set operation.
# Default log collector rootLogger, it displays the default log level is: WARN

# Set the display level to determine which logs can be exported.

# Define its own log collector. I have to decide what level of log is displayed.
= logging.getLogger Logger ( "PY17")
# set the output level
print ( "================================= ")
# Logger.setLevel (" iNFO ")
Logger.setLevel (logging.info)
logger.info (" I am a, info level log !! ")
logger.warning (" I am the one, warning level log! ! ")
logger.error (" I am a, error level log !! ")
logger.debug (" I am the one, debug level log !! ")

# Want to set the content presented in the form of logs.
# Special classes: the Formatter
# logging.Formatter (FMT, datefmt)
FMT = '% (the asctime) S% (filename)% S (funcName) S [Line:% (lineno) D]% (levelname) S% (Message ) S '
. ft = logging.Formatter (FMT)

# Handle === output channels. Console? file? Log output to where container.
handler1 = logging.StreamHandler () # log contents to the console
handler1.setFormatter (ft) # console determines which log content presentation format

# Bind format to the log collector py17 - can not be directly binding.
logger.addHandler (handler1)
logger.info ( "I am a, info level log !!")

# Log Collector -logger handler - output channel formatter - log content presentation style
# 1, create your own log collector - Logger = getLogger ( "PY17")
# 2, the log level display - logger.setLevel ( )

# If you want to change the default output style
# 3, create a handler - handler1 = logging.StreamHandler () - where output.
# 4, to create a formatter - ft = logging.Formatter (fmt) - log output style
# 5, the log output style set handler - handler1.setFormatter (. Ft)
#. 6, which bind to the log collector. logger.addHandler (handler1)

# Next lesson: Log output to a file - handle. --- define a class of its own logs! !
# If combined with excel class uses.
# Two classes used in combination! ! !

 

Guess you like

Origin www.cnblogs.com/qsmyjz/p/11261290.html