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.3.tar.gz (2.2 kB view hashes)

Uploaded Source

Built Distribution

structured_logger-1.0.3-py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 3

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