day23-logging module

# Two content logging logging: 1, there are five levels of logging mode. 2, two configurations: basicconfig, logger object. 
# Action logging: the 
# 1 troubleshooting when you need to print a lot of details to help troubleshoot. 
# 2, the error recorded. 
# 3, user behavior, it's right to be recorded. 

# 1.5 kinds of levels of logging modes: 
Import the logging 
logging.debug ( ' Debug Message ' ) # troubleshooting information to a low level 
logging.info ( ' info Message ' )    # normal information 
logging.warning ( ' warning Message ' ) # warnings 
logging.error ( ' error the message ' )   #Error message 
logging.critical ( ' Critical the Message ' ) # serious high-level error message 
# the WARNING: root: the Message warning only records the warning, error, critical, because low-level debug and info. 
# ERROR: the root: error Message 
# CRITICAL in: the root: Critical Message 

# 2. configurations: basicConfig 
Import the logging 
logging.basicConfig (Level = logging.DEBUG, # . Debug start recording from here DEBUG must be uppercase. 
    The format = ' % (the asctime) S -% (name) S -% (levelname) S -% (Module1) S:% (Message) S ' , 
    datefmt = ' % Y-M-% D%% H:% M:% S% the p- ' , #Time format 
    filename = ' x1.log ' , # filename 
    the fileMode = ' A ' ) # file operation mode is added. 

logging.debug ( ' Debug Message ' ) # 2019-07-31 14:43:46 the PM - the root - the DEBUG -day23-loggingģ: Debug Message 
logging.info ( ' info Message ' ) 
logging.warning ( ' warning Message ' ) 
logging.error ( ' error Message ' ) 
logging.critical ( ' Critical Message ' ) 

#3. user behavior (eg input), there is nothing wrong to be recorded. 
# Note: basicConfig not let Chinese output to the screen, garbled output to the screen is only written to a file. 
Import the logging 
logging.basicConfig ( 
    the format = ' % (the asctime) S -% (name) S -% (levelname) S -% (Module1) S:% (Message) S ' , 
    datefmt = ' %% Y-M-% H% D:% M:% S% P ' , 
    filename = ' x2.log ' , 
    the fileMode = ' a ' , 
    Level = logging.DEBUG)
 try :                        # when try following code is False, that is, the input is not a int type string, before the implementation of the code except the following.
    int (INPUT ( ' >>> ' ))
 the except : 
    logging.debug ( ' input is not a number ' )
     # 2019-07-31 14:25:14 the PM - the root - the DEBUG -day23-loggingģ: IJһ 
    # when input a letter, it is not a number, except the following code will be executed, the execution result is distortion of the screen can not be printed 'is not a digital input' to the screen 
    # because it is Chinese, if English is no problem. Because pycharm is uft-8 encoding, 
    # written to the computer's file x2.log is gbk coding, x2.log found in the computer, turn it on, there will be garbled. 
    # If you do not write the file to record information, then the filename = 'x2.log', filemode = 'a' be deleted, so there will be garbled output to the screen. 

# 4. basicConfig features: You can not go screen and file output, can only choose one. 

# 5. Logger object configuration: the default is 'a' append mode, I found only above the level recorded warning mode (warning, error, critical). 
# 5.1 output to a file log.
Import the logging 
Logger = logging.getLogger () # Logger target 
FH = logging.FileHandler ( ' log.log ' , encoding = ' UTF-. 8 ' ) # file operators for writing the log file 
Formatter = logging.Formatter ( ' % (the asctime) S,% (Module1) S,% (Message) S ' ) # format 
fh.setFormatter (Formatter) # file format associated operator 
logger.addHandler (FH) # Logger file object associated operator 
logging.warning ( ' warning Message ' ) 
logging.error ( ' error Message ') 
Logging.critical ( ' Critical Message ' ) 

# 5.2 output to a file simultaneously and the console screen: 
Import the logging 
Logger = logging.getLogger () 

FH = logging.FileHandler ( ' log.log ' , encoding = ' UTF-. 8 ' ) 
SH = logging.StreamHandler () # screen operator 

Formatter = logging.Formatter ( ' % (the asctime) S,% (Module1) S,% (Message) S ' ) 
formatter1 = logging.Formatter ( ' % (the asctime) S, % (Message) S ' ) # format

fh.setFormatter (formatter) 
sh.setFormatter (formatter1) # screen or formatter1 operator can be associated formatter. 

logger.addHandler (FH) 
logger.addHandler (SH) # Logger object associated screen operator 

logging.warning ( ' warning ' ) 
logging.error ( ' error ' ) 
logging.critical ( ' serious error message ' )

 

Guess you like

Origin www.cnblogs.com/python-daxiong/p/11304132.html