Skip to main content

A simple JSON logger, used for structured logging

Project description

yet-another-json-logger

Yet another JSON logger.

Complete with opinionated decisions and hardcoded personal preferences.

Usage

import logging

from yajl import JsonFormatter

json_handler = logging.FileHandler('log.json')
json_handler.setFormatter(JsonFormatter())

logger = logging.getLogger(__name__)
logger.addHandler(json_handler)
logger.setLevel(logging.INFO)

logger.info('Alice says %r', 'hi!')
try:
    1 / 0
except ZeroDivisionError:
    logger.exception('Bob divided by zero!')

Will result in log.json containing:

{"hostname": "alen-dev-vm", "pwd": "/mnt/alen/dev/yet-another-json-logger", "user": "alen", "name": "__main__", "module": "test", "level": {"name": "INFO", "number": 20}, "file": {"path": "/mnt/alen/dev/yet-another-json-logger/test.py", "filename": "test.py", "line": 12, "func": "<module>"}, "timestamp": {"abs": 1618609778.928479, "rel": 839.9991989135742}, "proc": {"id": 36424, "name": "MainProcess"}, "thread": {"id": 24544, "name": "MainThread"}, "message": "Alice says 'hi!'"}
{"hostname": "alen-dev-vm", "pwd": "/mnt/alen/dev/yet-another-json-logger", "user": "alen", "name": "__main__", "module": "test", "level": {"name": "ERROR", "number": 40}, "file": {"path": "/mnt/alen/dev/yet-another-json-logger/test.py", "filename": "test.py", "line": 16, "func": "<module>"}, "timestamp": {"abs": 1618609778.928479, "rel": 839.9991989135742}, "proc": {"id": 36424, "name": "MainProcess"}, "thread": {"id": 24544, "name": "MainThread"}, "message": "Bob divided by zero!\nTraceback (most recent call last):\n  File \"/mnt/alen/dev/yet-another-json-logger/test.py\", line 14, in <module>\n    1 / 0\nZeroDivisionError: division by zero", "exception": {"type": "builtins.ZeroDivisionError", "str": "division by zero", "traceback": "  File \"/mnt/alen/dev/yet-another-json-logger/test.py\", line 14, in <module>\n    1 / 0\n"}}

or, if prettified:

{
   "hostname": "alen-dev-vm",
   "pwd": "/mnt/alen/dev/yet-another-json-logger",
   "user": "alen",
   "name": "__main__",
   "module": "test",
   "level": {
      "name": "INFO",
      "number": 20
   },
   "file": {
      "path": "/mnt/alen/dev/yet-another-json-logger/test.py",
      "filename": "test.py",
      "line": 12,
      "func": "<module>"
   },
   "timestamp": {
      "abs": 1618609778.928479,
      "rel": 839.9991989135742
   },
   "proc": {
      "id": 36424,
      "name": "MainProcess"
   },
   "thread": {
      "id": 24544,
      "name": "MainThread"
   },
   "message": "Alice says 'hi!'"
}
{
   "hostname": "alen-dev-vm",
   "pwd": "/mnt/alen/dev/yet-another-json-logger",
   "user": "alen",
   "name": "__main__",
   "module": "test",
   "level": {
      "name": "ERROR",
      "number": 40
   },
   "file": {
      "path": "/mnt/alen/dev/yet-another-json-logger/test.py",
      "filename": "test.py",
      "line": 16,
      "func": "<module>"
   },
   "timestamp": {
      "abs": 1618609778.928479,
      "rel": 839.9991989135742
   },
   "proc": {
      "id": 36424,
      "name": "MainProcess"
   },
   "thread": {
      "id": 24544,
      "name": "MainThread"
   },
   "message": "Bob divided by zero!\nTraceback (most recent call last):\n  File \"/mnt/alen/dev/yet-another-json-logger/test.py\", line 14, in <module>\n    1 / 0\nZeroDivisionError: division by zero",
   "exception": {
      "type": "builtins.ZeroDivisionError",
      "str": "division by zero",
      "traceback": "  File \"/mnt/alen/dev/yet-another-json-logger/test.py\", line 14, in <module>\n    1 / 0\n"
   }
}

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

yet-another-json-logger-0.1.0.tar.gz (10.8 kB view hashes)

Uploaded Source

Built Distribution

yet_another_json_logger-0.1.0-py3-none-any.whl (5.8 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