#Sentry Not Capturing Python Logs

5 messages · Page 1 of 1 (latest)

charred crane
#

[SOLVED]Sentry not capturing Python logs as logs, only as breadcrumbs for error events
I've set up Sentry in my FastAPI application to capture both errors and logs. My configuration successfully captures exceptions and logger.error() calls as "Issues" in Sentry, and I can see my logger.info() messages attached as breadcrumbs to those issues.
However, none of my logs are appearing in the "Logs" tab in Sentry. I would expect logger.info() and logger.warning() calls to appear there as separate, searchable log entries.

My Goal:
I want all log records (INFO, WARNING, ERROR) to be sent to Sentry and be visible in the "Logs" section, in addition to having ERROR-level logs create "Issues".

Here is a reproducible example of my setup. This code correctly sends exceptions to Sentry, but the logs are missing.

sullen tusk
#

First of all you need to pass

    _experiments={
        "enable_logs": True,
    },

in your sentry.init call

#

Haven't looked at the code in detail but I see you're passing sentry_logs_level=None, and that might be wrong, please double check

charred crane
#

@sullen tusk Thanks for taking a look!
First of all, yes - I missed that part in my example

     _experiments={
        "enable_logs": True,
    },

However, in my actual project, the flag is set to ON, and unfortunately, it doesn't have any effect.

Here is the corrected version of Sentry init:

def _init_sentry(
        dsn: str | None,
        environment: str,
        denylist: Collection[str] = (),
        event_level: int = logging.ERROR,
        breadcrumb_level: int = logging.INFO,
) -> list[Integration]:
    """Initialize Sentry."""
    integrations = [
        ExcepthookIntegration(),
        AtexitIntegration(),
        LoggingIntegration(
            level=breadcrumb_level,
            event_level=event_level,
        ),
        FastApiIntegration(transaction_style="endpoint"),
    ]

    config = ENV_CONFIG.get(environment, ENV_CONFIG["default"])

    sentry_sdk.init(
        dsn=dsn,
        environment=environment,
        traces_sample_rate=config.get("traces_sample_rate", 0.0),
        profiles_sample_rate=config.get("profiles_sample_rate", 0.0),
        integrations=integrations,
        event_scrubber=EventScrubber(denylist=[*DEFAULT_DENYLIST, *denylist]),
        attach_stacktrace=True,
        send_default_pii=False,
        _experiments={
            "enable_logs": True,
        },
    )
    return integrations

Your second point about sentry_logs_level=None was double-checked, and I have tried both with and without. No effect as well.
For clarity, I'm using python=3.11 and sentry-sdk version = "2.26.0"

UPD: The issue was solved by bumping the Sentry SDK version:

name = "sentry-sdk"
version = "2.30.0"```