Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

realtebo's avatar

How to log to sentry when catching an Exception?

I'm actually catching exception when sending emails to be able to log incident to file, and doing more operations like rescheduling email sending.

BUT: I need this incident to be reported to Sentrry. Actually it's not logged to sentry because Exception is catched.

Can I manually log something to sentry?

0 likes
6 replies
tykus's avatar

You can send reportable exceptions to Sentry:

// app/Exceptions/Handler.php

public function report(Exception $exception)
{
    if (app()->bound('sentry') && $this->shouldReport($exception)){
        app('sentry')->captureException($exception);
    }

    parent::report($exception);
}
1 like
realtebo's avatar

I already have this function.

But catched exception are NOT reported because are not bubbling to log, obviously, I think.

I need (I'd like....) to be able to log to Sentry like I actually use Log::info

But your reply suggested me to try this

app('sentry')->captureException($exception);

I'll report esit.

realtebo's avatar

I confirm that we can call captureException in a catch module and everything works.

Also, I can use the same method to force a log to Sentry where I need even it is not an Exception.

Thanks

1 like
tykus's avatar

Cool.

I had assumed you meant that the framework was handling the Exception and not a try/catch.

realtebo's avatar

We added the common way Sentry to our Laravel project, so frameworkj is handling the not-catched exceptions sending the exception to sentry.

The problem was that in a few cases, we need both to handled the exception in the code with a try / catch AND to log it to sentry to evidence a rare situation we're still investigating. Our code is able to 'survide' to the exception, and it MUST survive, so we need the catch; but catched exception are not logged to sentry, and we need to log this particular exception to sentry to allow better investigation about the problem.

1 like

Please or to participate in this conversation.