How to inject $this->logger into routes.php in slim4

I want to do something logging like this
in some of the functions of routes.php i.e.

 $app->add(function ($request, $handler) {
        $response = $handler->handle($request);

But I’m getting server error:

"description": "ERROR: Undefined property: DI\\Container::$logger on line 26 in file \/var\/www\/html\/invAdminAPI\/app\/routes.php."

How to do this?

That hints helped me:

One more question:
how to “log” a $request inot a string ? or whatever $logger->info($request);
could manage?


You can log individual fields like the method and the URL as follows:

$method = $request->getMethod();
$url = (string)$request->getUri();
$this->logger->info(sprintf('%s %s', $method, $url));

Logging the entire $request object is not recommended because of circular references.

1 Like

thx, your are right, logging $request object doesn’t make senes.
But at least something like


is working , which under laravel didn’t. So the battle can go on…