Skip to content

root logger gets configured for any app that imports this library #58

@daniel-j-h

Description

@daniel-j-h

Hey folks I'm working over at https://github.com/daniel-j-h/meshcore-irc-bridge with your awesome meshcore_py library.

I noticed that we call

logging.basicConfig(level=logging.INFO)

this configures the root logger for any app that imports this library. In my case for example it configures my app's logger and overwrites and hard codes its behavior.

The second problem seems to be setting the levels in

if debug:
logger.setLevel(logging.DEBUG)
elif only_error:
logger.setLevel(logging.ERROR)
else:
logger.setLevel(logging.INFO)

but this less of a problem.

What I propose: the python idiomatic way for a library to set up logging seem to be

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler()) # if needed to prevent warnings

and to let apps importing this lib configure their logging like this

logging.basicConfig(level=logging.INFO)
logging.getLogger("meshcore").setLevel(logging.DEBUG)

This would make your awesome library a better citizen with apps where logging is already configured.

refs

  1. https://docs.python.org/3/howto/logging.html#library-config
  2. https://docs.python.org/3/library/logging.handlers.html#logging.NullHandler

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions