{"id":2793,"library":"structlog-sentry","title":"structlog-sentry","description":"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.","status":"active","version":"2.2.1","language":"en","source_language":"en","source_url":"https://github.com/kiwicom/structlog-sentry","tags":["logging","sentry","structlog","structured logging","error monitoring"],"install":[{"cmd":"pip install structlog-sentry","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core dependency for structured logging.","package":"structlog","optional":false},{"reason":"Core dependency for Sentry integration. Requires sentry-sdk v2+ as of structlog-sentry v2.2.0.","package":"sentry-sdk","optional":false}],"imports":[{"symbol":"SentryProcessor","correct":"from structlog_sentry import SentryProcessor"}],"quickstart":{"code":"import sentry_sdk\nimport structlog\nimport logging\nimport os\n\nfrom structlog_sentry import SentryProcessor\n\nsentry_sdk.init(\n    dsn=os.environ.get('SENTRY_DSN', 'YOUR_SENTRY_DSN_HERE'),\n    # Optional: Disable Sentry SDK's default logging integration if structlog-sentry handles all Sentry logging\n    # integrations=[sentry_sdk.integrations.logging.LoggingIntegration(event_level=None, level=None)]\n)\n\nstructlog.configure(\n    processors=[\n        structlog.stdlib.add_logger_name,  # Optional, but recommended. Must be before SentryProcessor\n        structlog.stdlib.add_log_level,    # Required. Must be before SentryProcessor\n        SentryProcessor(event_level=logging.ERROR), # Sends events ERROR or higher to Sentry\n        structlog.stdlib.ProcessorFormatter.wrap_for_formatter,\n    ],\n    logger_factory=structlog.stdlib.LoggerFactory(),\n    wrapper_class=structlog.stdlib.BoundLogger,\n)\n\n# Standard library logging setup (optional, for non-structlog compatible handlers)\n# import sys\n# logging.basicConfig(\n#     format=\"%(message)s\",\n#     stream=sys.stdout,\n#     level=logging.INFO,\n# )\n\nlog = structlog.get_logger()\n\ntry:\n    1 / 0\nexcept ZeroDivisionError:\n    log.error(\"A division by zero error occurred!\", operation=\"calculate_ratio\", user_id=42)\n\nlog.info(\"User activity log.\", action=\"login\", username=\"testuser\")","lang":"python","description":"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`."},"warnings":[{"fix":"Replace `SentryProcessor(hub=...)` with `SentryProcessor(scope=...)`.","message":"In `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`).","severity":"breaking","affected_versions":">=2.2.0"},{"fix":"Update `as_extra=True/False` to `as_context=True/False` in `SentryProcessor` initialization.","message":"As 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'.","severity":"breaking","affected_versions":">=2.0.0"},{"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.","message":"In `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.","severity":"breaking","affected_versions":">=2.0.0"},{"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)`.","message":"In `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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Adjust the order of processors in `structlog.configure()` to ensure `add_log_level` and `add_logger_name` come before `SentryProcessor`.","message":"The `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.","severity":"gotcha","affected_versions":"all"},{"fix":"Reorder processors: `SentryProcessor(...)`, then `structlog.processors.format_exc_info`.","message":"If 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.","severity":"gotcha","affected_versions":"all"},{"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()`.","message":"When 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.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}