Logmuse: Logging Setup
Logmuse is a lightweight Python library designed to simplify the setup of basic logging for applications. It provides utilities to configure named loggers, set log levels, and integrate logging options with `argparse`. The current version is 0.3.0, and it maintains a relatively stable release cadence with minor updates.
Common errors
-
AttributeError: module 'logmuse' has no attribute 'configure_logging'
cause You are trying to use a non-existent function. The primary function for setting up logging in `logmuse` is `setup_logging`.fixUse `from logmuse import setup_logging` and call `setup_logging(...)` instead. -
No handlers could be found for logger "your_application_name"
cause This generic Python logging error indicates that a logger tried to emit messages but had no handlers configured. This typically happens if `logmuse.setup_logging` was not called, or was called for a different logger name than the one emitting messages.fixEnsure `logmuse.setup_logging(name="your_application_name", ...)` is called *before* any messages are emitted by `logging.getLogger("your_application_name")`. -
ModuleNotFoundError: No module named 'logmuse'
cause The `logmuse` package is not installed in your current Python environment.fixRun `pip install logmuse` in your terminal to install the package.
Warnings
- gotcha Calling `setup_logging` multiple times for the same logger name (or without a name, affecting the root logger) can overwrite previous configurations or add duplicate handlers.
- gotcha The default logging level set by `logmuse.setup_logging` might not be `DEBUG`. If you're not seeing expected log messages, the level might be set too high.
- gotcha Interaction with `logging.basicConfig()`: If `logging.basicConfig()` has been called elsewhere in your application (e.g., by another library or implicitly), `logmuse.setup_logging` might not behave as expected or could lead to duplicate log messages.
Install
-
pip install logmuse
Imports
- setup_logging
from logmuse import setup_logging
- add_logging_options
from logmuse import add_logging_options
Quickstart
import logging
from logmuse import setup_logging
# Get a logger instance for your application
_LOGGER = logging.getLogger("my_app")
def main():
# Setup logging to console at INFO level for 'my_app'
# Set 'level="DEBUG"' to see more detailed messages
setup_logging(name="my_app", level="INFO")
_LOGGER.info("Application started successfully!")
_LOGGER.debug("This debug message will not be shown by default with INFO level.")
_LOGGER.warning("This is a warning message.")
_LOGGER.error("An error occurred, but the app continues.")
if __name__ == "__main__":
main()