Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't configure logging handlers in a library #228

Closed
tevansuk opened this issue Mar 11, 2021 · 2 comments
Closed

Don't configure logging handlers in a library #228

tevansuk opened this issue Mar 11, 2021 · 2 comments

Comments

@tevansuk
Copy link

logger = logging.getLogger('xmlschema')
logging_formatter = logging.Formatter('[%(levelname)s] %(message)s')
logging_handler = logging.StreamHandler(sys.stderr)
logging_handler.setFormatter(logging_formatter)
logger.addHandler(logging_handler)

You should not do this.

Note It is strongly advised that you do not add any handlers other than NullHandler to your library’s loggers. This is because the configuration of handlers is the prerogative of the application developer who uses your library. The application developer knows their target audience and what handlers are most appropriate for their application: if you add handlers ‘under the hood’, you might well interfere with their ability to carry out unit tests and deliver logs which suit their requirements.

Which is how I spent my morning :/ In order to fix this, the code in my project that sets up logging now needs to have knowledge of where you are (currently) configuring handlers, and import them before setting up our own desired logging so that we can remove the loggers added by the library. Huge pain.

@brunato
Copy link
Member

brunato commented Mar 11, 2021

I'm sorry, I was not aware of this. Logging is optionally used only in schema initialization to track namespace imports or XSD schemas inclusion.

I'm going to remove these lines for the next release:

logging_formatter = logging.Formatter('[%(levelname)s] %(message)s')
logging_handler = logging.StreamHandler(sys.stderr)
logging_handler.setFormatter(logging_formatter)
logger.addHandler(logging_handler)

@brunato
Copy link
Member

brunato commented Mar 14, 2021

Fix available within release v1.5.3.

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

No branches or pull requests

2 participants