Skip to content

feat(monolog): Allow skipping Sentry issue reporting via 'sentry' => false log context - #2400

Open
giosh94mhz wants to merge 1 commit into
getsentry:masterfrom
giosh94mhz:feat/ignore-log-from-context
Open

giosh94mhz wants to merge 1 commit into
getsentry:masterfrom
giosh94mhz:feat/ignore-log-from-context

Conversation

@giosh94mhz

Copy link
Copy Markdown
Contributor

Description

Adds a simple per-record "kill switch" to the Monolog handlers that create Sentry issues: when a log record's context contains 'sentry' => false, the record is not captured as a Sentry issue.

$logger->warning('Invalid CSV row', ['row' => 42, 'sentry' => false]);

The record is still passed on to every other handler. That includes BreadcrumbHandler and LogsHandler, so it stays available as context: if a later record triggers an issue, this one appears in its breadcrumbs.

Only a strict false disables reporting. null, 0, '', 'false', etc. are ignored, so an issue can't be dropped by accident.

This probably needs a short mention in the Monolog docs and in the changelog.

Use case

some conditions should be logged at warning level or above, but the we already knows there is no bug or anomalies to report. For example, validation errors on user-provided data, such as a CSV import. Today you can only suppress them by building custom handlers, processors or before_send filters.

A context flag keeps the decision next to the log call, it's easy to implement and survive message logging changes.

…alse` log context

Records whose context contains `'sentry' => false` are no longer captured as
Sentry issues by `LogToSentryIssueHandler`, `ExceptionToSentryIssueHandler`
and the deprecated `Handler`. The record still reaches every other handler,
including `BreadcrumbHandler` and `LogsHandler`, so it remains available as
context if another record later triggers an issue.

This is useful for records that are worth logging at warning level or above
(e.g. validation errors on user-provided data) but are known, at the call
site, not to indicate a bug worth reporting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@giosh94mhz
giosh94mhz requested review from a team and stayallive as code owners November 19, 2026 11:24
@Litarnus

Litarnus commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the PR. I'm not sure if this is really required as a built-in feature. You could pretty easily get the same functionality by having a small before_send callback such as:

'before_send' => function (\Sentry\Event $event): ?\Sentry\Event {
    if ($event->getExtra()['monolog.context']['sentry'] === false) {
        return null;
    } 
    return $event;
}

edit: corrected to the proper before_send

@giosh94mhz

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestion! I tried it and it does work as a workaround, but a few caveats made me prefer a handler-level check — let me know what you think.

A monolog.context is only set by LogToSentryIssueHandler when fillExtraContext is enabled (off by default). So the callback needs that flag, which then sends the whole log context to Sentry for every issue.

Also, the event is fully built and then thrown away, even though the call site already knows it's not worth reporting. For frequent logs like validation warnings that's wasted work. And events dropped in before_send are recorded in client reports as before_send discards, so intentionally skipped records show up as dropped events in the stats.

In my opinion this is a very minor feature that improves quality of life, especially when migrating legacy code, much like @phpstan-ignore-next-line does for static analysis. Now that I think about it, a more explicit name for the flag could also be 'sentry_ignore' => true with the same semantics.

That said, if you'd rather not have it built in, I'm fine with closing this. I started the implementation because the before_send approach felt too clumsy in our codebase, and I preferred to invest some time in improving the shared code instead. If this isn't merged, maybe a small configuration example in the Monolog docs could help others.

@Litarnus

Copy link
Copy Markdown
Collaborator

Thanks for the explanation @giosh94mhz! I do agree with many points and I now think it makes sense to include it. While I believe it's a rather niche issue, it's still justified to have considering how small the PR is.

I personally don't have a strong preference towards the naming, but I'm slightly leaning towards 'sentry' => false because it's shorter. Do you have a preference @stayallive?

Thank you for opening the PR and starting the discussion, appreciate it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants