Capture / Log caught exceptions in whole application

Laravel 5.8 Php 7.1

I am currently maintaining a application with a large code base. The application is still under development.

My goal is to log all the exceptions that are thrown in the application, Also the ones that are caught in the try catch block. Due to the large code base i cannot add single lines of code in the catch blocks or create my custom exception class.

The way i tried to solve it or look for a sollution are:

Listen for class construction in the whole application

Override the Exception class (can’t do this because it is an core php class)

My most recent code that i tried is: My test exception (dashboardcontroller.php)

try {
        throw new \Exception('custom throwed exception');
    } catch (\Exception $e) {
        Log::info('Exception catched');
    }

Exceptions\Handler.php

public function report(Exception $exception)
    {
        Log::info($exception);
        parent::report($exception);
    }

Log:

[2020-03-10 17:19:09] local.INFO: Exception catched

So the question is: How can i log / handle thrown exceptions that are caught in the try catch block without adding code that requires lot of changes?

You dont need to override the exception class, you just need to log the meaningful info. If there is not logging being done in the catch block, you are going to have to add the lines that do this. Even if they are going to their own logs, those can be aggregated.

Sponsor our Newsletter | Privacy Policy | Terms of Service