0

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

wow, I even read that in the docs and thought to myself "nope, they have different names" because I was thinking about the file names, and not the getLogger call. Duh! Thanks much, works as expected now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.