MArablu
MArablu

Reputation: 41

Python logging - How to add a new log file for each iteration in a loop?

I am trying to generate new log file for each iteration in a loop using:

def custlogger(Filename,loglevel=logging.DEBUG):
    # set method name from where its called
    logger_name = inspect.stack()[1][3]
    # create logger
    logger = logging.getLogger(logger_name)
    logger.setLevel(loglevel)
    # create console handler of file handler and set log level
    file_handler = logging.FileHandler(filename=Filename)
    # create formatter
    formatter = logging.Formatter('%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s')
    # add formatter to console or file handler    
    file_handler.setFormatter(formatter)
    # add console handler to loogger
    logger.addHandler(file_handler)

and I call the newlogger = custlogger(newfilename) and shut down logging at the end of each iteration logging.shutdown() inside the loop. Is this the right way to do it?

Here is an example how I am using it:

def analyze_scds_files(scds_directory, sample_name):
    logfile = scds_directory+'/log_'+sample_name+'.txt'
    newlogger = custlogger(logfile,logging.ERROR)                                                        
    try:
        dosomething()
    except Exception as e:
        newlogger.logger.exception('error while trying to ...: ')
        sys.exit()
    .
    .
    .
    # Shut down the logger
    logging.shutdown()

    return status

Upvotes: 1

Views: 877

Answers (1)

Marc
Marc

Reputation: 1987

Suggestions, a little under documented question, Move this out of func and call in header (1x, you are redeclaring)

logger = logging.getLogger(logger_name)
logger.setLevel(loglevel)

The minimal pattern is:

import logging
logger = logging.getLogger(__name__)
# in header ^^ in loop ---v
logger('xx') # call this in loop

Get it to print in terminal first, then add formatting and other features. KISS v1, then complify.

Upvotes: 2

Related Questions