Logger works but exceptions are not being logged to file, only "screen"


#1

I’m using what I believe are the default Monolog settings. Exceptions are being logged to the web page but not the log file. However if I explicitly generate a log entry, the file is updated, so I know the logger works. I’d like exceptions to be logged to a file so that I can debug them. Can somebody please help accomplish this?

settings.php
return [
‘settings’ => [
‘displayErrorDetails’ => true, // set to false in production
’addContentLengthHeader’ => false, // Allow the web server to send the content-length header

// Renderer settings
’renderer’ => [
‘template_path’ => DIR . ‘/…/templates/’,
],

// Monolog settings
’logger’ => [
‘name’ => ‘slim-app’,
‘path’ => DIR . ‘/…/logs/app.log’,
‘level’ => \Monolog\Logger::DEBUG,
],
],
];

dependencies.php
$container = $app->getContainer();

// view renderer
$container[‘renderer’] = function ($c) {
$settings = $c->get(‘settings’)[‘renderer’];
return new Slim\Views\PhpRenderer($settings[‘template_path’]);
};

// monolog
$container[‘logger’] = function ($c) {
$settings = $c->get(‘settings’)[‘logger’];
$logger = new Monolog\Logger($settings[‘name’]);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler($settings[‘path’], $settings[‘level’]));
return $logger;
};


#2

You need to write your own error handler as Slim core doesn’t know about your monolog instance.

See https://akrabat.com/logging-errors-in-slim-3/ for ideas.