Skip to main content

Log without the setup via a pre-configured structlog logger with optional Sentry integration

Project description

Structlog-Sentry-Logger

CI codecov pre-commit GitHub PyPI - Python Version PyPI Code style: black

A multi-purpose, pre-configured, performance-optimized structlog logger with (optional) Sentry integration via structlog-sentry.

Why

  1. Makes logging as easy as using print statements, but prettier and less smelly!
  2. Highly opinionated! There are only two (2) distinct configurations.
  3. Structured logs in JSON format means they are ready to be ingested by many of your favorite log analysis tools!

What You Get

Powerful Automatic Context Fields

The pre-configured options include:

  1. Timestamps
    • DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  2. Log levels
    • Added to the JSON context for filtering and categorization
  3. Logger names
    • Automatically assigned to namespaced versions of the initializing python modules (.py files), relative to your project directory.
      • e.g., the logger in docs_src/sentry_integration.py is named docs_src.sentry_integration

With fields sorted by key for easier at-a-glance analysis.

Performance

structlog-sentry-logger is fully-tuned and leverages ORJSON as the JSON serializer for lightning-fast logging (more than a 4x speedup over Python's built-in JSON library[1]). It's 2020, you don't have to let your obligate cross-cutting concerns cripple performance any longer!

For further reference, see:

[1] source: Choosing a faster JSON library for Python: Benchmarking

Built-in Sentry Integration (Optional)

Automatically add much richer context to your Sentry reports.

  • Your entire logging context is sent as a Sentry event when the structlog-sentry-logger log level is error or higher.
    • i.e., logger.error(""), logger.exception("")
  • See structlog-sentry for more details.

Table of Contents

Installation

pip install structlog-sentry-logger

Usage

Pure structlog Logging (Without Sentry)

At the top of your Python module, import and instantiate the logger:

import structlog_sentry_logger

LOGGER = structlog_sentry_logger.get_logger()

Now anytime you want to print anything, don't. Instead do this:

LOG_MSG = "Information that's useful for future me and others"
LOGGER.info(LOG_MSG, extra_field="extra_value")
Note: all the regular Python logging levels are supported.

Which automatically produces this:

{
    "event": "Information that's useful for future me and others",
    "extra_field": "extra_value",
    "level": "info",
    "logger": "docs_src.pure_structlog_logging_without_sentry",
    "sentry": "skipped",
    "timestamp": "2020-10-18 15:30:05"
}

Sentry Integration

Export your Sentry DSN into your local environment.

  • An easy way to do this is to put it into a local .env file and use python-dotenv to populate your environment:
# On the command line:
SENTRY_DSN=YOUR_SENTRY_DSN
echo "SENTRY_DSN=${SENTRY_DSN}" >> .env

Then load the .env file in your Python code prior to instantiating the logger, e.g.:

from dotenv import find_dotenv, load_dotenv
load_dotenv(find_dotenv())

import structlog_sentry_logger
LOGGER = structlog_sentry_logger.get_logger()

Log Custom Context Directly to Sentry

With structlog, you can even incorporate custom messages in your exception handling which will automatically be reported to Sentry (thanks to the structlog-sentry module):

import uuid

import structlog_sentry_logger

LOGGER = structlog_sentry_logger.get_logger()

curr_user_logger = LOGGER.bind(uuid=uuid.uuid4().hex)  # LOGGER instance with bound UUID
try:
    curr_user_logger.warn("A dummy error for testing purposes is about to be thrown!")
    x = 1 / 0
except ZeroDivisionError as err:
    ERR_MSG = (
        "I threw an error on purpose for this example!\n"
        "Now throwing another that explicitly chains from that one!"
    )
    curr_user_logger.exception(ERR_MSG)
    raise RuntimeError(ERR_MSG) from err
{
    "event": "A dummy error for testing purposes is about to be thrown!",
    "level": "warning",
    "logger": "docs_src.sentry_integration",
    "sentry": "skipped",
    "timestamp": "2020-10-18 15:29:55",
    "uuid": "181e0e00b9034732af4fed2b8424fb11"
}
{
    "event": "I threw an error on purpose for this example!\nNow throwing another that explicitly chains from that one!",
    "exception": 'Traceback (most recent call last):\n  File "/app/structlog-sentry-logger/docs_src/sentry_integration.py", line 10, in <module>\n    x = 1 / 0\nZeroDivisionError: division by zero',
    "level": "error",
    "logger": "docs_src.sentry_integration",
    "sentry": "sent",
    "sentry_id": null,
    "timestamp": "2020-10-18 15:29:55",
    "uuid": "181e0e00b9034732af4fed2b8424fb11"
}
Traceback (most recent call last):
  File "/app/structlog-sentry-logger/docs_src/sentry_integration.py", line 10, in <module>
    x = 1 / 0
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/app/structlog-sentry-logger/docs_src/sentry_integration.py", line 17, in <module>
    raise RuntimeError(ERR_MSG) from err
RuntimeError: I threw an error on purpose for this example!
Now throwing another that explicitly chains from that one!

Output: Formatting & Storage

The default behavior is to stream JSON logs directly to the standard output stream like a proper 12 Factor App.

For local development, it often helps to prettify logging to stdout and save JSON logs to a .logs folder at the root of your project directory for later debugging. To enable this behavior, set the following environment variable (e.g. via python-dotenv as in the Sentry Integration section):

CI_ENVIRONMENT_SLUG=dev-local

In doing so, with our previous exception handling example we would get:

Summary

That's it. Now no excuses. Get out there and program with pride knowing no one will laugh at you in production! For not logging properly, that is. You're on your own for that other observability stuff.

Further Reading

structlog: Structured Logging for Python

Sentry: Monitor and fix crashes in realtime.

structlog-sentry: Provides the structlog SentryProcessor for Sentry integration.

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

structlog-sentry-logger-0.5.5.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

structlog_sentry_logger-0.5.5-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file structlog-sentry-logger-0.5.5.tar.gz.

File metadata

  • Download URL: structlog-sentry-logger-0.5.5.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for structlog-sentry-logger-0.5.5.tar.gz
Algorithm Hash digest
SHA256 4effddfbbc9cfa1e20c740d2fadd8ac6104b497c194752b6d1b382758639335a
MD5 cbb4770d7469182c30df6531c892576c
BLAKE2b-256 a8ff62ab6c26364cc330341bb61f9547ed7e7c5182cd08bad2b6df6a3511cbe4

See more details on using hashes here.

File details

Details for the file structlog_sentry_logger-0.5.5-py3-none-any.whl.

File metadata

  • Download URL: structlog_sentry_logger-0.5.5-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for structlog_sentry_logger-0.5.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a7f6f44e44f5354d2f492f246b4fbf6201196fd6d1b18b4102e82bb914d541fc
MD5 7aa48bcd7748ab66da6d1ddf79554555
BLAKE2b-256 702282bdc298cb16d6198e6912e1017ab558614275b79692b007da94842769b4

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