0
votes

I configured monolog in my slim 4 application and set logErrors and logErrorDetails to true in ErrorMIddleware but when I got an error it does not write logs. To emulate error I turn on the cache on my app and I'm getting an error like this Call to undefined function apcu_fetch() b-z doctrine using apcu by default to cache all metadata and apcu is not installed on my dev env.

Here is the PHP-DI config for logger:

           LoggerInterface::class => static function (Container $container) {
                $config = $container->get(Config::class);
                $logger = new Logger((string)$config->get('logger.name'));

                $fileNameSuffix = PHP_SAPI === 'cli' ? 'php-cli-' : 'php-fpm';
                $logger->pushHandler(
                    new StreamHandler(
                        $config->get('logger.log_dir') . '/' . $fileNameSuffix . '-' .    $config->get('logger.name') . '.log',
                        $config->get('logger.log_level'))
                );

                if ((bool)$config->get('main.debug')) {
                    $logger->pushHandler(new FirePHPHandler());
                }

                return $logger;
            }

and here is the the ErrorMiddleware config:

     $definitions[ErrorMiddleware::class] = static function(ContainerInterface $container): ErrorMiddleware {

        $middleware = new ErrorMiddleware(
            $container->get(CallableResolverInterface::class),
            $container->get(ResponseFactoryInterface::class),
            (bool)$container->get(Config::class)->get('main.debug'), //false or true
            $container->has(LoggerInterface::class) ? true : false,
            $container->has(LoggerInterface::class) ? true : false
        );
        $middleware->setErrorHandler(HttpNotFoundException::class, $container->get(NotFoundHandler::class));


        return $middleware;
    };

And yes, I'm adding that middleware to the App like this $app->add($container->get(ErrorMiddleware::class));

I check different slim 4 skeleton Github repositories and I did not find any extra configurations, so I do not know why it's not writing the logs to the files. Maybe I need to set a custom default error handler? As slim 4 default one using php error_log function to write the logs?

1

1 Answers

0
votes

This is how it configure the LoggerInterface in my dependencies.php. I use bzikarsky/gelf-php package to send the correct GELF format.

For local environment the StreamHandler, for any other environment the GelfHandler is used.

When using Graylog keep an eye on the exact port and protocol. In my example I use the UdpTransport. Feel free to use some other transport like TcpTransport.

dependencies.php

<?php
use Monolog\Logger;

// ...
LoggerInterface::class => function (ContainerInterface $c) {
    $config = $c->get(Configuration::class)->getArray('logger');
    $logger = new Logger($config['name']);

    if ($c->get(Configuration::class)->getString('settings.environment') === 'local') {
        $processor = new UidProcessor();
        $logger->pushProcessor($processor);

        $handler = new StreamHandler($config['path'], $config['level']);
    } else {
        $transport = new UdpTransport(
            $config['graylog']['host'],
            $config['graylog']['port'],
            UdpTransport::CHUNK_SIZE_LAN
        );

        $publisher = new Publisher();
        $publisher->addTransport($transport);
        $handler = new GelfHandler($publisher);
    }
    $logger->pushHandler($handler);

    return $logger;
},
// ...