Skip to main content

Write your logs as json-lines.

Source Code

aweber/json-scribe

Downloads

json-scribe @ pypi.org

This library is the result of sending docker container logs to loggly. Loggly works well with structured Syslog data but if you pass structured Syslog lines to an aggregator as JSON you end up with an unindexed mess. This library includes a few classes that make it easier and cleaner to send log content as JSON objects on single lines including exceptions.

Usage

Configuration

{
   "version": 1,
   "filters": {
      "defaultsetter": {
         "()": "jsonscribe.AttributeSetter",
         "add_fields": {
            "correlation_id": "ext://UUID"
         }
      }
   },
   "formatters": {
      "jsonlines": {
         "()": "jsonscribe.JSONFormatter",
         "include_fields": [
            "name",
            "levelname",
            "asctime",
            "message",
            "module",
            "correlation_id",
            "exc_info"
         ],
      }
   },
   "handlers": {
      "loggly": {
         "class": "logging.StreamHandler",
         "formatter": "jsonlines",
         "filters": ["defaultsetter"],
         "stream": "ext://sys.stdout"
      }
   },
   "loggers": {
      "somepackage": {
         "level": "DEBUG",
      }
   },
   "root": {
      "level": "INFO",
      "handlers": ["jsonlines"]
   }
}

Logging

The following snippet is the simplest usage. It is nothing more than the textbook usage of the logging module. It uses the logging configuration from above and generates a JSON blob.

import logging.config
import json

if __name__ == '__main__':
   config = json.load(open('config.json'))
   logging.config.dictConfig(config)
   logger = logging.getLogger(__package__).getChild('main')
   logger.info('processing request')

The JSON message looks something like the following. It is reformatted to make it readable. The default is to render it as compact JSON.

{
   "name": "somepackage.main",
   "levelname": "INFO",
   "asctime": "2018-08-09T07:44:54.231",
   "module": "somepackage.entrypoints",
   "correlation_id": "33CA3FB3-D66F-4D32-83A5-D5A8C3D92A6E",
   "message": "processing request",
   "exc_info": null
}

Release files for json-scribe 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-scribe 2.0.0
File Size Uploaded
json-scribe-2.0.0.tar.gz 9.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-scribe 2.0.0
File Interpreter ABI Platform
json_scribe-2.0.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 15.6 kB

Release files / json-scribe-2.0.0.tar.gz

Download URL json-scribe-2.0.0.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d3cfb1267dd4de61f7c3fe9a846427cac0bfc368d19d9ffb569ee46aef2b8d15
BLAKE2b-256 checksum
How to use checksums
dc8cdb949f599592942cb624a18c9773f3daeb79fd1e45a7e49bec41f0039632
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release files / json_scribe-2.0.0-py2.py3-none-any.whl

Download URL json_scribe-2.0.0-py2.py3-none-any.whl
Size 6.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d846bb7033ab4aab95a034935e6e460e10a4bf300a4ab67f64798954b3616351
BLAKE2b-256 checksum
How to use checksums
92a848c28ab9f2ae510c7b7881e3947dfa1db82cd7f0b730d7cf2c91ea462555
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page