Skip to main content

A small wrapper around the standard Python logger

Project description

Structured Logger

This logger has been written in an attempt to provide structured logging around Python's default logging library. Potential uses include adding additional objects to be parsed and filtered by in an Elastic stack.

This library may be used if structured JSON log output is desired, without the potential overkill of a full implementation such as structlog. This isn't to take anything away from structlog, and for large projects it may still be a preferable option.

Usage

The logger should be used in a similar fashion to the Python logging package. Without additional context keys, it may be used as below:

from structured_logger import Logger

logger = Logger(__name__)
logger.set_level("DEBUG") # This could be set by an environment variable

def test_function():
    do_a_thing()
    logger.info("this is a typical logging message")

However, additional context may be output by passing a dict of parameters following the message:

from structured_logger import Logger

logger = Logger(__name__)
logger.set_level("DEBUG") # This could be set by an environment variable

def test_function():
    user_info = login()
    logger.info("user logged in", {"user": user_info})

Finally, the error() function will provide additional stack trace information by default, and exception information where appropriate:

from structured_logger import Logger

logger = Logger(__name__)
logger.set_level("DEBUG") # This could be set by an environment variable

def test_function():
    try:
        something_that_raises_a_value_error()
    except ValueError:
        logger.error("a value error has been thrown")

The above should output JSON with additional information in the fields errorName, errorMessage and errorStackTrace.

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

structured_logger-1.0.4.tar.gz (2.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

structured_logger-1.0.4-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file structured_logger-1.0.4.tar.gz.

File metadata

  • Download URL: structured_logger-1.0.4.tar.gz
  • Upload date:
  • Size: 2.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

File hashes

Hashes for structured_logger-1.0.4.tar.gz
Algorithm Hash digest
SHA256 ab49332b42ce5f2b6e1cf69094b2c5af06f79b32355f554ce5fd1d134ce889b9
MD5 627a689a76221eeff4e4b895167f166b
BLAKE2b-256 26e51e381e86d260aaaf7f4fbf35aee24433b428bc3ff1242f6df9d84de6266f

See more details on using hashes here.

File details

Details for the file structured_logger-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: structured_logger-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

File hashes

Hashes for structured_logger-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b4773ea5a99e6e72a092e7a69515fdf586ef1976312491eb91515710345fd9ae
MD5 84ed2ac3721d7bcc8d3cff9d031b7047
BLAKE2b-256 c09b42b00421dd089976713cd34958ec64e6180a01980bba18898e0a0a2dd3fd

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