I crate a container logger, and use it for addErrorMiddleware.
But how to reach this logger inside routes?
app.php:
use DI\Container;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
$container = new Container();
$container->set(‘logger’, function () {
$logger = new Logger(‘app’);
$file_handler = new StreamHandler(DIR . ‘/…/logs/app.log’);
$logger->pushHandler($file_handler);
return $logger;
});
AppFactory::setContainer($container);
$app = AppFactory::create();
…
$errorMiddleware = $app->addErrorMiddleware(true, true, true, $container->get(‘logger’));
require ‘routes.php’;
and routes.php:
use …
$app->group(’/v1’, function( Group $group ) {
$group->group(’/user’, function( Group $grp ) {
$grp->get(’/hello/{name}’, function( Request $request, Response $response, array $args ) {
$name = $args[‘name’];
// how to rich $container->get(‘logger’) ?
$response->getBody()->write(“Hello, $name”);
return $response;
});
…