Registry / observability / structlog-sentry

structlog-sentry

JSON →
library2.2.1pypypi✓ verified 24d ago

structlog-sentry is a Python library that provides seamless integration between structlog, a structured logging library, and Sentry, an error tracking and performance monitoring platform. It allows users to send structured log events from their applications directly to Sentry, enhancing debugging and observability. The current version is 2.2.1, and the library is actively maintained with regular releases.

pip install structlog-sentry
INSTALL
IMPORT
SIG · STRUCTLOG-SENTRY
S
structlog-sentry
observabilitypythonv2.2.1
Install
2.6s avg
Import
502ms
Disk
23MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.2.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.534s · 24.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.6s · import 0.470s · 25MB
23MB installed
● package 23MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SentryProcessor
from structlog_sentry import SentryProcessor

This quickstart demonstrates how to configure `structlog` to use `SentryProcessor` to send structured logs and exceptions to Sentry. Ensure `sentry_sdk.init()` is called with your DSN and `structlog.stdlib.add_log_level` (and optionally `add_logger_name`) are placed before `SentryProcessor`.

import sentry_sdk import structlog import logging import os from structlog_sentry import SentryProcessor sentry_sdk.init( dsn=os.environ.get('SENTRY_DSN', 'YOUR_SENTRY_DSN_HERE'), # Optional: Disable Sentry SDK's default logging integration if structlog-sentry handles all Sentry logging # integrations=[sentry_sdk.integrations.logging.LoggingIntegration(event_level=None, level=None)] ) structlog.configure( processors=[ structlog.stdlib.add_logger_name, # Optional, but recommended. Must be before SentryProcessor structlog.stdlib.add_log_level, # Required. Must be before SentryProcessor SentryProcessor(event_level=logging.ERROR), # Sends events ERROR or higher to Sentry structlog.stdlib.ProcessorFormatter.wrap_for_formatter, ], logger_factory=structlog.stdlib.LoggerFactory(), wrapper_class=structlog.stdlib.BoundLogger, ) # Standard library logging setup (optional, for non-structlog compatible handlers) # import sys # logging.basicConfig( # format="%(message)s", # stream=sys.stdout, # level=logging.INFO, # ) log = structlog.get_logger() try: 1 / 0 except ZeroDivisionError: log.error("A division by zero error occurred!", operation="calculate_ratio", user_id=42) log.info("User activity log.", action="login", username="testuser")
Debug
Known issues
breakingIn `v2.2.0`, the `SentryProcessor` constructor changed from accepting a `hub` argument (for `sentry_sdk.Hub`) to a `scope` argument (for `sentry_sdk.Client` or `sentry_sdk.Scope`).
fix
Replace `SentryProcessor(hub=...)` with `SentryProcessor(scope=...)`.
affects: >=2.2.0
breakingAs of `v2.0.0`, the `as_extra` argument for `SentryProcessor` was renamed to `as_context` due to Sentry SDK's deprecation of 'extra' in favor of 'context'.
fix
Update `as_extra=True/False` to `as_context=True/False` in `SentryProcessor` initialization.
affects: >=2.0.0
breakingIn `v2.0.0`, the `SentryJSONProcessor` class was removed. Users previously relying on this for JSON output must now configure `SentryProcessor` and potentially disable the default `sentry-sdk` logging integration to prevent duplicate events.
fix
Remove `SentryJSONProcessor`. Use `SentryProcessor` and consider passing `integrations=[sentry_sdk.integrations.logging.LoggingIntegration(event_level=None, level=None)]` to `sentry_sdk.init()` for JSON logging setups.
affects: >=2.0.0
breakingIn `v2.0.0`, the `level` argument in `SentryProcessor` was renamed to `event_level` (controlling which log levels are sent to Sentry as events). A new `level` argument was introduced to control the log level of breadcrumbs.
fix
Rename `level` to `event_level` for event reporting. If breadcrumb level control is needed, use the new `level` argument: `SentryProcessor(event_level=logging.ERROR, level=logging.INFO)`.
affects: >=2.0.0
gotchaThe `structlog.stdlib.add_log_level` and `structlog.stdlib.add_logger_name` processors must be placed *before* `SentryProcessor` in your `structlog.configure` processor chain for `SentryProcessor` to correctly access and use these log attributes.
fix
Adjust the order of processors in `structlog.configure()` to ensure `add_log_level` and `add_logger_name` come before `SentryProcessor`.
affects: all
gotchaIf you are using `structlog.processors.format_exc_info`, place `SentryProcessor` *before* it in the processor chain. `format_exc_info` removes `exc_info` from the event dictionary, which `SentryProcessor` needs to correctly capture exception details.
fix
Reorder processors: `SentryProcessor(...)`, then `structlog.processors.format_exc_info`.
affects: all
gotchaWhen combining `structlog-sentry` with `sentry-sdk`'s default `LoggingIntegration` (which is often implicitly active), you might experience duplicate log events in Sentry, especially with JSON logging.
fix
Explicitly disable `sentry-sdk`'s `LoggingIntegration` when initializing the Sentry SDK by passing `integrations=[sentry_sdk.integrations.logging.LoggingIntegration(event_level=None, level=None)]` to `sentry_sdk.init()`.
affects: all
Upgrade
Version history
2.2.1latest on PyPI · released Nov 12, 2024
Audit
Dependencies
structlogrequiredCore dependency for structured logging.
sentry-sdkrequiredCore dependency for Sentry integration. Requires sentry-sdk v2+ as of structlog-sentry v2.2.0.
Agent activity
24 hits · last 30 days
node
20
OpenAI (training)
1
Resources