python logging module basis

Getting started is simple logging module

. 1  Import the logging
 2  # five kinds of log level, the default output to the screen; warning level and above the default print log 
. 3 logging.debug ( " Test Debug " )
 . 4 logging.info ( " Test info " )
 . 5 logging.warning ( " Test warning " )
 . 6 logging.error ( " Test error " )
 . 7 logging.critical ( " Test Critical " )
 . 8  
. 9  # set the log output parameters, the output of the log file path filename, in which case the log is not output to a file. level output to a log file level; format and time format which is the log information, datefmt timestamp format for the log 
10 # Note: You can not print the log before logging logging.basicConfig otherwise logging.basicConfig not log output to a file, or will output to the screen 
11 logging.basicConfig (filename = " Test3 " ,
 12                      Level = logging.info,
 13                      format = ' % (the asctime) S% (levelname) S% (Message) S ' ,
 14                      datefmt = " % m /% D /% the I the Y%:% m:% S " )
 15 logging.warning ( " Test warning " )

Meaning summary log format in the value of each property

% (names) s logger name 
% (levelno) s digital format log level
% (levelname) s in text format log level
% function module full path (pathname) s call log
% (filename) s call log output module file name
% (module) s call log output function module name
name of the function% (funcName) s call log output
% (lineno) d log output function call statement line where
% (created) f unix timestamp format standard time
milliseconds% (relativeCreated) d Logger inception of
% (asctime) s current time format string
% (thread) s thread the above mentioned id
% (threadName) s thread name
% (process) d process the above mentioned id
% (the Message) s log message

log output file to a different
. 1  Import logging
 2  "" " 
. 3  Python use the logging module according to four main classes
 . 4  Logger provides an interface for applications to use directly
 . 5  Handler will send log output to the appropriate destination
 . 6  filter provides filtering, determines which log output
 7  final decision format log output formatter
 8  "" " 
9  # Create a logger, and set the logging level to Debug 
10 logger1 = logging.getLogger ( " logger_name " )
 . 11  logger1.setLevel (logging.DEBUG)
 12 is  
13 is  # Create a screen and the output handler set the logging level to info 
14 SH = logging.StreamHandler ()
 15  sh.setLevel (logging.info)
 16 
17  # Create a file output handler and set the logging sector as a warning, the output log file is file_log 
18 FH = logging.FileHandler ( " file_log.log " )
 19  fh.setLevel (logging.WARNING)
 20  
21  # Set the log format, , the same format logging.basicConfig parameters here meaning, name argument corresponding logging.getLogger 
22 is formatter1 logging.Formatter = ( ' % (the asctime) S% (levelname) S% (name) S% (Message) S ' )
 23 is  
24  # add to the Handler log format, the different format may be associated to different hd 
25  sh.setFormatter (formatter1)
 26 is  fh.setFormatter (formatter1)
 27  
28  # the logger hd in association can only be associated with a 
29 logger1.addHandler (SH)
 30  logger1.addHandler (FH)
 31 is  
32 logger1.error ( " Test error " )
 33 is logger1.info ( " Test info " )
 34 is  "" " 
35  screen output
 36  2019-12-11 21:54 : 10,961 Test error eRROR logger1
 37 [  2019-12-11 21 is: 54 is: 10,961 logger1 the iNFO info Test
 38 is  the document output
 39  2019-12-11 21 is: 54 is: 10,961 eRROR error logger1 Test
 40  "" "

 


Log segmentation

1  # automatic cutting of the log, it is necessary to import a separate handlers 
2  from the logging Import handlers
 . 3 logger2 = logging.getLogger ( the __name__ ) # name of this incoming program 
4  # -create the specified file handler and cutting, each file is stored 3 byte logs, retains the latest three files and sets file level 
. 5 FH1 handlers.RotatingFileHandler = (filename = " split_byte.log " , MaxBytes = 2, = BACKUPCOUNT. 3 )
 . 6  # create handler and the specified file to the standard cutting time , cut once every 5 seconds, retain up to three files. S s H h M W min D days per week (interval == 0 for Monday) every morning Midnight 
. 7 FH2 handlers.TimedRotatingFileHandler = (filename = " split_time " , When ="S", interval=5, backupCount=3)
 8 fh1.setLevel(logging.INFO)
 9 fh1.setFormatter(formatter1)
10 logger2.addHandler(fh1)
11 logger2.warning("this is a waring log,split log test xxxxxxxdddddddddddddddddddddddddddddddddddd")
12 logger2.warning("this is a waring log,split log test xxxxxxx")

 



Guess you like

Origin www.cnblogs.com/flags-blog/p/12026312.html