darwin-logger
Zero-dependency structured logging for Darwin CLI tools and data scripts. It is a small configuration layer on top of the Python standard library logging: you still get ordinary logging.Logger objects, but setup, colors, file rotation and JSON output are handled in one place.
- Zero core dependencies — standard library only;
pytestis a test extra - stderr by default — safe for scripts, containers and cron jobs
- Text by default, JSON on demand — one environment flag makes logs ingestible by ELK, Loki and Datadog
- Explicit reconfiguration —
configure_logging()is safe to call repeatedly;reconfigure_logging()rebuilds it - No import side effects —
get_logger()only returns a logger, it never mutates the root logger - Rotation and context built in — size/time rotation and
contextvars-based task context
Python >= 3.12.
pip install darwin-logger # core
pip install 'darwin-logger[test]' # with pytest
pip install 'darwin-logger[color]' # optional Windows ANSI support
1. Quick start
from darwin_logger import configure_logging, get_logger
configure_logging(level="INFO", use_color=False)
log = get_logger("demo")
log.debug("hidden by default")
log.info("application started")
Output goes to stderr:
2026-11-26 13:40:00 [INFO] demo: application started
get_logger() is a pure function. Call configure_logging() once in your application entry point; library modules may call get_logger(__name__) at import time without changing global state.
2. JSON logs for observability
export LOG_JSON=1
export LOG_SERVICE=fetch
export LOG_ENV=prod
from darwin_logger import bind_context, clear_context, configure_logging, get_logger
configure_logging()
token = bind_context(run_id="run-42", source="tushare")
try:
get_logger("worker").info("job_done", extra={"rows": 120})
finally:
clear_context(token)
One line per event:
{
"@timestamp": "2026-11-26T05:40:00.123Z",
"level": "INFO",
"logger": "worker",
"message": "job_done",
"service": "fetch",
"env": "prod",
"run_id": "run-42",
"source": "tushare",
"rows": 120
}
The field names are intentionally flat and generic: Loki/Promtail can parse the line as-is, Elasticsearch/OpenSearch can index it without a custom grok pattern, and Datadog accepts level, message, service and @timestamp directly. Add your own fields with extra= or bind_context().
3. File output and rotation
from pathlib import Path
from darwin_logger import RotationConfig, configure_logging, get_logger
configure_logging(
level="INFO",
file=Path("logs/app.log"),
rotation=RotationConfig(kind="size", max_bytes=10 * 1024 ** 2, backup_count=5),
stderr=False,
from_env=False,
)
get_logger("file").info("persisted")
kind="time" uses the standard library TimedRotatingFileHandler:
RotationConfig(kind="time", when="midnight", interval=1, backup_count=7)
The parent directory is created automatically. Handlers created by this library are closed and replaced on reconfigure_logging() and reset_logging().
4. Environment variables
| Variable | Default | Meaning |
|---|---|---|
LOG_LEVEL |
INFO |
Root level; names or integers. Invalid values raise InvalidLevelError |
LOG_FORMAT |
built-in text format | %-style format for text output |
LOG_DATE_FORMAT |
%Y-%m-%d %H:%M:%S |
Date format for text output |
LOG_JSON |
0 |
1/true/yes/on enables JSON output |
LOG_STDERR |
1 |
0/false/no/off disables the stderr handler |
LOG_FILE |
unset | Enable a UTF-8 file handler at this path |
LOG_ROTATE |
unset | size or time |
LOG_MAX_BYTES |
10MB |
Size threshold when LOG_ROTATE=size |
LOG_BACKUP_COUNT |
5 |
Number of rotated files to keep |
LOG_ROTATE_WHEN |
midnight |
S/M/H/D/W0..W6/MIDNIGHT when LOG_ROTATE=time |
LOG_ROTATE_INTERVAL |
1 |
Rotation interval when LOG_ROTATE=time |
LOG_SERVICE |
unset | Adds service to JSON output |
LOG_ENV |
unset | Adds env to JSON output |
NO_COLOR |
unset | Disable colors regardless of value |
FORCE_COLOR |
unset | Force colors; NO_COLOR still wins |
DARWIN_COLOR |
unset | Legacy alias with truthy values 1/true/yes/on |
Explicit arguments override environment variables. An already-active configuration is returned unchanged unless you pass force=True or call reconfigure_logging().
5. API at a glance
from darwin_logger import (
configure_logging,
reconfigure_logging,
reset_logging,
is_configured,
config_from_env,
get_logger,
set_log_level,
set_logger_level,
get_logging_config,
get_logger_config,
bind_context,
clear_context,
get_context,
ConsoleFormatter,
JsonFormatter,
LoggingConfig,
RotationConfig,
)
| Function | Purpose |
|---|---|
configure_logging(**kwargs) |
Initial setup; repeat calls are no-ops unless force=True |
reconfigure_logging(**kwargs) |
Rebuild handlers and replace the active configuration |
reset_logging() |
Restore root/named logger levels and close owned handlers |
get_logger(name="") |
Return a standard logging.Logger; no side effects |
set_log_level(level) |
Change the root level at runtime |
set_logger_level(name, level) |
Change one named logger; restored by reset_logging() |
bind_context(**fields) |
Bind context fields to the current async task |
get_logging_config() |
Inspect the active setup as a dictionary |
Invalid levels raise InvalidLevelError; invalid boolean or rotation settings raise ConfigurationError; file sink failures raise SinkError. All derive from DarwinLoggerError.
6. Colors
Text format supports two optional placeholders:
"%(log_color)s%(levelname)s%(reset)s %(message)s"
When colors are off, both placeholders expand to empty strings. Color resolution order is: explicit use_color > NO_COLOR > FORCE_COLOR > DARWIN_COLOR > stream isatty(). JSON output never contains ANSI codes.
7. Testing
uv run --extra test python -m pytest tests -q
The suite covers unit behavior, stderr/file/JSON integration, executed examples, zero third-party imports, stable exports and bilingual documentation. See docs/development-guide.md for architecture details and examples/ for runnable scripts.
8. License
MIT. See LICENSE.
Metadata
Release files for darwin-logger 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| darwin_logger-0.1.0.tar.gz | 10.4 kB | Without provenance | Details for darwin_logger-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| darwin_logger-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for darwin_logger-0.1.0-py3-none-any.whl |
Total release size: 22.8 kB
Release files / darwin_logger-0.1.0.tar.gz
| Download URL | darwin_logger-0.1.0.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3b7d5a3d24c6227bede3d23380418c11328f14e34182bfe3921289fcf7e3a79e
|
|
BLAKE2b-256 checksum How to use checksums |
14076d17d67a6f8c0643aaddd96d839fc4e52fa7ddcee96c8cf20bcac19bdfcf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / darwin_logger-0.1.0-py3-none-any.whl
| Download URL | darwin_logger-0.1.0-py3-none-any.whl |
|---|---|
| Size | 12.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
214a47a0f25c3224b51fdf01521027205a0079464fdbc6733997f21523c40220
|
|
BLAKE2b-256 checksum How to use checksums |
b4e955fd4ffad3e78c1d068ad2d7293f86292bf7faf588fce6607eda0ec9dcdf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for darwin-logger 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.