Skip to main content

Pretty Python JSON logs

Project description

pretty-json-loguru

Pretty Python JSON logs with loguru.

Basic usage

from loguru import logger
from pretty_json_loguru import configure_logger

configure_logger(level="DEBUG")

logger.debug("Hello", who="Friend!")

Why JSON logs?

  • Optimized for both developers and automated parsers
  • Load large logs into any JSON viewer to expand and inspect every field

How it looks

Vanilla loguru

Before

pretty-json-loguru

After

API

configure_logger

def configure_logger(
    level: str = "DEBUG",
    colorize: bool = True,
    include_traceback: bool = False,
    print_traceback_below: bool = True,
    indent: bool = False,
    remove_existing_sinks: bool = True,
    keys: List[LogKey] = ["ts", "msg", "source", "extra", "error", "traceback", "level"],
):
    """Configure the Loguru logger with JSON formatting.

    Args:
        level: Logging level. One of ["DEBUG", "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL"].
        colorize: Adds colors to the log.
        include_traceback: Adds "error" and "traceback" fields to JSON when exceptions occur.
        print_traceback_below: Prints full traceback below the JSON line.
        indent: Formats JSON with indentation for readability.
        remove_existing_sinks: Removes existing sinks.
        keys: Keys to include in the log. Available: "ts", "msg", "source", "extra", "error", "traceback", "level", "module", "function", "filename", "line", "process_name", "process_id", "thread_name", "thread_id", "name".
    """

create_json_formatter

def create_json_formatter(
    colorize: bool = True,
    include_traceback: bool = False,
    print_traceback_below: bool = True,
    indent: bool = False,
    keys: List[LogKey] = ["ts", "msg", "source", "extra", "error", "traceback", "level"],
):
    """Create a JSON formatter for Loguru with optional colorization.

    Args:
        colorize: Adds colors to the log.
        include_traceback: Adds "error" and "traceback" fields to JSON when exceptions occur.
        print_traceback_below: Prints full traceback below the JSON line.
        indent: Formats JSON with indentation for readability.
        keys: Keys to include in the log. Available: "ts", "msg", "source", "extra", "error", "traceback", "level", "module", "function", "filename", "line", "process_name", "process_id", "thread_name", "thread_id", "name".

    Returns:
        A function that formats a loguru log record as a colored JSON string.
    """

better_exceptions

Install better-exceptions for prettier tracebacks (used by default in loguru if installed)

License

MIT License

Author

Mark Lidenberg marklidenberg@gmail.com

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

pretty_json_loguru-0.3.2.tar.gz (45.6 kB view details)

Uploaded Source

Built Distribution

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

pretty_json_loguru-0.3.2-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file pretty_json_loguru-0.3.2.tar.gz.

File metadata

  • Download URL: pretty_json_loguru-0.3.2.tar.gz
  • Upload date:
  • Size: 45.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for pretty_json_loguru-0.3.2.tar.gz
Algorithm Hash digest
SHA256 9c0a3cfe39114bb196962f929babfdcb1b3e970cc6f03609c349a35ae340363c
MD5 85a367925528cab167d5c4afca9a97b4
BLAKE2b-256 fa9748d1b75bbcdb2359ac6afe39bbcc92db2fe573f4440c7146c76f936c254e

See more details on using hashes here.

File details

Details for the file pretty_json_loguru-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pretty_json_loguru-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a4ad06140a4d820b40448f4b90320039e34fbd8bb0c751782f8aa2326e15eca9
MD5 74cfcfe80a112d26bc5df01f0fc745ab
BLAKE2b-256 d5c4e1a350518836e40b1768b211bf7776c40295202f80ba8a8dc484d553095a

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