Logistro
Logistro is an extremely light wrapper over Python's standard `logging` module, providing sensible defaults for logging configuration. It simplifies common logging setups and includes specialized functionality like `getPipeLogger()` for piping subprocess stderr to the main logger. The library is currently at version 2.0.1 and maintains an active development status, with recent releases focused on minor fixes and metadata updates.
Warnings
- gotcha When switching between human-readable (`set_human()`) and structured (`set_structured()`) logging formats, these functions must be called *before* any other logging calls. Calling them mid-execution with an existing logger requires an additional step (`logistro.coerce_logger(logistro.getLogger())`) to ensure the formatter is updated.
- gotcha The `logistro.betterConfig()` function, which is implicitly called by `logistro.getLogger()`, accepts most arguments identical to Python's standard `logging.basicConfig()` but *ignores* the `format` argument. This is because Logistro manages its own formatting based on `set_human()` or `set_structured()`.
- gotcha When using `logistro.getPipeLogger()` to capture subprocess output, it returns a pipe file descriptor. It is crucial to manually close this pipe using `os.close(pipe)` after the subprocess has completed to prevent resource leaks.
Install
-
pip install logistro
Imports
- getLogger
import logistro logger = logistro.getLogger(__name__)
- getPipeLogger
import logistro pipe, logger = logistro.getPipeLogger(__name__ + '-subprocess')
- set_structured
import logistro logistro.set_structured()
- set_human
import logistro logistro.set_human()
Quickstart
import logistro
import os
import subprocess
# Basic logger setup (automatically configured by getLogger)
logger = logistro.getLogger(__name__)
logger.debug2("This is a custom debug level (more verbose)")
logger.debug("This is a standard debug message")
logger.info("Application started")
try:
# Example with getPipeLogger for subprocesses
# Note: In a real app, manage 'cli_command' securely (e.g., using shlex.split)
cli_command = [os.environ.get('EXAMPLE_SUBPROCESS_COMMAND', 'ls'), '-l']
pipe, sub_logger = logistro.getPipeLogger(__name__ + '-subprocess')
process = subprocess.Popen(cli_command, stderr=pipe)
process.wait() # Wait for the subprocess to finish
os.close(pipe) # Crucial to close the pipe after use
sub_logger.info("Subprocess finished with exit code: %s", process.returncode)
raise ValueError("Something went wrong")
except ValueError as e:
logger.exception("An error occurred: %s", e)
# Switch to structured logging mid-execution (affects subsequent logs)
logistro.set_structured()
logger.info("This message is now structured (JSON)", user_id="123", event="test_event")