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 License PyPI - Python Version PyPI pre-commit Code style: black powered by semgrep


Documentation: https://structlog-sentry-logger.readthedocs.io

Source Code: https://github.com/TeoZosa/structlog-sentry-logger


:teacher: Overview

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

:sparkles: Features

  1. Makes logging as easy as using print statements, but prettier and easier to capture and filter!
  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!
  4. Cloud Logging compatible!

:confetti_ball: What You Get

:muscle: Powerful Automatic Context Fields

The pre-configured options include:

  1. :watch: Timestamps
    • DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  2. :vertical_traffic_light: Log levels
    • Added to the JSON context for filtering and categorization
  3. :mag: 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.

:zap: Performance

structlog-sentry-logger is C-compiled and fully-tuned, leveraging orjson as the JSON serializer for lightning-fast logging (more than a 4x speedup over Python's built-in JSON library[^1]; see here for sample performance benchmarks). Don't let your obligate cross-cutting concerns cripple performance any longer!

For further reference, see:

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

:robot: 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

:tada: Installation

pip install structlog-sentry-logger

:rocket: Usage

:loud_sound: 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")

:memo: 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"
}

:goal_net: 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[^2]:
# 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.:

import structlog_sentry_logger

LOGGER = structlog_sentry_logger.get_logger()

:outbox_tray: 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!

:cloud: Cloud Logging Compatibility

The logger will attempt to infer if an application is running in a cloud environment by inspecting for the presence of environment variables that may be automatically injected by cloud providers (namely, KUBERNETES_SERVICE_HOST, GCP_PROJECT, and GOOGLE_CLOUD_PROJECT).

If any of these environment variables are detected, log levels will be duplicated to a reserved severity key in the emitted logs to enable parsing of the log level and the remaining log context (as jsonPayload) by Cloud Logging (see: Cloud Logging: Structured logging).

:memo: ️Note
This behavior can also be manually enabled by adding the STRUCTLOG_SENTRY_LOGGER_CLOUD_LOGGING_COMPATIBILITY_MODE_ON variable to your environment, e.g., via a .env file[^2].

:warning:️ Warning
If a user manually specifies a value for the severity key, it will be overwritten! Avoid using this key if possible to preempt any future issues.

:chart_with_downwards_trend: 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 (assuming you are populating environment variables via a .env file[^2], as in the Sentry Integration section):

echo "STRUCTLOG_SENTRY_LOGGER_LOCAL_DEVELOPMENT_LOGGING_MODE_ON=" >> .env

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

Output_Formatting_example_0 Output_Formatting_example_1

[^2]: This library uses python-dotenv to automatically populate your environment with this variable (if it exists)

:wrench: Development

For convenience, implementation details of the below processes are abstracted away and encapsulated in single Make targets.

:fire: Tip
Invoking make without any arguments will display auto-generated documentation on available commands.

:building_construction: Package and Dependencies Installation

Make sure you have Python 3.7+ and poetry installed and configured.

To install the package and all dev dependencies, run:

make provision-environment

:fire: Tip
Invoking the above without poetry installed will emit a helpful error message letting you know how you can install poetry.

:package: Python Module to C-Extension Compilation

The projects's build.py file specifies which modules to package.

For manual per-module compilation, see: Mypyc Documentation: Getting started - Compiling and running

:white_check_mark: Testing

We use tox and pytest for our test automation and testing frameworks, respectively.

To invoke the tests, run:

make test

Run mutation tests to validate test suite robustness (Optional):

make test-mutations

:information_source: Technical Details
Test time scales with the complexity of the codebase. Results are cached in .mutmut-cache, so once you get past the initial cold start problem, subsequent mutation test runs will be much faster; new mutations will only be applied to modified code paths.

:rotating_light: Code Quality

We use pre-commit for our static analysis automation and management framework.

To invoke the analyses and auto-formatting over all version-controlled files, run:

make lint

:rotating_light: Danger
CI will fail if either testing or code quality fail, so it is recommended to automatically run the above locally prior to every commit that is pushed.

:arrows_counterclockwise: Automate via Git Pre-Commit Hooks

To automatically run code quality validation on every commit (over to-be-committed files), run:

make install-pre-commit-hooks

:warning:️ Warning
This will prevent commits if any single pre-commit hook fails (unless it is allowed to fail) or a file is modified by an auto-formatting job; in the latter case, you may simply repeat the commit and it should pass.

