Skip to main content

Yet another package to convert log messages to JSON.

Project description

Logging to JSON

Code style: black

Yet another log to JSON library for Python. I needed a library that integrated well with applications that mixed logging using built-in logging outputs and JSON output. I was also unable to find a library that worked with logging.QueueHandler Thus this library was born.

Install

pip install log-to-json

or

conda install log-to-json -c conda-forge

Example

Basic Usage

from log_to_json import JsonFormatter
import logging

logger = logging.getLogger()
handler = logging.StreamHandler()
json_formatter = JsonFormatter(keys=("name",))
handler.setFormatter(json_formatter)

logger.critical({"app-name": "my-app", "app_version": "1.0"})

# produces the log
{"app-name": "my-app", "app_version": "1.0", "name": "root"}

Slightly more advanced Usage

from log_to_json import JsonFormatter
import logging

logger = logging.getLogger()
handler = logging.StreamHandler()

def finalizer(message_dict):
    """rename name to logger_name"""
    message_dict["logger_name"] = message_dict.pop("name")
    return message_dict

json_formatter = JsonFormatter(
    keys=("message", "name", "user"),
    formatters={"user": str.upper},
    finalizer=finalizer
)
handler.setFormatter(json_formatter)

logger.critical("critical failure", extra={"user": "guest"})

# produces the message

{"message": "critical failure", "user": "GUEST", "logger_name": "root"}

Notable Features

Individual Key Formatters

Finalizer

Per Message Prefix

Integrates with non-JSON Logging

Pluggable Serializer

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

log-to-json-0.1.1.tar.gz (5.4 kB view hashes)

Uploaded source

Built Distribution

log_to_json-0.1.1-py3-none-any.whl (4.6 kB view hashes)

Uploaded py3

Supported by

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