Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception logging no longer occurs on CakePHP 4.4+ #12

Open
challgren opened this issue Jun 23, 2022 · 15 comments
Open

Exception logging no longer occurs on CakePHP 4.4+ #12

challgren opened this issue Jun 23, 2022 · 15 comments

Comments

@challgren
Copy link
Contributor

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
Copy link
Owner

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

@challgren
Copy link
Contributor Author

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
Copy link

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
Copy link
Contributor Author

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
Copy link

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
Copy link
Contributor Author

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
Copy link

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
Copy link
Contributor Author

No its Exceptions that aren't being logged.

@markstory
Copy link

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
Copy link
Contributor Author

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
Copy link

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link

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.

return parent::handleException($exception, $request);

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
Copy link
Contributor

Please take a look at #13

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

No branches or pull requests

4 participants