amoodie
amoodie

Reputation: 331

Log to different files for different Python objects that exist simultaneously

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

Upvotes: 0

Views: 643

Answers (1)

M Z
M Z

Reputation: 4799

I think your issue comes from the fact that logging is returning the same logger object. From the python docs: "Multiple calls to getLogger() with the same name will always return a reference to the same Logger object."

You then are adding file handlers that should go to separate objects to one object, so it is logging it to the same files.

What you'd want to do is probably along the lines of self.logger = logging.getLogger(str(i))

Upvotes: 2

Related Questions