dereuromark / cakephp-whoops

Whoops Error Handler for CakePHP - PHP errors and exceptions for cool kids
MIT License
13 stars 6 forks source link

Exception logging no longer occurs on CakePHP 4.4+ #12

Open challgren opened 2 years ago

challgren commented 2 years ago

Using version 1.0.0 of this plugin along side CakePHP 4.4.1 with the updated Exception and Error traps. Exceptions are no longer logged. This occurs even when debug is false.

dereuromark commented 2 years ago

Any idea what needs to be done? I am currently not actively using this plugin

challgren commented 2 years ago

Looking at what cakephp/debug_kit does https://github.com/cakephp/debug_kit/blob/344fd1afe381e1901bda1a279955c1adf69d6b6d/src/Plugin.php#L97-L120 need to change to an Event listener. But I'm not sure as the new ErrorTrap and ExceptionTrap aren't very well documented and a lot of the docs reference old methods/solutions.

markstory commented 2 years ago

But I'm not sure as the new ErrorTrap and ExceptionTrap aren't very well documented and a lot of the docs reference old methods/solutions.

What kind of docs are you looking for? The book should have the basic usage scenarios covered.

challgren commented 2 years ago

But I'm not sure as the new ErrorTrap and ExceptionTrap aren't very well documented and a lot of the docs reference old methods/solutions.

What kind of docs are you looking for? The book should have the basic usage scenarios covered.

@markstory Well my use case is I want all logging/errors sent to papertrail which I can configure via monolog/monolog. With the Error/Exception trap stuff there's no longer documentation on using monolog/monolog.

markstory commented 2 years ago

With the Error/Exception trap stuff there's no longer documentation on using monolog/monolog.

Ok. I'm not familiar with how this plugin does logging, but for the core Error/Exception handling the default behavior is to log to Cake\Log\Log which is compatible with monolog adapters. Are you looking to replace that and log to papertrail more directly?

challgren commented 2 years ago

Well this plugin just captures errors/exceptions and displays them nicely. My use case is in production, I'm not really trying to use it more directly, I'm trying to understand why exceptions are no longer logged using Cake\Log\Log. We could take this convo to a different repo too. I guess my main issue with cakephp/cakephp is https://github.com/challgren/papertrail-logger/blob/main/src/Plugin.php and this repo no longer Log exceptions.

markstory commented 2 years ago

I'm not sure why you wouldn't be getting logs with the default error handling with 4.4, as the default error logger is ErrorLogger which uses Log::write() to write logs for errors and Log::error() for exceptions.

challgren commented 2 years ago

No its Exceptions that aren't being logged.

markstory commented 2 years ago

Ok, but both exceptions and errors use the same logger class in 4.4. Does your application define the Error.logger configuration value? You could also add more debugging to the cake libraries to figure out what is going on.

challgren commented 2 years ago

Yep the default from cakephp/app, copied it over after 4.4.0 was released. But if you install this plugin or challgren/papertrail-logger both do not log exceptions because of the changes. Yes I understand I can add more debugging to the libraries but when an exception is not capture its real hard to figure out whats going on. And thats why I say there needs to be more documentation. If you were to do

            Log::setConfig('default', function () {
                $formatter = new LineFormatter('[%datetime%] %channel%.%level_name%: %message%', 'Y-m-d H:i:s.v');

                $log = new Logger(strval(Configure::read('papertrail.channel', 'cakephp')));
                $sysLog = new SyslogUdpHandler(
                    strval(Configure::read('papertrail.host', env('PAPERTRAIL_HOST') ?? env('PAPERTRAIL_URL'))),
                    intval(Configure::read('papertrail.port', env('PAPERTRAIL_PORT'))),
                    LOG_USER,
                    Logger::DEBUG,
                    true,
                    strval(Configure::read('papertrail.ident', 'ident'))
                );
                $sysLog->setFormatter($formatter);
                $log->pushHandler($sysLog);

                return $log;
            });

            if (Configure::read('papertrail.drop', false)) {
                Log::drop('debug');
                Log::drop('error');
            }

You'd expect exceptions to be caught because I'm setting the default to be monolog/monolog and then I drop the debug and error Log configurations, expecting the default Log config to capture exceptions and errors.

markstory commented 2 years ago

I agree, I would expect that logger to be called when logging the error. You're sure that your custom logger is being created?

I'll try to make time later this week to reproduce with a similar setup to your plugin.

challgren commented 2 years ago

Yep 100% sure its configured as I am seeing regular Log messages that I create.

Screen Shot 2022-07-25 at 21 36 01

My controller action is

/**
     * Display error check testing
     *
     * @return void
     * @throws \Exception
     */
    public function errorCheck(): void
    {
        $this->log('Error Check: Emergency', LogLevel::EMERGENCY);
        $this->log('Error Check: Alert', LogLevel::ALERT);
        $this->log('Error Check: Critical', LogLevel::CRITICAL);
        $this->log('Error Check: Error', LogLevel::ERROR);
        $this->log('Error Check: Warning', LogLevel::WARNING);
        $this->log('Error Check: Notice', LogLevel::NOTICE);
        $this->log('Error Check: Info', LogLevel::INFO);
        $this->log('Error Check: Debug', LogLevel::DEBUG);
        $this->viewBuilder()->disableAutoLayout();
        deprecationWarning('Dont do this');

        throw new \Exception('Error Check: Exception');
    }
challgren commented 2 years ago

I agree, I would expect that logger to be called when logging the error. You're sure that your custom logger is being created?

I'll try to make time later this week to reproduce with a similar setup to your plugin.

Exception BTW, Errors are logged fine, its exceptions that are not logged.

markstory commented 2 years ago

I took a look at this further, and without this plugin installed, exception logging is working fine.

However, when this plugin is installed logging doesn't work because the implementation in this plugin doesn't call parent::handleException() nor does it call logException() on the ErrorHandler sub-class.

https://github.com/dereuromark/cakephp-whoops/blob/ad08ac481fb3c1ca8b401fda5baf9fbfb874853d/src/Error/Middleware/WhoopsHandlerMiddleware.php#L40

The comment above this line implies that not calling parent is intentional. I'm not sure exception logging from the middleware worked before 4.4.0 either as there would still be a missing parent call.

These problems might be resolvable if this plugin implemented an ExceptionRendererInterface instead of a middleware and handler. The renderer could be used with all versions of 4.x and would avoid the logging problem that is being encountered here, but I'm not the person to make that change.

LordSimal commented 1 year ago

Please take a look at https://github.com/dereuromark/cakephp-whoops/pull/13