Skip to main content

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

This project has been originally open sourced by exoscale (which is a great hosting service btw), thanks to them.

Installing

Pip (PyPI):

pip install logstash_formatter

Manual:

python setup.py install

Usage

Json outputs are provided by the LogstashFormatter logging formatter.

import logging
from logstash_formatter import LogstashFormatterV1

logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = LogstashFormatterV1()

handler.setFormatter(formatter)
logger.addHandler(handler)

The LogstashFormatter may take the following named parameters:

  • fmt: Config as a JSON string that supports:

    • extra: provide extra fields always present in logs.

    • source_host: override source host name.

  • json_cls: JSON encoder to forward to json.dump.

  • json_default: Default JSON representation for unknown types, by default coerce everything to a string.

LogstashFormatterV1 adheres to the more 1.2.0 schema and will not update fields, apart from a special handling of msg which will be updated to message when applicable.

You can also add extra fields to your json output by specifying a dict in place of message, or by specifying the named argument extra as a dictionary. When supplying the exc_info named argument with a truthy value, and if an exception is found on the stack, its traceback will be attached to the payload as well.

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

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

Sample output for LogstashFormatter

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 \"test.py\", line 16, in <module>\n    k['unknown']\n",
          "KeyError: 'unknown'\n"
      ],
      "filename": "test.py",
      "funcName": "<module>",
      "levelname": "WARNING",
      "levelno": 30,
      "lineno": 18,
      "module": "test",
      "msecs": 13.036966323852539,
      "name": "root",
      "pathname": "test.py",
      "process": 1819,
      "processName": "MainProcess",
      "relativeCreated": 18.002986907958984,
      "thread": 140060726359808,
      "threadName": "MainThread"
  },
  "@message": "TEST",
  "@source_host": "phoenix.spootnik.org",
  "@timestamp": "2013-05-02T09:39:48.013158"
}

Sample output for LogstashFormatterV1

The following keys will be found in the output JSON:

  • @timestamp: ISO 8601 timestamp

  • @version: Version of the schema

{"@version": 1,
 "account": "pyr",
 "lineno": 1,
 "levelno": 30,
 "filename": "test.py",
 "thread": 140566036444928,
 "@timestamp": "2015-03-30T09:46:23.000Z",
 "threadName": "MainThread",
 "relativeCreated": 51079.52117919922,
 "process": 10787,
 "source_host": "phoenix.spootnik.org",
 "processName": "MainProcess",
 "pathname": "test.py",
 "args": [],
 "module": "test",
 "msecs": 999.9005794525146,
 "created": 1427708782.9999006,
 "name": "root",
 "stack_info": null,
 "funcName": "<module>",
 "levelname": "WARNING",
 "message": "foo"}

Release files for logstash_formatter 0.5.17

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

Source distribution (sdist)

Source distribution for logstash_formatter 0.5.17
File Size Uploaded
logstash_formatter-0.5.17.tar.gz 4.7 kB Details

Release files / logstash_formatter-0.5.17.tar.gz

Download URL logstash_formatter-0.5.17.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
163f5ef62df5459f1d36dcb54e9c3f8f0c90157a324a3361887532b615bf4fb9
BLAKE2b-256 checksum
How to use checksums
7f6973c5b373ac656de0ee61175597d6739bd69bddcbc80894940cdb4237af8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.5.17 This release

1 release file

0.5.15

2 release files

0.5.14

1 release file

0.5.13

1 release file

0.5.12

1 release file

0.5.11

1 release file

0.5.9

1 release file

0.5.8

1 release file

0.5.7

1 release file

0.5.5

1 release file

0.5.4

1 release file

0.5.2

1 release file

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