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.
Here is the PHP-DI
config for logger:
LoggerInterface::class => static function (Container $container) {
$config = $container->get(Config::class);
$logger = new Logger((string)$config->get('logger.name'));
$fileNameSuffix = PHP_SAPI === 'cli' ? 'php-cli-' : 'php-fpm';
$logger->pushHandler(
new StreamHandler(
$config->get('logger.log_dir') . '/' . $fileNameSuffix . '-' . $config->get('logger.name') . '.log',
$config->get('logger.log_level'))
);
if ((bool)$config->get('main.debug')) {
$logger->pushHandler(new FirePHPHandler());
}
return $logger;
}
and here is the the ErrorMiddleware
config:
$definitions[ErrorMiddleware::class] = static function(ContainerInterface $container): ErrorMiddleware {
$middleware = new ErrorMiddleware(
$container->get(CallableResolverInterface::class),
$container->get(ResponseFactoryInterface::class),
(bool)$container->get(Config::class)->get('main.debug'), //false or true
$container->has(LoggerInterface::class) ? true : false,
$container->has(LoggerInterface::class) ? true : false
);
$middleware->setErrorHandler(HttpNotFoundException::class, $container->get(NotFoundHandler::class));
return $middleware;
};
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?