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 details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: tiny_json_log-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.3 Linux/6.1.67-gentoo

File hashes

Hashes for tiny_json_log-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c3cf1ae8ab119461cd4db801d7c8957707b1a0e51845cc1c74f1a48224550314
MD5 55307c8513324b0c4d06a6542e797bfc
BLAKE2b-256 7a27b2312c97b7f2fb1314f6bc6b0f39b8283d209b3cf5650eb9f4c5d192c8c9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tiny_json_log-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.3 Linux/6.1.67-gentoo

File hashes

Hashes for tiny_json_log-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe1a4e9b56d83c14b45ba0ae18bdd811b202de91cf7c388fc777d9aad6bbde46
MD5 e18cfd05f4d81c95b3762d9cf7ec8403
BLAKE2b-256 7514dae80f8f66c011e32d869203d9a8a87016e2ccab1271ed41cb39e7078694

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