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()