Env-aware logging with per-call stdout control and a handy ErrorHandler.
Project description
logging-and-error-handling-kit
Env-aware logging with per-call stdout control and rotating file handlers. Designed for services that need clean console logs in dev and quiet consoles in prod—without losing file logs.
Features
- Per-call stdout control:
logger.info("...", display_on_stdout=False) - Env-aware default: console shows logs in local/staging, stays quiet in prod (
APP_ENV=prod) - Rotating files:
service.log,errors.log, optionaldebug.log - One-liner setup with
setup_logger()andget_logger(__name__) - Compatible with standard
loggingAPI; no vendor lock-in.
Install
pip install logging-and-error-handling-kit
Quickstart
from logging_and_error_handling_kit import setup_logger, get_logger
setup_logger() # once at app startup
log = get_logger(__name__)
log.info("Hello!") # prints in non-prod, always goes to files
log.info("Silent on console", display_on_stdout=False) # still goes to files
log.error("Oops", display_on_stdout=True) # force to console (subject to handler level)
How it decides what hits stdout
-
APP_ENVcontrols the default:APP_ENV=prod→ defaultdisplay_on_stdout=False- any other (or unset) → default
True
-
You can override per call:
display_on_stdout=True/False -
Console level via
CONSOLE_LOG_LEVEL(defaultINFO)
Environment variables
| Name | Default | Purpose |
|---|---|---|
APP_ENV |
local |
Env profile: local, staging, prod… |
CONSOLE_LOG_LEVEL |
INFO |
Console verbosity (DEBUG, INFO, WARNING, ERROR) |
DEBUG |
False |
If true, also writes detailed debug.log |
LOGS_DIR (if you add this) |
logs |
Directory for log files |
Tip: Add a
.envand usepython-dotenv(already in dependencies) to load these at startup.
File outputs (rotating)
logs/service.log→INFO+logs/errors.log→ERROR+logs/debug.log→DEBUG+(only ifDEBUG=true)
API
setup_logger() -> None
# Initializes handlers/formatters/filters on the root logger.
get_logger(name: str) -> logging.Logger
# Standard logger retrieval.
# Log methods (standard logging API) accept:
logger.info(msg, *args, display_on_stdout: bool = <env default>, **kwargs)
logger.debug(...)
logger.warning(...)
logger.error(...)
logger.critical(...)
logger.exception(msg, *args, display_on_stdout=<env default>, **kwargs)
# .exception sets exc_info=True automatically
Examples
Service-style bootstrap
import os
from logging_and_error_handling_kit import setup_logger, get_logger
from dotenv import load_dotenv
load_dotenv() # loads APP_ENV, CONSOLE_LOG_LEVEL, etc.
setup_logger()
log = get_logger("my.service")
log.info("Service starting…")
try:
1 / 0
except Exception:
log.exception("Calculation failed", display_on_stdout=True)
Quiet console in prod, verbose locally
# local
export APP_ENV=local
python app.py # console shows logs
# prod
export APP_ENV=prod
python app.py # console minimal unless you force display_on_stdout=True
Project structure (suggested)
src/
logging_and_error_handling_kit/
__init__.py
error_handler.py
logger_config.py
README.md
LICENSE
pyproject.toml
Versioning & releases
- Bump
versioninpyproject.toml - Build:
python -m build - Upload:
python -m twine upload dist/* - Install:
pip install logging-and-error-handling-kit
Contributing
Issues and PRs welcome. Please add tests and keep public API stable.
License
MIT – see LICENSE.
How to publish the README changes to PyPI
-
Edit
README.md -
Ensure
pyproject.tomlhasreadme = "README.md" -
Rebuild & upload a new version:
# bump version in pyproject.toml (e.g., 0.1.2) python -m build python -m twine upload dist/*
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 logging_and_error_handling_kit-0.1.2.tar.gz.
File metadata
- Download URL: logging_and_error_handling_kit-0.1.2.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
318fc4f2db13e80b89649b37829fb6e49a04f07450d9d8359c66e2c8854e3bf3
|
|
| MD5 |
f32aeae3214242e7e327f8f8a07879c0
|
|
| BLAKE2b-256 |
1563b44f3a52125996fd9cff91dedeb99544fa65483c216ef40dfbce6cd1b421
|
File details
Details for the file logging_and_error_handling_kit-0.1.2-py3-none-any.whl.
File metadata
- Download URL: logging_and_error_handling_kit-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcda1d3e00a03926b3756f36c43b4618fb299de4a642bcd8b63cf8232e8b5ef7
|
|
| MD5 |
2635974eaf0b987b9a8900c7f0037158
|
|
| BLAKE2b-256 |
79879b89e6011d96fb022ec588338efabb0644a2312c7d7c2a1a31687da589b4
|