Skip to main content

JSON / Logstash formatters for Python logging

Project description

JSON logging for Python PyPi version

This library provides Python logging formatters to output JSON, 2 formatters are specific for Logstash message format version 0 or 1.

Installation

Using pip:

pip install json-logging-py

From source:

python setup.py install

Usage

The name of the library is jsonlogging, it provides 3 formatters:

JSONFormatter

{
    "tags": [
        "env=prod",
        "role=www"
    ],
    "timestamp": "2015-09-22T22:40:56.178715Z",
    "level": "ERROR",
    "host": "server-01.example.com",
    "path": "example.py",
    "message": "hello world!",
    "logger": "root"
}

LogstashFormatterV0

{
    "@source": "JSON://server-01.example.com/example.py",
    "@source_host": "server-01.example.com",
    "@message": "hello world!",
    "@tags": [
        "env=prod",
        "role=www"
    ],
    "@fields": {
        "logger": "root",
        "levelname": "ERROR"
    },
    "@timestamp": "2015-09-22T22:42:02.094525Z",
    "@source_path": "example.py",
    "@type": "JSON"
}

LogstashFormatterV1

{
    "host": "server-01.example.com",
    "logger": "root",
    "type": "JSON",
    "tags": [
        "env=prod",
        "role=www"
    ],
    "path": "example.py",
    "@timestamp": "2015-09-22T22:43:11.966558Z",
    "@version": 1,
    "message": "hello world!",
    "levelname": "ERROR"
}

Python example

import logging
import jsonlogging


logger = logging.getLogger()

logHandler = logging.StreamHandler()

# You can also use LogstashFormatterV0 or LogstashFormatterV1
formatter = jsonlogging.JSONFormatter(
    hostname="server-01.example.com"
    tags=["env=prod", "role=www"],
    indent=4)
logHandler.setFormatter(formatter)

logger.addHandler(logHandler)

# You can pass additional tags
logger.error('hello world!', extra={"tags": ["hello=world"]})

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-logging-py-0.2.tar.gz (3.6 kB view details)

Uploaded Source

File details

Details for the file json-logging-py-0.2.tar.gz.

File metadata

File hashes

Hashes for json-logging-py-0.2.tar.gz
Algorithm Hash digest
SHA256 118b1fe1f4eacaea6370e5b9710d0f6d0c0a4599aef9d5b9875a6a579974fc9a
MD5 ba93b0595b2df3913ea9d41e7c4e2a7b
BLAKE2b-256 e9e146c70eebf216b830867c4896ee678cb7f1b28bb68a2810c7e9a811cecfbc

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