# Problems with monolog and slim 4

**URL:** <https://discourse.slimframework.com/t/problems-with-monolog-and-slim-4/6063>\
**Category:** Questions\
**Created:** [February 27, 2025, 6:27am UTC](https://discourse.slimframework.com/t/problems-with-monolog-and-slim-4/6063 "2025-02-27T06:27:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DimionIp](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@DimionIp](https://discourse.slimframework.com/u/DimionIp)\
**Post date:** [February 27, 2025, 6:27am UTC](https://discourse.slimframework.com/t/problems-with-monolog-and-slim-4/6063/1 "2025-02-27T06:27:45Z")

</div>

I am installing Monolog in Slim 4, but it is not working correctly and throws errors. Here is the trace:

```auto
Uncaught TypeError: {closure:/home/e24/www/test.com/orders/index.php:228}():
Argument #1 ($request) must be of type ServerRequestInterface, 
Slim\Psr7\Request given, called in /home/e24/www/test.com/orders/slim/slim/slim/Slim/Middleware/ErrorMiddleware.php 
on line 92 and defined in /home/e24/www/test.com/orders/index.php:235

```

Stack trace:

```bash
#0 /home/e24/www/test.com/orders/slim/slim/slim/Slim/Middleware/ErrorMiddleware.php(92): {closure:/home/e24/www/test.com/orders/index.php:228}()
#1 /home/e24/www/test.com/orders/slim/slim/slim/Slim/Middleware/ErrorMiddleware.php(79): Slim\Middleware\ErrorMiddleware->handleException()
#2 /home/e24/www/test.com/orders/slim/slim/slim/Slim/MiddlewareDispatcher.php(129): Slim\Middleware\ErrorMiddleware->process()
#3 /home/e24/www/test.com/orders/slim/slim/slim/Slim/MiddlewareDispatcher.php(73): Psr\Http\Server\RequestHandlerInterface@anonymous->handle()
#4 /home/e24/www/test.com/orders/slim/slim/slim/Slim/App.php(209): Slim\MiddlewareDispatcher->handle()
#5 /home/e24/www/test.com/orders/slim/slim/slim/Slim/App.php(193): Slim\App->handle()
#6 /home/e24/www/test.com/orders/index.php(2207): Slim\App->run()
#7 {main} thrown in /home/e24/www/test.com/orders/index.php on line 235

```

Code in slim 4:

```php

//Optional: Define custom error logger
$logger = new Logger('error');
$logger -> pushHandler(new RotatingFileHandler('error.log'));

//Define Custom Error Handler
$custom_error_handler = function (
	ServerRequestInterface $request,
	Throwable $exception,
	bool $display_error_details,
	bool $log_errors,
	bool $log_error_details
)
use ($app, $logger) {
	if ($logger) {
		$logger -> error($exception -> getMessage());
	}

	$payload = ['error' => $exception -> getMessage()];

	$response = $app -> getResponseFactory() -> createResponse();
	$response -> getBody() -> write(
		json_encode($payload, JSON_UNESCAPED_UNICODE)
	);

	return $response;
	
};

// Add Error Middleware
$error_middleware = $app -> addErrorMiddleware(true, true, true, $logger);
$error_middleware -> setDefaultErrorHandler($custom_error_handler);

```

I want to create my own 404 error handling.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [March 6, 2025, 12:23pm UTC](https://discourse.slimframework.com/t/problems-with-monolog-and-slim-4/6063/2 "2025-03-06T12:23:24Z")

</div>

> [@DimionIp](#):
>
> `Argument #1 ($request) must be of type ServerRequestInterface,`

Did you add the using statement like this on top of your index.php file?  
Maybe this is just missing:

```php
use Psr\Http\Message\ServerRequestInterface;

```

---

<div class="post-metadata">

**Author:** ![DimionIp](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@DimionIp](https://discourse.slimframework.com/u/DimionIp)\
**Post date:** [March 6, 2025, 1:44pm UTC](https://discourse.slimframework.com/t/problems-with-monolog-and-slim-4/6063/3 "2025-03-06T13:44:31Z")

</div>

Well yes

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/d/dfe8173544edf3a8872df798adc82f08aedc9992.png)

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [March 7, 2025, 12:05pm UTC](https://discourse.slimframework.com/t/problems-with-monolog-and-slim-4/6063/4 "2025-03-07T12:05:11Z")

</div>

Not sure what is going on without seeing the full file. I would try this working example. Please give feedback if it works.

```php
<?php

require_once __DIR__. '/../vendor/autoload.php';

use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Exception\HttpNotFoundException;
use Slim\Factory\AppFactory;

$app = AppFactory::create();

$logger = new Logger('error');
$logger->pushHandler(new RotatingFileHandler('error.log'));

// Custom Error Handler
$custom_error_handler = function (
    ServerRequestInterface $request,
    Throwable $exception,
    bool $display_error_details,
    bool $log_errors,
    bool $log_error_details
) use ($app, $logger) {
    if ($logger) {
        $logger->error($exception->getMessage());
    }

    $statusCode = $exception instanceof HttpNotFoundException ? 404 : 500;
    $payload = ['error' => $exception->getMessage(), 'code' => $statusCode];

    $response = $app->getResponseFactory()->createResponse($statusCode);
    $response->getBody()->write(json_encode($payload, JSON_UNESCAPED_UNICODE));

    return $response->withHeader('Content-Type', 'application/json');
};

$app->addRoutingMiddleware();

$errorMiddleware = $app->addErrorMiddleware(true, true, true, $logger);
$errorMiddleware->setDefaultErrorHandler($custom_error_handler);

$app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) {
    throw new \RuntimeException('test message');
});

$app->run();

```

---

<div class="post-metadata">

**Author:** ![LLEGAZ](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llegaz/32/1665_2.png) [@LLEGAZ](https://discourse.slimframework.com/u/LLEGAZ)\
**Post date:** [March 10, 2025, 12:56pm UTC](https://discourse.slimframework.com/t/problems-with-monolog-and-slim-4/6063/5 "2025-03-10T12:56:05Z")

</div>

Your define the `ServerRequestInterface` as `Request`

Goofy guess:

Maybe change from

```php
$custom_error_handler = function (
	ServerRequestInterface $request,

```

to

```php
$custom_error_handler = function (
	Request $request,

```

---

<div class="post-metadata">

**Author:** ![LLEGAZ](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llegaz/32/1665_2.png) [@LLEGAZ](https://discourse.slimframework.com/u/LLEGAZ)\
**Post date:** [March 10, 2025, 1:41pm UTC](https://discourse.slimframework.com/t/problems-with-monolog-and-slim-4/6063/6 "2025-03-10T13:41:02Z")

</div>

Or rather consider removing the `as Request` in your use statement maybe it can conflict with the Request object.

Fun fact `Slim\Psr7\Request` does implement the `ServerRequestInterface` so this TypeError is really weird, no ?
