I configured monolog in my slim 4 application and set logErrors and logErrorDetails to true in ErrorMIddleware but when I got an error it does not write logs. To emulate error I turn on the cache on my app and I’m getting an error like this Call to undefined function apcu_fetch() b-z doctrine using apcu by default to cache all metadata and apcu is not installed on my dev env.
And yes, I’m adding that middleware to the App like this $app->add($container->get(ErrorMiddleware::class));
I check different slim 4 skeleton Github repositories and I did not find any extra configurations, so I do not know why it’s not writing the logs to the files. Maybe I need to set a custom default error handler? As slim 4 default one using php error_log function to write the logs?
1 I set it with $app->add not $app->addErrorMiddleware
2 and 3. Handlers and logger works, as when I inject logger for example to controller and manually adding some logs it working (writing logs to the file) , it’s only not working for errors cathed by slim
4 thanks, I’ll fix that
as far as I can see the issue is because slim using default Slim\Handlers\ErrorHandler which is using error_log($error); and probably I need to override default handler to some custom one when defining middleware? But honestly, I do not like such approach, as for me it too much to write own handler for monolog
I think if you define a custom error logger, then the responsibility for logging is also delegated to your custom (NotFoundHandler) class. The parameters $displayErrorDetails, $logErrors, $logErrorDetails will be passed to your custom handler. Your error handler should declare the LoggerInterface or a LoggerFactory as dependency.
@odan So you agreed that Slim is not working with monolog or any other LoggerInterface implementation out of the box and I need to write custom handler(s) to add the ability of log every application error? And yeah in such a case if I want my NotFoundHandler to log not found info I need to inject logger to it (but it’s optionally). I was hope that slim log all errors by default no matter what handler I’m using, as slim trying to follow PSR and LoggerInterface is also PSR
The current implementation of the default ErrorHandler class uses error_log($error); which doesn’t work out of the box with the PSR-3 LoggerInterface. But you can add a custom default handler to it, which uses it’s own logger. This is a very flexible concept, but yeah… I know what you mean.
I was hope that slim log all errors by default no matter what handler I’m using
That’s the case. There are people who doesn’t even use a PSR-11 container. But Slim has to create an log entry even in such situations. By default it works for everyone, but if you have a specific requirement, you can create a custom handler (and logger) for your use case.
well, it’s probably possible to check if LoggerInterface is implemented and if yes use it, otherwise use error_log. Anyway thank you @odan for your help, I’m using Slim by the first time and was not sure if it’s I’m doing something wrong or not, documentation is really limited (maybe slim 3 has more detailed documentation). I understand it’s a micro framework but still is nice to have some more detailed documentation . Btw @odan I saw you have sleketon app on GitHub, it’s also using error_log for errors, or you made some custom handler?