Skip to main content

python-bunyan

Json logger compatible with node-bunyan

Get the code

git clone git@github.com:uphold/python-bunyan.git

Install

pip install bunyan

How to setup

Programatically

Create a new log handler and assign a BunyanFormatter formatter. Register the handler on the current logger.

import bunyan
import logging
import sys

logger = logging.getLogger()

logHandler = logging.StreamHandler(stream = sys.stdout)
formatter = bunyan.BunyanFormatter()
logHandler.setFormatter(formatter)
logger.addHandler(logHandler)
logger.setLevel(logging.DEBUG)

Using dictConfig

This is the same example as defined above, but using a dictionary with dictConfig.

LOG_CONFIG = {
  'formatters': {
    'bunyan': {
      '()' : 'bunyan.BunyanFormatter'
    }
  },
  'handlers': {
    'debug': {
      'class': 'logging.StreamHandler',
      'formatter': 'bunyan',
      'stream': 'ext://sys.stdout'
    },
  },

  'root': {
    'level': 'DEBUG',
    'handlers': ['debug']
  },
  'version': 1
}

import logging.config
logging.config.dictConfig(config)

How to use

After setting up your loggers, bunyan allows to log in two different ways: - Traditional - Using a dictionary

Traditional logging

String message

Traditionaly logging in python allows to log a string message.

logger.debug("This is a log message")

This will output:

{
  "name": "root",
  "pathname": "test.py",
  "levelname": "DEBUG",
  "msg": "This is a log message",
  "time": "2016-03-14T16:34:47Z",
  "hostname": "jalpedrinha-mbp.local",
  "level": 20,
  "pid": 41414,
  "v": 0
}

String message with extra dictionary

This module extends this functionality by allowing an extra keyword arg, and passing a dictionary.

logger.debug("This is a log message with extra context", extra = {'some': 'additional data'})

And the output will include some key and value:

{
  "name": "root",
  "time": "2016-03-14T16:36:12Z",
  "some": "additional data",
  "pathname": "test.py",
  "msg": "This is a log message with extra context",
  "levelname": "DEBUG",
  "hostname": "jalpedrinha-mbp.local",
  "level": 20,
  "pid": 41495,
  "v": 0
}

Dictionary

This method works similarly to using an extra dictionary without the string message, but instead of passing a keyword argument extra, you just pass the first positional argument as a dictionary.

logger.debug({'some': 'data'})

Which results in:

{
  "name": "root",
  "some": "data",
  "pathname": "test.py",
  "msg": "",
  "time": "2016-03-14T16:45:23Z",
  "levelname": "DEBUG",
  "hostname": "jalpedrinha-mbp.local",
  "level": 20,
  "pid": 43263,
  "v": 0
}

Testing

Docker-compose and tox

Run tox container:

docker-compose up

Natively

First, install dev-requirements

pip install -r dev-requirements.txt

Then run nose:

nosetests tests

Changelog

0.1.0 / 2016-03-15

  • #2 Add Bunyan Formatter (@jAlpedrinha)

Metadata

Release files for bunyan 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 bunyan 0.1.2
File Size Uploaded Provenance Actions
bunyan-0.1.2.tar.gz 8.0 kB Without provenance Details for bunyan-0.1.2.tar.gz

Release files / bunyan-0.1.2.tar.gz

Download URL bunyan-0.1.2.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e49e112b14cffa92c2a4e0e0052eac43a811f6ec594601f871b82355dc66432a
BLAKE2b-256 checksum
How to use checksums
becd661e57aca2dc0d1548112c65e6cccf185ea16421d95438bed2d28a0f138f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for bunyan 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file No provenance

0.1.0

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