-
Notifications
You must be signed in to change notification settings - Fork 21
Improve logging practices/make hatchet logging opt in #114
Description
Current Behavior
The library currently sets up logging configuration at the module level:
import logging
import sys
# Create a named logger
logger = logging.getLogger("hatchet")
logger.setLevel(logging.ERROR)
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("[%(levelname)s] hatchet -- %(asctime)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.propagate = FalseAdditionally, this single hatchet logger is used throughout the library instead of module-local loggers.
Issues
-
Inflexible Logging Configuration: The current setup makes it difficult for users to opt out of or modify the logging behavior. This limits the flexibility for applications that may need different logging setups (e.g., logging to files, using structured logging, or integrating with specific logging services).
-
Single Logger Usage: Using a single
hatchetlogger throughout the library, instead of module-local loggers, makes it harder for users to have fine-grained control over logging from different parts of the library.
Proposed Changes
-
Optional Logging Configuration: Make the current logging setup optional, allowing users to easily opt out or configure their own logging setup. I would even be fine if it were enabled by default without an explicit opt out somehow.
-
Module-Local Loggers: Use module-local loggers (e.g.,
logging.getLogger(__name__)) throughout the library instead of a singlehatchetlogger.
Benefits
These changes would:
- Provide more flexibility for users to integrate the library into their existing logging setups.
- Allow for more fine-grained control over logging from different parts of the library.
- Align with Python logging best practices for libraries.
Example of Good Logging Practices
For reference, SQLAlchemy provides a good example of logging practices in a Python library:
https://docs.sqlalchemy.org/en/14/core/engines.html#configuring-logging
Possible Implementation
- Move the current logging setup into a function that users can optionally call.
- Replace all instances of
logging.getLogger("hatchet")withlogging.getLogger(__name__). - Update documentation to explain how users can configure logging.