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.

Files for json-scribe, version 2.0.0
Filename, size File type Python version Upload date Hashes
Filename, size json_scribe-2.0.0-py2.py3-none-any.whl (6.0 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size json-scribe-2.0.0.tar.gz (9.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page