Skip to main content

Rich-formatted structured logging for Python with automatic syntax highlighting and beautiful console output.

Project description

richstructlog4

Structured logging with structlog, rendered for humans with rich.

richstructlog4 gives you:

  • Readable console output with timestamps, levels, and key/value fields.
  • Stream routing: WARNING and above go to stderr, everything below goes to stdout.
  • Optional file logging: if you pass log_file, all logs are also written to that file in a plain, ANSI-free format.

Requirements

  • Python: 3.8+
  • Dependencies: structlog, rich

Install

From PyPI:

pip install richstructlog4

From Git:

pip install git+https://github.com/MapIV/richstructlog4

Quick start

from richstructlog4 import Logger

log = Logger(log_level="INFO")

log.info("server started", host="127.0.0.1", port=8080)
log.warning("disk space is low", path="/var")

Console routing (stdout vs stderr)

  • stdout: levels below WARNING
  • stderr: WARNING and above

This is useful for CLI tools and scripts where you want to pipe normal output but keep warnings/errors separate.

File logging (plain text)

Pass log_file to also write every event to a file. The file output is plain text (no ANSI escape codes).

from richstructlog4 import Logger

log = Logger(log_file="app.log", log_level="DEBUG")
log.debug("debug goes to file")
log.info("info goes to stdout and file")
log.error("error goes to stderr and file")

Structured fields

Add extra fields as keyword arguments. They are shown as key/value pairs in console output and included in file logs.

log.info("user login", user_id="123", ip="192.168.1.1")
log.error("db connection failed", retry_count=3, database="users")

Rich markup and highlighting

Console output supports Rich markup in message strings.

log.info("Here are rich text markup examples:")
log.info(" - We are hiring. Visit our [link=https://map4.jp]website[/link]!")
log.warning(" - We are going to have a problem")
log.error(" - [bold red]ALERT![/bold red] Something happened")

![example rich markup](images/example-rich-markup.png)

It also highlights common patterns (HTML/XML, IPs, MACs, UUIDs, literals, numbers, paths, URLs).

log.info("The following texts are automatically highlighted:")
log.info(" - XML/HTML: `<tag>content</tag>`")
log.info(" - IP addresses: `192.168.1.1`, `2001:db8::1`")
log.info(" - MAC addresses: `00:1B:44:11:3A:B7`")
log.info(" - UUIDs: `123e4567-e89b-12d3-a456-426614174000`")
log.info(" - Python literals: `True`, `False`, `None`")
log.info(" - Numbers: `42`, `3.14`, `1+2j`")
log.info(" - Paths: `/usr/local/bin`")
log.info(" - URLs: `https://map4.jp`")

![example auto highlighting](images/example-auto-highlighting.png)

Development

This project uses uv for running tools.

uv run ruff check
uv run ty check
uv run pytest --cov

License

MIT

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

richstructlog4-1.0.1.tar.gz (126.5 kB view details)

Uploaded Source

Built Distribution

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

richstructlog4-1.0.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file richstructlog4-1.0.1.tar.gz.

File metadata

  • Download URL: richstructlog4-1.0.1.tar.gz
  • Upload date:
  • Size: 126.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for richstructlog4-1.0.1.tar.gz
Algorithm Hash digest
SHA256 0be0995b42e4a1d861874e3d68eec41bdceea7af406e68ad34a9b61640d4e83e
MD5 261b06e57a5f885f4ca81d772ea2b2fc
BLAKE2b-256 a29b1effa02ee68f6ca1ccca42733fff01b456c91592bd894c137bc84a637efa

See more details on using hashes here.

File details

Details for the file richstructlog4-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: richstructlog4-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for richstructlog4-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 47d70287ea87805b5d45e37d6b2887f2c8949d9d6dc9d947ed8646ce561ee27a
MD5 c68798e97a6cd91546445b9c5f2cbc24
BLAKE2b-256 31291ad27cecc7db3194bb3a6b3eefd78ee595b92a25c403780a92b7071aac34

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