logging module - log configuration

Functional simple configuration

Import the logging   
logging.debug ( ' Debug Message ' )   # low level for debugging information 
logging.info ( ' info Message ' )   # normal information 
logging.warning ( ' warning Message ' )   # warning 
logging.error ( ' the message error ' )   # error message 
logging.critical ( ' critical the message ' ) # high level, used for severe error messages

 Python's logging module logs printed by default to the standard output, and only shows a greater than or equal WARNING level log, indicating that the default log level WARNING (log level Level CRITICAL> ERROR> WARNING> INFO> DEBUG), the default log format for the log level: Logger name: user output messages.

Two kinds of configuration log, basicconfig configuration and log object configuration

# Basicconfig simple but can do relatively little, there are Chinese garbage problem, can not go on file and screen output

# Log object configuration, a little bit can do relatively more complex

basicconfig Configuration

Import the logging 
file_handler are = logging.FileHandler (filename = ' x1.log ' , MODE = ' A ' , encoding = ' UTF-. 8 ' ,) to create a log # 
logging.basicConfig ( 
    the format = ' % (the asctime) S -% (name ) S -% (levelname) S -% (Module1) S:% (Message) S ' , # formatting 
    datefmt = ' % Y-M-% D%% H:% M:% S% P ' , the time # formatting 
    handlers = [file_handler are], # set the input to create a file, if this line is not set, the default output to the console                            
    level = logging.error # logging level is set, and if not, the default system settings 
)

log object configuration

Import the logging 
Logger
= logging.getLogger ()
# Create a handler operator for writing the log file
FH = logging.FileHandler ( ' the test.log ' , encoding = ' UTF-. 8 ' )
# then create a handler operator, for output to the console
CH = logging.StreamHandler ()
Formatter
= logging.Formatter ( ' % (the asctime) S -% (name) S -% (levelname) S -% (Message) S ' ) fh.setFormatter (Formatter ) # format associated with the corresponding operator ch.setFormatter (Formatter) logger.addHandler (fh) # operator and log files associated with objects, Logger plurality of objects can be added and fh ch objects logger.addHandler (ch)

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/account/p/11271474.html