:memo: Documentation

make docs-clean docs-html

:fire: Tip
For faster feedback loops, this will attempt to automatically open the newly built documentation static HTML in your browser.

:clipboard: 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.

:books: Further Reading

:one: structlog: Structured Logging for Python

:two: Sentry: Monitor and fix crashes in realtime

:three: structlog-sentry: Provides the structlog integration for Sentry


:judge: Legal

:page_facing_up: License

Structlog-Sentry-Logger is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

:busts_in_silhouette: Credits

This project was generated from @TeoZosa's cookiecutter-cruft-poetry-tox-pre-commit-ci-cd template.

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.15.0.tar.gz (28.0 kB view details)

Uploaded Source

Built Distributions

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

structlog_sentry_logger-0.15.0-cp39-cp39-win_amd64.whl (86.2 kB view details)

Uploaded CPython 3.9Windows x86-64

structlog_sentry_logger-0.15.0-cp39-cp39-manylinux_2_31_x86_64.whl (331.8 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.31+ x86-64

structlog_sentry_logger-0.15.0-cp39-cp39-macosx_10_16_x86_64.whl (90.2 kB view details)

Uploaded CPython 3.9macOS 10.16+ x86-64

structlog_sentry_logger-0.15.0-cp38-cp38-win_amd64.whl (85.2 kB view details)

Uploaded CPython 3.8Windows x86-64

structlog_sentry_logger-0.15.0-cp38-cp38-manylinux_2_31_x86_64.whl (324.2 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.31+ x86-64

structlog_sentry_logger-0.15.0-cp38-cp38-macosx_10_16_x86_64.whl (89.0 kB view details)

Uploaded CPython 3.8macOS 10.16+ x86-64

structlog_sentry_logger-0.15.0-cp37-cp37m-win_amd64.whl (85.1 kB view details)

Uploaded CPython 3.7mWindows x86-64

structlog_sentry_logger-0.15.0-cp37-cp37m-manylinux_2_31_x86_64.whl (301.3 kB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.31+ x86-64

structlog_sentry_logger-0.15.0-cp37-cp37m-macosx_10_16_x86_64.whl (88.1 kB view details)

Uploaded CPython 3.7mmacOS 10.16+ x86-64

File details

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

File metadata

  • Download URL: structlog-sentry-logger-0.15.0.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for structlog-sentry-logger-0.15.0.tar.gz
Algorithm Hash digest
SHA256 711dace032d97e69dc1c90088608816cea95d4d55f3b7f53eb89c196d43db2ab
MD5 e1c81298f70aaec6a6c81c0c0407038e
BLAKE2b-256 47117b3f36ac77d849de3ed0e37bb64c24c1def3bf9315a611913a67de7dec83

See more details on using hashes here.

File details

Details for the file structlog_sentry_logger-0.15.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: structlog_sentry_logger-0.15.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 86.2 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for structlog_sentry_logger-0.15.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 cfdea428b0200f3ee1c231866b35d74028f36ec20debb36f981bd81bdc80ab24
MD5 0dd26e919730121f173a02b26287349e
BLAKE2b-256 3455c2edd25c9953f48f2e72554554440131314f799b121c7c851965e02e5bec

See more details on using hashes here.

File details

Details for the file structlog_sentry_logger-0.15.0-cp39-cp39-manylinux_2_31_x86_64.whl.

File metadata

  • Download URL: structlog_sentry_logger-0.15.0-cp39-cp39-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 331.8 kB
  • Tags: CPython 3.9, manylinux: glibc 2.31+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for structlog_sentry_logger-0.15.0-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 d31c0f8657ce5fe2cbb171d874a625f987f4f35118c07a2b9d65d44bd427802c
MD5 a7bf3cbb006e1829efe2766b2a6bcd9d
BLAKE2b-256 096db896d58ac843a678823b044b1882e0993d294ab86d8bfce60b0b86709d23

See more details on using hashes here.

File details

Details for the file structlog_sentry_logger-0.15.0-cp39-cp39-macosx_10_16_x86_64.whl.

File metadata

  • Download URL: structlog_sentry_logger-0.15.0-cp39-cp39-macosx_10_16_x86_64.whl
  • Upload date:
  • Size: 90.2 kB
  • Tags: CPython 3.9, macOS 10.16+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for structlog_sentry_logger-0.15.0-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm Hash digest
SHA256 a74ec383676ec67fa27d981c18251e3c9b681c7d163e7d22dbb5f1cc7f25fe6d
MD5 41826252c42756974b54726427fa7001
BLAKE2b-256 1632103d0818fc5b90a0e7a6d9d24383209cee20d368fbc91bb95be9f1e865e4

See more details on using hashes here.

File details

Details for the file structlog_sentry_logger-0.15.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: structlog_sentry_logger-0.15.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 85.2 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for structlog_sentry_logger-0.15.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 28efc734bd523e67ee60d5c139bf3d7003c0e518f76fed03adde01aa2536d952
MD5 fc860241ddef698ec859153ffe582d5c
BLAKE2b-256 88bc5fbd1e52f6eec3b59f6c91e1dcf19469a0094cf87e2465e061661eda346d

See more details on using hashes here.

File details

Details for the file structlog_sentry_logger-0.15.0-cp38-cp38-manylinux_2_31_x86_64.whl.

File metadata

  • Download URL: structlog_sentry_logger-0.15.0-cp38-cp38-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 324.2 kB
  • Tags: CPython 3.8, manylinux: glibc 2.31+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for structlog_sentry_logger-0.15.0-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 9354e10c3c7a07cf7cbb5ea8b02c3f2fdf38aa4cb7ec76d4895956e0c19ba816
MD5 55933b3c3b7887cddc87dea904b55e1b
BLAKE2b-256 c8fd9950bbf3c0a311298c8d476a7d2506442772b3e32e078b99cf9517fd9299

See more details on using hashes here.

File details

Details for the file structlog_sentry_logger-0.15.0-cp38-cp38-macosx_10_16_x86_64.whl.

File metadata

  • Download URL: structlog_sentry_logger-0.15.0-cp38-cp38-macosx_10_16_x86_64.whl
  • Upload date:
  • Size: 89.0 kB
  • Tags: CPython 3.8, macOS 10.16+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for structlog_sentry_logger-0.15.0-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm Hash digest
SHA256 0d2d14dda27bd9fcf1ae35e1981fdcd2d4526991702fcc20dec16828ae599070
MD5 8fc8db53eb3ead0d375ec267991f3951
BLAKE2b-256 f5c619e694df4633ac03346fafebd0388a0e3f65a57b330ff618fe2b4625f4e9

See more details on using hashes here.

File details

Details for the file structlog_sentry_logger-0.15.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: structlog_sentry_logger-0.15.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 85.1 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for structlog_sentry_logger-0.15.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 a675c86a9622008cd290bdd1c034d2a897e956f9cecb790aceb17f6be97b04c1
MD5 aebaae60507d6db95207b948de268dce
BLAKE2b-256 99dd243a7addd3c757df7e425ea5fd71544e756cae6d42c77dee0c4a3e8dafa1

See more details on using hashes here.

File details

Details for the file structlog_sentry_logger-0.15.0-cp37-cp37m-manylinux_2_31_x86_64.whl.

File metadata

  • Download URL: structlog_sentry_logger-0.15.0-cp37-cp37m-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 301.3 kB
  • Tags: CPython 3.7m, manylinux: glibc 2.31+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for structlog_sentry_logger-0.15.0-cp37-cp37m-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 ea895b0b22a74410a6a164bed54836ac7fb0b11c4e017ab7886c454c4a395eb9
MD5 1d063003242d4bcf205ba03ecaba6fea
BLAKE2b-256 bbc367e193522d9ac92d67d41ced16ab9dfe20552d8d9025ffd171fa0c1e76a9

See more details on using hashes here.

File details

Details for the file structlog_sentry_logger-0.15.0-cp37-cp37m-macosx_10_16_x86_64.whl.

File metadata

  • Download URL: structlog_sentry_logger-0.15.0-cp37-cp37m-macosx_10_16_x86_64.whl
  • Upload date:
  • Size: 88.1 kB
  • Tags: CPython 3.7m, macOS 10.16+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for structlog_sentry_logger-0.15.0-cp37-cp37m-macosx_10_16_x86_64.whl
Algorithm Hash digest
SHA256 9f4ce773bd20001b4c86d2b3b71c16d0f6ad18a6e6c21c1e694dc3a86bba130c
MD5 8fd5bfa66260db134c3323d39f45eeeb
BLAKE2b-256 3bc62e0f46a1fe4ab7bc7e3e3ccc5e4320b86e032a6eab8e8342c5bb44df848a

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