Logging setup
Project description
logmuse
A small Python package that makes it easy to add CLI-controllable logging to your command-line tools. It provides a simple interface for standard logging arguments (--verbosity, --silent, --logdev) that can be passed directly to a logger.
Installation
pip install logmuse
Quick start
Add logging options to your CLI and create a logger in three lines:
import argparse
import logmuse
parser = argparse.ArgumentParser()
# your arguments here...
parser = logmuse.add_logging_options(parser)
args = parser.parse_args()
logger = logmuse.logger_via_cli(args)
logger.info("Hello, world!")
This adds --silent, --verbosity, and --logdev options to your CLI automatically.
Setup for a CLI package
1. Add CLI args to your argparser
parser = logmuse.add_logging_options(parser)
This gives you --verbosity, --silent, and --logdev options.
2. Activate in your main function
import logmuse
def main():
# ... parse args ...
logger = logmuse.logger_via_cli(args, make_root=True)
That's it. Call logger_via_cli once in your CLI entry point, and all loggers in your package (and its dependencies) will respect the CLI options.
For imported packages
Imported packages don't need logmuse at all. Just use the standard logging module:
import logging
_LOGGER = logging.getLogger(__name__)
The CLI arguments passed to logmuse will control these loggers too.
Avoid calling init_logger at import time
Do not put logmuse.init_logger() in your package's __init__.py. This replaces the logger's handlers at import time, which prevents it from inheriting configuration from a root logger. This causes problems when your package is used as a library (not via its own CLI).
Using logmuse interactively
To set logging to DEBUG in an interactive session:
logmuse.init_logger("my_package", "DEBUG", devmode=True)
Usage reference
CLI options
add_logging_options(parser) adds three arguments to an argparse.ArgumentParser:
| Option | Effect |
|---|---|
--silent |
Silence all logging output |
--verbosity V |
Set logging level (1=CRITICAL, 2=ERROR, 3=WARN, 4=INFO, 5=DEBUG, or a level name) |
--logdev |
Use a detailed developer log format with timestamps, module, and line numbers |
init_logger parameters
| Parameter | Default | Description |
|---|---|---|
name |
"logmuse" |
Logger name |
level |
None |
Logging level (int or string) |
verbosity |
None |
Verbosity level (1-5); alternative to level |
silent |
False |
Silence all output |
devmode |
False |
Use detailed developer format |
stream |
stderr |
Output stream ("OUT" for stdout, "ERR" for stderr) |
logfile |
None |
Path to log file (mutes stream output) |
make_root |
None |
Use as root logger |
propagate |
False |
Allow messages to reach parent loggers |
fmt |
None |
Custom format string |
datefmt |
"%H:%M:%S" |
Time format |
plain_format |
False |
Force plain format even in devmode |
style |
None |
Format style (%, {, or $) |
use_full_names |
False |
Don't truncate level names |
Verbosity levels
| Verbosity | Logging Level |
|---|---|
| 1 | CRITICAL |
| 2 | ERROR |
| 3 | WARN |
| 4 | INFO |
| 5 | DEBUG |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file logmuse-0.3.0.tar.gz.
File metadata
- Download URL: logmuse-0.3.0.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
298c3336cbca049de2f3d6d4bc1ea4ca5d9d6154c748f34cfc86e89d31e3fc9f
|
|
| MD5 |
7876355cbf68985212a13ab5c15e20b4
|
|
| BLAKE2b-256 |
d33dd1ed45e1488b6e7aaee971f6d72f7a5f95d7b389dfe872dc08dd83ed2cf0
|
Provenance
The following attestation bundles were made for logmuse-0.3.0.tar.gz:
Publisher:
python-publish.yml on databio/logmuse
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
logmuse-0.3.0.tar.gz -
Subject digest:
298c3336cbca049de2f3d6d4bc1ea4ca5d9d6154c748f34cfc86e89d31e3fc9f - Sigstore transparency entry: 1046864888
- Sigstore integration time:
-
Permalink:
databio/logmuse@a7223bf4e84fd87402fb864b39c4e80b3b5234c4 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/databio
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@a7223bf4e84fd87402fb864b39c4e80b3b5234c4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file logmuse-0.3.0-py3-none-any.whl.
File metadata
- Download URL: logmuse-0.3.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
810ba709895bc2e08867d557d992e44a16895dfb4e477e61418d445e12be8a01
|
|
| MD5 |
b5e61df14078e38cb7096d4f1030bb79
|
|
| BLAKE2b-256 |
af56dce8413a48934065e6dcb814a58aba3f592b6109d149c2bdb4214aab4bca
|
Provenance
The following attestation bundles were made for logmuse-0.3.0-py3-none-any.whl:
Publisher:
python-publish.yml on databio/logmuse
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
logmuse-0.3.0-py3-none-any.whl -
Subject digest:
810ba709895bc2e08867d557d992e44a16895dfb4e477e61418d445e12be8a01 - Sigstore transparency entry: 1046864945
- Sigstore integration time:
-
Permalink:
databio/logmuse@a7223bf4e84fd87402fb864b39c4e80b3b5234c4 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/databio
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@a7223bf4e84fd87402fb864b39c4e80b3b5234c4 -
Trigger Event:
release
-
Statement type: