Razmodius
Razmodius

Reputation: 301

Python logging across open source modules

I'm working on a python script with logging functionality. I've read the docs for the logging module as well as many Stack Overflow posts and I've learned a lot, but I'm still having trouble understanding how the logging module allows different modules to send log events to the same destination.

I have my main module which uses a python module I found online. This module already has logging set up to write to a log file, but I'd like it to write to the log file I define in my main module.

#myModule.py
import logging
import otherModule

logger = logging.getLogger(__name__)
if __name__ == "__main__": 
    logger.setLevel(logging.DEBUG)
    handler = logging.FileHandler('my_log.log')
    handler.setLevel(logging.DEBUG)
    formatter = logging.Formatter('[%(asctime)s] %(levelname)%s: %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)

def main():
    logger.info('hello world')
    otherModule.do_stuff()

As you can see, I explicitly set up my handlers and formatters and then add them to my logger instance. But this is how the module I'm using has logging setup:

#otherModule.py
import logging

logging.basicConfig(filename='otherModule.log', filemode='w')

def do_stuff():
    logging.info('Stuff is happening')

With this setup I have two questions:

  1. What is the difference in the ways that we are setting up our logging? I tried using logging.basicConfig() at first, but I wasn't able to get it to write to the log file (I may have been doing something wrong). But I'd like to know what the difference between creating a logging instance (logger = logging.getLogger(__name__)) and calling the logging functions directly (logging.info(msg)).
  2. How can I get otherModule.py to write to the same log file defined in my main module, and how does Python know to use the logger created in myModule.py (I tried to figure the answer to this out by looking through the Python docs, but they mostly just said that you can log to the same place across multiple modules)? Since it is an open source module, I'd rather not modify it for my own use.

Upvotes: 2

Views: 188

Answers (1)

postoronnim
postoronnim

Reputation: 556

That is the general problem with modules that do their own logging and don't have if __name__ == "__main__": block. When they get imported, you cannot make them follow your logging setup because it gets overwritten. If you are a contributor to this open source project, I'd suggest submit a patch, otherwise just report it as a bug. The way to fix it would be to add the aforementioned header to the logging setup in otherModule.

Upvotes: 1

Related Questions