The logger logs dictionary python configuration file

Import OS
 Import logging.config    # is not only introduced into the logging 

base_dir = os.path.dirname (os.path.dirname ( __FILE__ ))
 # DB_PATH R & lt = '% S \ DB \ db.txt'% base_dir 
DB_PATH R & lt = ' % S \ DB ' % base_dir 

# define a log file path 
log_path R & lt = ' % S \ log \ the access.log ' % base_dir 
BOSS_LOG_PATH = R & lt ' % S \ log \ boss.log ' % base_dir 

# define three formats for log output start 
standard_format = '[% (the asctime) S] [% (threadName) S:% (Thread) D] [task_id:% (name) S] [% (filename) S:% (lineno) D] ' \
                   ' [% (levelname) S] [% (Message) S] '  # where name is getlogger name specified 

simple_format = ' [% (levelname) S] [% (the asctime) S] [% (filename) S:% (lineno) D]% ( Message) S ' 

id_simple_format = ' [% (levelname) S] [% (the asctime) S]% (Message) S ' 
# define the format of the log output end 

logfile_dir = os.path.dirname (os.path.abspath with ( __FILE__ ))   # directory log files 
logfile_name = ' all2.log '   # log file name 

#If the log directory does not exist to create a defined 
IF  not os.path.isdir (logfile_dir): 
    os.mkdir (logfile_dir) 

# full path to the log file 
logfile_path = os.path.join (logfile_dir, logfile_name) 

# log configuration dictionary 
LOGGING_DIC = {
     ' Version ' :. 1 ,
     ' disable_existing_loggers ' : False,
     ' formatters ' : {
         ' Standard ' : {
             ' the format ' : standard_format 
        }, 
        ' Simple ' : {
            ' The format ' : simple_format 
        }, 
        ' id_simple ' : {
             ' the format ' : id_simple_format 
        }, 
    }, 
    ' Filters ' : {},
     ' handlers ' : {
         # printed to the log of the terminal 
        ' Stream ' : {
             ' Level ' : ' the DEBUG ' ,
             ' class ' : ' logging.StreamHandler ' ,  #Printed to the screen 
            ' Formatter ' : ' Simple ' 
        }, 
        # printed to the log file, the log collecting and above info 
        ' Access ' : {
             ' Level ' : ' the DEBUG ' ,
             ' class ' : ' logging.handlers.RotatingFileHandler ' ,   # save to file 
            ' Formatter ' : ' Standard ' ,
             ' filename ' : logfile_path,  # Log file
            ' MaxBytes ' : 1024 * 1024 * 5,   # log size 5M 
            ' BACKUPCOUNT ' : 5 ,
             ' encoding ' : ' UTF-8 ' ,   # encoding of the log file, no longer have to worry about Chinese log garbled 
        }
         # Print to File log, the log collecting and above error 
        ' BOSS ' : {
                     ' Level ' : ' eRROR ' ,
                     ' class ' : ' logging.handlers.RotatingFileHandler ' ,  #Save to File 
                    ' Formatter ' : ' id_simple ' ,
                     ' filename ' : BOSS_LOG_PATH,   # log file 
                    # 'MaxBytes': 1024 * 1024 * 5, # log size 5M 
                    ' MaxBytes ' : 300,   # log size 5M 
                    ' BACKUPCOUNT ' : 5 ,
                     ' encoding ' : ' UTF-8 ' ,   # encoding of the log file, no longer have to worry about Chinese log garbled 
                }, 
    }, 
    ' Loggers ' :
        {
        # Logging.getLogger (__ name__) get logger configuration 
        '' : {
             ' handlers ' : [ ' Stream ' , ' Access ' , ' BOSS ' ],   # where the handler are defined above plus two, i.e. log data both write files and printed to the screen 
            ' level ' : ' the DEBUG ' ,
             ' Propagate ' : True,   # up (the higher level logger) passing 
        },
         # we will then take the logger object logging.getLogger (__ name__), different file different __name__, which ensures that when the print log different identification information 
        #But took the name to loggers where to find key name can not be found when we got there, so the default key = '' configuration 
    }, 
} 


DEF load_my_logging_cfg (): 
    logging.config.dictConfig (LOGGING_DIC)   # import definitions above logging configuration 
    = logging.getLogger Logger ( the __name__ )   # generate a log example 
    logger.info ( ' It Works! ' )   # record operating state of the file 

IF  the __name__ == ' __main__ ' : 
    load_my_logging_cfg ()
View Code

 

Guess you like

Origin www.cnblogs.com/mylu/p/11116437.html