Skip to main content

JSON-based logging for the masses

Project description

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
}

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

json-scribe-2.0.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

json_scribe-2.0.0-py2.py3-none-any.whl (6.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file json-scribe-2.0.0.tar.gz.

File metadata

  • Download URL: json-scribe-2.0.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for json-scribe-2.0.0.tar.gz
Algorithm Hash digest
SHA256 d3cfb1267dd4de61f7c3fe9a846427cac0bfc368d19d9ffb569ee46aef2b8d15
MD5 2cba003503fc36f6bf31a1b98ba742d8
BLAKE2b-256 dc8cdb949f599592942cb624a18c9773f3daeb79fd1e45a7e49bec41f0039632

See more details on using hashes here.

File details

Details for the file json_scribe-2.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: json_scribe-2.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using 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

File hashes

Hashes for json_scribe-2.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d846bb7033ab4aab95a034935e6e460e10a4bf300a4ab67f64798954b3616351
MD5 1fbd94dc3aa4004958d1d0b68f403e98
BLAKE2b-256 92a848c28ab9f2ae510c7b7881e3947dfa1db82cd7f0b730d7cf2c91ea462555

See more details on using hashes here.

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