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

I want to do something logging like this
$this->logger->info($request);
in some of the functions of routes.php i.e.

 $app->add(function ($request, $handler) {
        $response = $handler->handle($request);
       $this-logger->info($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?

Hi!

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

error_log(print_r($request,true));

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