Skip to main content

Python logging formatter for creating log entries in a JSON logstash-friendly format.

Supports renaming of python default logging fields to logstash friendly names (e.g: renaming asctime to @timestamp)

LogstashFormatter can receive the following arguments:

  • fmt, list or tuple containing the fields to include in each entry. Defaults to [“asctime”, “levelname”, “filename”, “funcName”, “msg”, “exc_info”].

  • datefmt, date format string to be passed to formatTime(). Defaults to ISO8601 time format.

  • rename, dictionary containing mapping of { key: new_key } to be renamed. Defaults to { “asctime”: “@timestamp” }.

  • version, version as for the @version attribute used in Logstash. Defaults to “1”.

Sample output

{
  "@timestamp": "2016-09-28T16:24:24,799",
  "@version": "1",
  "exc_info": null,
  "filename": "<ipython-input-21-de248ad5b09c>",
  "funcName": "<module>",
  "levelname": "INFO",
  "message": "This is a normal message to be logged"
}

Usage

Add LogstashFormatter as the formatter of your handler, as usual:

self.logger = logging.getLogger()
self.handler = logging.StreamHandler()
self.handler.setFormatter(LogstashFormatter())
self.logger.addHandler(self.handler)

Using in Django

Include logstash formatter in your settings file:

LOGGING = {
    'formatters': {
        'logstash': {
            '()': 'logstash_formatter.LogstashFormatter',
            'format': ("asctime", "levelname", "name", "lineno", "message",
                       "pathname", "module", "funcName", "process",),
            'datefmt': '%Y-%m-%dT%H:%M:%S',
            'rename': {
                'asctime': '@timestamp',
            },
            'version': '1'
        },
    }
}

Metadata

Release files for logstash-python-formatter 0.1.2

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-python-formatter 0.1.2
File Size Uploaded
logstash-python-formatter-0.1.2.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logstash-python-formatter 0.1.2
File Interpreter ABI Platform
logstash_python_formatter-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.0 kB

Release files / logstash-python-formatter-0.1.2.tar.gz

Download URL logstash-python-formatter-0.1.2.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
87730d89a69185b7ca54942a414a01c66a7f8f74f63bff57587de8fed6bf00a6
BLAKE2b-256 checksum
How to use checksums
8d74bf92e08db3dc5ecbc367aea5665b81ddfb03a4b22a497ef9159bb9cbaa77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

Release files / logstash_python_formatter-0.1.2-py3-none-any.whl

Download URL logstash_python_formatter-0.1.2-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
85534ada37a9c8580154fa0c955ca13bb6de62d86be134be46c9ee11b06aa6e4
BLAKE2b-256 checksum
How to use checksums
e5c559e378b435c19e9a910c4361afa001076a27764d77e0b4b3a120b2d98757
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

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