For the MWE below, I would like to be able to create two ModelObjects, and then run the model objects at a later time. In actuality, there are an unknown number of ModelObjects and many steps comprise the run action, but this covers the idea.
What I want from the logging is to have a .log file that records all of the log information, warnings, etc., for an individual ModelObject, and nothing from the other models.
import logging
class ModelObject(object):
def __init__(self, i):
self.i = i
self.logger = logging.getLogger('driver')
self.logger.setLevel(logging.INFO)
fh = logging.FileHandler('logs/log_'+str(self.i)+'.log')
self.logger.addHandler(fh)
self.logger.info('finished initialization in %s' % self.i)
def run(self):
self.logger.info('running job in %s' % self.i)
if __name__ == '__main__':
objs_list = []
for i in range(2):
objs_list.append(ModelObject(i))
## later, want to run the jobs
for i in range(2):
objs_list[i].run()
The MWE produces the log files logs/log_0.log and logs/log_1.log correctly, but all information is being logged to each file, after that log has been created.
# log_0.log
finished initialization in 0
finished initialization in 1
running job in 0
running job in 1
# log_1.log
finished initialization in 1
running job in 0
running job in 1
How do I write to only the single log file for a single object (i.e., self.logger for each object)?