Skip to main content

JSON logstash formatter for logbook

Project description

JSON logs with logstash format for logbook

This library is provided to allow logbook logging to output log data as json objects ready to be shipped out to logstash.

This project is a fork of exoscale/python-logstash-formatter

Installing

Pip:

$ pip install logbook-logstash

Pypi:

https://pypi.python.org/pypi/logbook-logstash

Manual:

$ python setup.py install

Usage

Json outputs are provided by the LogstashFormatter logging formatter, for instance:

import sys

import logbook_logstash
from logbook import Logger, StreamHandler


log = Logger('testlog')

handler = StreamHandler(sys.stdout)
handler.formatter = logbook_logstash.LogstashFormatter()
handler.push_application()

log.info('My test')

You can provide extra variables, and show the exception traceback.

log.info({"account": 123, "ip": "172.20.19.18"})
log.info("classic message for account: %s", account, extra={"account": account})

try:
    h = {}
    h['key']
except:
    log.info("something unexpected happened", exc_info=True)

Sample output

The following keys will be found in the output JSON:

  • @source_host: source hostname for the log

  • @timestamp: ISO 8601 timestamp

  • @message: short message for this log

  • @fields: all extra fields

{
  "@fields": {
      "account": "pyr",
      "args": [],
      "created": 1367480388.013037,
      "exception": [
          "Traceback (most recent call last):\n",
          "  File \"toto.py\", line 16, in <module>\n    k['unknown']\n",
          "KeyError: 'unknown'\n"
      ],
      "filename": "toto.py",
      "funcName": "<module>",
      "levelname": "WARNING",
      "levelno": 30,
      "lineno": 18,
      "module": "toto",
      "msecs": 13.036966323852539,
      "name": "root",
      "pathname": "toto.py",
      "process": 1819,
      "processName": "MainProcess",
      "relativeCreated": 18.002986907958984,
      "thread": 140060726359808,
      "threadName": "MainThread"
  },
  "@message": "TOTO",
  "@source_host": "phoenix.spootnik.org",
  "@timestamp": "2013-05-02T09:39:48.013158"
}

Tests

This project has basic tests, and uses the pytest library. Just execute the following commands in the project root.

$ pip install -r dev-requirements.txt
$ py.test

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

logbook-logstash-0.1.3.tar.gz (3.7 kB view details)

Uploaded Source

File details

Details for the file logbook-logstash-0.1.3.tar.gz.

File metadata

File hashes

Hashes for logbook-logstash-0.1.3.tar.gz
Algorithm Hash digest
SHA256 46ca474750b01b9e6ae8296f352f885e2f7a4e204c5ae87fcadad5d0ec2945b1
MD5 31630c96806290e7393b5c655db170a1
BLAKE2b-256 f27d154d425f54facdebf9d8f96bd95c745663d093fcaa80c1b2808218fc2bfb

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