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.

Files for structured-logger, version 1.0.4
Filename, size File type Python version Upload date Hashes
Filename, size structured_logger-1.0.4-py3-none-any.whl (3.6 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size structured_logger-1.0.4.tar.gz (2.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page