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

[^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.13.0.tar.gz (27.8 kB view details)

Uploaded Source

Built Distributions

structlog_sentry_logger-0.13.0-cp39-cp39-win_amd64.whl (85.5 kB view details)

Uploaded CPython 3.9 Windows x86-64

structlog_sentry_logger-0.13.0-cp39-cp39-manylinux_2_31_x86_64.whl (329.9 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.31+ x86-64

structlog_sentry_logger-0.13.0-cp39-cp39-macosx_10_16_x86_64.whl (89.2 kB view details)

Uploaded CPython 3.9 macOS 10.16+ x86-64

structlog_sentry_logger-0.13.0-cp38-cp38-win_amd64.whl (84.6 kB view details)

Uploaded CPython 3.8 Windows x86-64

structlog_sentry_logger-0.13.0-cp38-cp38-manylinux_2_31_x86_64.whl (322.7 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.31+ x86-64

structlog_sentry_logger-0.13.0-cp38-cp38-macosx_10_16_x86_64.whl (88.1 kB view details)

Uploaded CPython 3.8 macOS 10.16+ x86-64

structlog_sentry_logger-0.13.0-cp37-cp37m-win_amd64.whl (84.5 kB view details)

Uploaded CPython 3.7m Windows x86-64

structlog_sentry_logger-0.13.0-cp37-cp37m-manylinux_2_31_x86_64.whl (299.7 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.31+ x86-64

structlog_sentry_logger-0.13.0-cp37-cp37m-macosx_10_16_x86_64.whl (87.2 kB view details)

Uploaded CPython 3.7m macOS 10.16+ x86-64

File details

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

File metadata

  • Download URL: structlog-sentry-logger-0.13.0.tar.gz
  • Upload date:
  • Size: 27.8 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.13.0.tar.gz
Algorithm Hash digest
SHA256 a0193093c551130001f084e3fb4ab6e668f2e246bf1cd0d9f0ac081c094efc28
MD5 ff2cfa6acf5f474852e0db42c8becd07
BLAKE2b-256 7b5dba3948cba141b8c64349a6be9dafdb9d5a16a0342f9f2dff85fe4a342a7b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: structlog_sentry_logger-0.13.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 85.5 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.13.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 05138a7d058ee4f694238cccddc61ab78171592fc560d780dd2888ac04b85abe
MD5 713fda77d1e2f9aa2e881b30b0a524d7
BLAKE2b-256 ae831f61b027b77cb6f5f84611b77b12891f944e3988ce8d35c34049883da8ad

See more details on using hashes here.

File details

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

File metadata

  • Download URL: structlog_sentry_logger-0.13.0-cp39-cp39-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 329.9 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.13.0-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 453e903c0b749b9fbfdadc4ebd122aa54388e5ac2554ac86f890474eb92cd6b1
MD5 11e392b53296643113ceaf4a0beee059
BLAKE2b-256 6075078b449fcc0531261d458168aa54853c4f47b8ac4de7aae5ca41093cefb6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for structlog_sentry_logger-0.13.0-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm Hash digest
SHA256 75ba5310e12a57774248eec0d58ff1c4d3ddf5b73382d9147151b5f9edbe947e
MD5 1c96e9fe76fd684dd6079fd302343ecc
BLAKE2b-256 2f5489492523e50c8a50b7b455ac08b28ba9e02ec6f1a785d3adc15ffe6b5180

See more details on using hashes here.

File details

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

File metadata

  • Download URL: structlog_sentry_logger-0.13.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 84.6 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.13.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 f9a0aa3f2f5f866c671514ec1f79f4bb0d53108b22f769390afcffbfedc0a3eb
MD5 c249bbd4d86ce5922a9beb827ede798d
BLAKE2b-256 d66c104f7122751f1b10c3b273997883d930ecd89ac8946743ac9294980034f8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: structlog_sentry_logger-0.13.0-cp38-cp38-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 322.7 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.13.0-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 537992557521f00c8d010f8fbb40ec89bcdb5951a5da6f1feb21d1e8771be276
MD5 cf59ea2104f8f117052e3d4df0284bfb
BLAKE2b-256 37e3bb814cd66300ab1ff3ac860718af370bbc80923560d5d2374cfb7719d8b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for structlog_sentry_logger-0.13.0-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm Hash digest
SHA256 eb6844cfca8f43df540ed4f7f4594819920d1bb8adc9bb84822e2b085629f72c
MD5 d643a6a3fa9d2c2853029fa7aca68b0b
BLAKE2b-256 2575fc97180f76ca8e9c4ab43be98101c9090212cd37ade217bcfe0918fec20f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: structlog_sentry_logger-0.13.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 84.5 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.13.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 ef93a422c5326623f2c36a7f133b14da7b80700b05824f3b582c05e2ffc382b3
MD5 d5088364a705ddcdd8718948f800ee37
BLAKE2b-256 99db7d8a30dde6f088f9dbaf207e768d697c1d5b951a75aebd85e249f6654c60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for structlog_sentry_logger-0.13.0-cp37-cp37m-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 69b5c3f36666c17c8b265c70752b3f78dda1669169166a2b86433f97f44009a9
MD5 0fe6e074e012b891b430e60004332ac1
BLAKE2b-256 bad67191f5ecc603a4361b2cd69d553c9094455f4c7e2ea29440b95e393e01cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for structlog_sentry_logger-0.13.0-cp37-cp37m-macosx_10_16_x86_64.whl
Algorithm Hash digest
SHA256 b103ab6c5f939b652c8ac4053407d6bb0caed13ded5db72b7042cb031aaf8d9f
MD5 35f5c485269fb0dcdd1ba984d2fe09ba
BLAKE2b-256 729ff7b18804dc6d6b75f3ac82744f5ad31288e152d218137d236fa78ba85ec7

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