Skip to main content

Compact/easy to use json formatter

Project description

tiny-json-log

Compact and easy to use json logging lib

Usage

To write logs in json simply pass an instance of JSONFormatter class to your handler

import logging
from tiny_json_log import JSONFormatter

#You can use any other handler 
#for example FileHandler
handler = logging.StreamHandler()
handler.setFormatter(JSONFormatter())

#Here we use root logger but
#you can use any logger you wish
root = logging.getLogger()
root.addHandler(handler)
root.setLevel("INFO")

root.info("informative message")

this will print to stderr

{"severity": "INFO", "logger": "root", "message": "informative message"}

Json message

If your message string is in json format, library will convert it to json and nest it under message key

import logging
import json
import uuid
from tiny_json_log import JSONFormatter

log_dict = {
    "src": "module.some_class",
    "userid": f"{uuid.uuid4()}",
    "nested": {
        "some_nested_attr": 123
    }
}

handler = logging.StreamHandler()
handler.setFormatter(JSONFormatter())
root = logging.getLogger()
root.addHandler(handler)
root.setLevel("DEBUG")

root.info(json.dumps(log_dict))

this will print to stderr something like

{"severity": "INFO", "logger": "root", "message": {"src": "module.some_class", "userid": "32b8bf63-3958-4caa-b566-a500c898e429", "nested": {"some_nested_attr": 123}}}

you can also pass merge_message=True to JSONFormatter constructor and it will merge you JSON message with other logRecord attributes

import logging
import json
import uuid
from tiny_json_log import JSONFormatter

log_dict = {
    "src": "module.some_class",
    "userid": f"{uuid.uuid4()}",
    "nested": {
        "some_nested_attr": 123
    }
}

handler = logging.StreamHandler()
handler.setFormatter(JSONFormatter(merge_message=True))
root = logging.getLogger()
root.addHandler(handler)
root.setLevel("DEBUG")

root.info(json.dumps(log_dict))

will print

{"severity": "INFO", "logger": "root", "src": "module.some_class", "userid": "f10a8b6a-86c1-4280-8685-421ceea58811", "nested": {"some_nested_attr": 123}}

Formatting

You can also pass a format string to control what logRecord attributes are printed. Format string is a space separated list of logRecord attrs enclosed in {}. For example

import logging
from tiny_json_log import JSONFormatter


fmt = "{levelname} {message} {name}" 

handler = logging.StreamHandler()
handler.setFormatter(JSONFormatter(fmt))

root = logging.getLogger()
root.addHandler(handler)
root.setLevel("DEBUG")

root.error("informative message")

this will print to stderr

{"levelname": "ERROR", "message": "informative message", "name": "root"}

By default log key names are equal to logRecord attribute names. You can change this behaviour by specifing <custom attr key>={<some logRecord attr>} for example

import logging
import sys
from tiny_json_log import JSONFormatter


fmt = "lvl={levelname} {message} logger={name}"

handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(JSONFormatter(fmt))

root = logging.getLogger()
root.addHandler(handler)
root.setLevel("DEBUG")

root.info("informative message")

this will print to stdout

{"lvl": "INFO", "message": "informative message", "logger": "root"}

Default format string is severity={levelname} logger={name} {message}

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

tiny_json_log-0.1.0.tar.gz (4.2 kB view hashes)

Uploaded Source

Built Distribution

tiny_json_log-0.1.0-py3-none-any.whl (4.6 kB view hashes)

Uploaded Python 3

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