2

I am writing a package that has a __main__.py with something like:

def main():
    logging.basicConfig(level=logging.INFO)
    print(logging.getLogger(__name__))

if __name__ == "__main__":
    main()

And I am running it like so:

python3 -m my_package

And seeing:

<Logger __main__ (ERROR)>

instead of INFO. And so none of my log messages are showing up on the screen.

Why is basicConfig not having effect?

shikhanshu
  • 1,466
  • 2
  • 16
  • 32
  • This works fine for me. What code are you using to log a message? – Michael M. Mar 26 '23 at 01:34
  • The usual. logging.info(“something”). Tried setting level to debug and logging.debug didn’t show up either. The level remains set to ERROR for some reason. – shikhanshu Mar 26 '23 at 01:59
  • Ok, I have narrowed the reason down to a third-party package that I am using. It might be doing something bad that is causing my basicConfig to not work. What's a good way to ignore what that package might be doing with logging and enforce my own rules? – shikhanshu Mar 26 '23 at 02:09
  • I think I see my answer here - https://stackoverflow.com/questions/38537905/set-logging-levels basicConfig doesn't work if something else is setting root handler. I should use `getLogger` to get the logger and set its level. Trying now. – shikhanshu Mar 26 '23 at 02:13

1 Answers1

3

Another package I was importing was setting logging upfront, and my basicConfig settings were not overriding. I added force=True to my basicConfig call and it works now.

shikhanshu
  • 1,466
  • 2
  • 16
  • 32