Skip to content

Commit

Permalink
Improve logging usage
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioheleno committed Apr 23, 2024
1 parent 65e2915 commit b5456b3
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions app/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@
use Courier\Transport\AmqpTransport;
use DI\ContainerBuilder;
use League\Config\ConfigurationInterface;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger;
use Monolog\Processor\MemoryPeakUsageProcessor;
use Monolog\Processor\MemoryUsageProcessor;
use Monolog\Processor\PsrLogMessageProcessor;
use Monolog\Processor\UidProcessor;
use Monolog\Processor\WebProcessor;
use Nyholm\Dsn\DsnParser;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use PUGX\Poser\Poser;
use PUGX\Poser\Render\SvgFlatRender;
use PUGX\Poser\Render\SvgFlatSquareRender;
Expand Down Expand Up @@ -117,16 +124,42 @@
LoggerInterface::class => function (ContainerInterface $container): LoggerInterface {
/** @var \League\Config\ConfigurationInterface */
$config = $container->get(ConfigurationInterface::class);
if ((bool)$config->get('logging.enabled') === false) {
return new NullLogger();
}

$logger = new Logger('app');

$processor = new UidProcessor();
$logger->pushProcessor($processor);
if (PHP_SAPI !== 'cli') {
$logger->pushProcessor(new WebProcessor());
}

$logger
->pushProcessor(new MemoryPeakUsageProcessor())
->pushProcessor(new MemoryUsageProcessor())
->pushProcessor(new PsrLogMessageProcessor())
->pushProcessor(new UidProcessor());

// running from cli-server (dev mode), logs go to error_log()
if ($_ENV['PHP_ENV'] === 'dev' && PHP_SAPI === 'cli-server') {
$logger->pushHandler(
new ErrorLogHandler(
ErrorLogHandler::SAPI,
Level::Debug
)
);

return $logger;
}

$handler = new StreamHandler(
$config->get('logging.path'),
$config->get('logging.level')
// fallback, logs go to /path/to/logs/access.log
$logger->pushHandler(
new StreamHandler(
$config->get('logging.path'),
Level::fromName($config->get('logging.level'))
)
);

$logger->pushHandler($handler);

return $logger;
Expand Down

0 comments on commit b5456b3

Please sign in to comment.