Skip to content

Call logging.basicConfig in Python2 setups#18

Merged
jpotter merged 1 commit intomainfrom
jeff-fix-16
May 18, 2021
Merged

Call logging.basicConfig in Python2 setups#18
jpotter merged 1 commit intomainfrom
jeff-fix-16

Conversation

@jpotter
Copy link
Contributor

@jpotter jpotter commented May 18, 2021

Fixes #16

In Python 3, calling logger.warn/error/whatever() functions automatically triggers logging configuration when it's not been set up. In Python 2, not configuring logging causes a spurious No handlers could be found for logger "jebenaclient" to be emitted.

This PR only calls basicConfig() when in python2 -- from what I recall, calling basicConfig() in a package that one imports can cause some side-effects if the main program hasn't already triggered its logging config. (Essentially: the first call to configure logging is the one that wins; or at least by default. I would need to research to confirm if this is strictly true, but ... I've seen enough issues with this pattern to want to avoid it.)

Simple way to reproduce the issue:

  1. In test.py, put:
import foo
foo.bar()
  1. In foo.py, put:
import logging
LOGGER = logging.getLogger("jebenaclient")
# logging.basicConfig()

def bar():
  LOGGER.error("error log")
  print("bar() called, logger stuff called.")

Running python3 test.py will result in just the print() statement; running python2 will cause the logger-not-configured error. (Note I'm testing on MacOS, where future imports automatically support print() notation in Python 2.)

@jpotter jpotter merged commit eab4d2c into main May 18, 2021
@jpotter jpotter deleted the jeff-fix-16 branch May 18, 2021 01:35
jpotter added a commit that referenced this pull request Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No handlers could be found for logger "jebenaclient"

2 participants