3

I have a function that I want to output logging from. The logfiles are part of the program output as a record of when the function did what (i.e. not just debugging or post mortem stuff), so I want to store them in a specific location. That location is loaded from a config file, but I want to log success or failure in reading the config file!

So I can't create the logfile until after reading config but I don't want to read the config until after I can log the process.

Is there a way to postpone a set of log statements being written to file until later? A simplified version of the function is below: obviously it doesn't run because logfile isn't available until later.

It looks like basicConfig(..., delay=True) might be the answer but it sounds like that only handles a delay for the first logging event. Is there an answer for more logging events?

import logging
import json

def record(config):

    logger = logging.getLogger(LOG)
    logging.basicConfig(filename=logfile, level=logging.INFO)
    logger.info('Start of continuous sampling')

    # Load the config file
    try:
        config = json.load(open(config_file))
        logfile = config['logfile']
        logger.info('Config file found')
    except IOError:
        logger.critical('Config file not found')
        sys.exit()
David_O
  • 1,143
  • 7
  • 16
  • Please always proof read your typing, it is not easy to follow. The `delay` is like lazy loading, it will write when you do your first call to `info`, `warn,` ,`error` etc. – user1767754 Dec 11 '17 at 17:51

1 Answers1

0

There are multiple options, but here are two rather simple ones:

Option 1

Start logging into a temp file. If you were able to parse the config file, copy the temp file content and change the logging file handle. If there was an error, dump the temp file to stdout.

Option 2

Create a wrapper class that delays calls to the logger as long as the config file is not available. Something along the lines of

class MyLogger(object):
  def __init__(self, ...)
    self.logger = ... # temp file

  def info(self, msg):
    self.logger.info(msg)

  def changeLogfile(self, f):
    self.logger = ... # now writing to f

Once config['logfile'] is available, call mylogger.changeLogfile() and continue logging.

Pavel
  • 7,436
  • 2
  • 29
  • 42