Skip to main content

Beautiful, production-ready Python logging with colors and JSON output

Project description

prettylogx

Beautiful, production-ready Python logging — zero external dependencies.

prettylogx wraps Python's built-in logging module and gives you:

  • Colored, human-readable logs for your terminal
  • Structured JSON logs for production log shippers
  • Automatic request ID tracing (per-thread)
  • Clean exception / traceback output
  • Custom log levels (VERBOSE, SUCCESS)
  • A single function — setup_logger() — to set it all up

Installation (local development)

# 1. Clone the repo
git clone https://github.com/yourusername/prettylogx.git
cd prettylogx

# 2. Create and activate a virtual environment
python -m venv .venv
# Windows:
.venv\Scripts\activate
# macOS / Linux:
source .venv/bin/activate

# 3. Install in editable mode (changes to source take effect immediately)
pip install -e ".[dev]"

Quick start

from prettylogx import setup_logger

logger = setup_logger("my-app")

logger.info("Server started on port 8000")
logger.warning("Database response time is high")
logger.error("Payment failed for order #9921")

Terminal output:

2024-01-15 10:30:45 | INFO     | my-app | Server started on port 8000
2024-01-15 10:30:46 | WARNING  | my-app | Database response time is high
2024-01-15 10:30:47 | ERROR    | my-app | Payment failed for order #9921

Each level is printed in its own color (green / yellow / red / etc.).


Colored logs (terminal)

from prettylogx import setup_logger

logger = setup_logger(
    name="my-app",
    level="DEBUG",       # show all levels including DEBUG
    show_time=True,      # include timestamp (default: True)
    show_file=True,      # include filename:lineno
    use_colors=True,     # ANSI colors (default: True)
)

logger.debug("Connecting to DB")
logger.info("User logged in")
logger.warning("Rate limit at 80%")
logger.error("Auth token expired")
logger.critical("Service unreachable")

JSON logs (production)

Switch to JSON output by passing json_logs=True. Every line is a self-contained JSON object you can pipe to jq or ship to Datadog / Loki / CloudWatch.

from prettylogx import setup_logger

logger = setup_logger("api", json_logs=True)

logger.info("Request received")
logger.error("DB connection refused")

Output:

{"timestamp": "2024-01-15T10:30:45Z", "level": "INFO", "logger": "api", "message": "Request received", "module": "app", "function": "handle", "line": 12}
{"timestamp": "2024-01-15T10:30:46Z", "level": "ERROR", "logger": "api", "message": "DB connection refused", "module": "app", "function": "connect", "line": 34}

Request ID tracing

Track a single request across many log lines without passing the ID to every function.

from prettylogx import setup_logger, set_request_id, clear_request_id

logger = setup_logger("api")

# At the start of a request:
set_request_id("req-abc123")

logger.info("Validating input")     # → includes [req-abc123]
logger.info("Querying database")    # → includes [req-abc123]
logger.info("Returning response")   # → includes [req-abc123]

# At the end of the request:
clear_request_id()

Or pass it explicitly per call:

logger.info("Order placed", extra={"request_id": "req-xyz"})

Exception logging

from prettylogx import setup_logger

logger = setup_logger("app")

try:
    result = 1 / 0
except ZeroDivisionError:
    logger.exception("Unexpected error in calculation")

The full traceback is captured and formatted cleanly below the log line. In JSON mode it appears as the "exception" field.


setup_logger() parameters

Parameter Type Default Description
name str "app" Logger name shown in every log line
level str|int "INFO" Minimum level: DEBUG / INFO / WARNING / ERROR / CRITICAL
json_logs bool False True → JSON output, False → colored output
show_time bool True Include timestamp in colored output
show_file bool False Include filename:lineno in colored output
use_colors bool True False strips ANSI codes (e.g. for file output)

Why not just use the raw logging module?

Feature logging prettylogx
Colored terminal output No Yes
JSON / structured output No Yes
Request ID tracing Manual Built-in
Zero-config setup No Yes
No external dependencies Yes Yes
Works with existing code Yes

prettylogx is a thin wrapper — you can still use every standard logging feature (handlers, filters, log levels) alongside it.


Running locally

# Run examples
python examples/basic_usage.py
python examples/json_logs.py
python examples/request_id.py
python examples/exception_logging.py

# Run tests
pytest

# Run tests with coverage report
pytest --cov=prettylogx --cov-report=term-missing

# Build the distributable package
pip install build
python -m build
# → creates dist/prettylogx-0.1.0.tar.gz and dist/prettylogx-0.1.0-py3-none-any.whl

Roadmap

  • File handler support (write logs to a rotating file)
  • Async support (asyncio-safe context variables via contextvars)
  • Middleware helpers for FastAPI / Django / Flask
  • Log sampling (emit only N% of DEBUG logs in production)
  • PyPI publication

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prettylogx-0.1.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prettylogx-0.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file prettylogx-0.1.0.tar.gz.

File metadata

  • Download URL: prettylogx-0.1.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for prettylogx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b36846ab87b58d0e27e1344c4b4c05d4ec66373bc3057358dcbb66651f0dd8c9
MD5 f18677c574a27c4adc108a6edac11e20
BLAKE2b-256 a5a2ca504671bf2d5dcc8a67de36e64b3f5be69bddffdc0c6dc7fa6892175ef2

See more details on using hashes here.

File details

Details for the file prettylogx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: prettylogx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for prettylogx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 553614bdc719a084f231c551bd004e735e3aba48c4e0074da360afc0d39086fe
MD5 0edbe1c856d377caf1c8d98b076ef810
BLAKE2b-256 48257d4f767e579fab6355e0dbcafeecc4266e33c554c6d2a418c4c8d4d17804

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page