Updated to monolog 3

This commit is contained in:
Alejandro Celaya 2022-06-04 08:59:17 +02:00
parent a20b99e643
commit 52a6d55e5d
6 changed files with 36 additions and 98 deletions

View File

@ -38,8 +38,6 @@
"mezzio/mezzio-problem-details": "^1.5", "mezzio/mezzio-problem-details": "^1.5",
"mezzio/mezzio-swoole": "^4.0", "mezzio/mezzio-swoole": "^4.0",
"mlocati/ip-lib": "^1.17", "mlocati/ip-lib": "^1.17",
"monolog/monolog": "^2.3",
"nikolaposa/monolog-factory": "^3.1",
"ocramius/proxy-manager": "^2.11", "ocramius/proxy-manager": "^2.11",
"pagerfanta/core": "^3.5", "pagerfanta/core": "^3.5",
"php-amqplib/php-amqplib": "^3.1", "php-amqplib/php-amqplib": "^3.1",
@ -47,9 +45,9 @@
"predis/predis": "^1.1", "predis/predis": "^1.1",
"pugx/shortid-php": "^1.0", "pugx/shortid-php": "^1.0",
"ramsey/uuid": "^4.2", "ramsey/uuid": "^4.2",
"shlinkio/shlink-common": "^4.4", "shlinkio/shlink-common": "dev-main#3244088 as 4.5",
"shlinkio/shlink-config": "^1.6", "shlinkio/shlink-config": "^1.6",
"shlinkio/shlink-event-dispatcher": "^2.3", "shlinkio/shlink-event-dispatcher": "^2.4",
"shlinkio/shlink-importer": "^3.0", "shlinkio/shlink-importer": "^3.0",
"shlinkio/shlink-installer": "^7.1", "shlinkio/shlink-installer": "^7.1",
"shlinkio/shlink-ip-geolocation": "^2.2", "shlinkio/shlink-ip-geolocation": "^2.2",

View File

@ -4,72 +4,36 @@ declare(strict_types=1);
namespace Shlinkio\Shlink; namespace Shlinkio\Shlink;
use Monolog\Formatter; use Monolog\Level;
use Monolog\Handler;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Processor;
use MonologFactory\DiContainerLoggerFactory;
use PhpMiddleware\RequestId; use PhpMiddleware\RequestId;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\Logger\LoggerFactory;
use Shlinkio\Shlink\Common\Logger\LoggerType;
use const PHP_EOL; $common = [
'level' => Level::Info->value,
$processors = [ 'processors' => [RequestId\MonologProcessor::class],
'exception_with_new_line' => [ 'line_format' => '[%datetime%] [%extra.request_id%] %channel%.%level_name% - %message%',
'name' => Common\Logger\Processor\ExceptionWithNewLineProcessor::class,
],
'psr3' => [
'name' => Processor\PsrLogMessageProcessor::class,
],
'request_id' => RequestId\MonologProcessor::class,
];
$formatter = [
'name' => Formatter\LineFormatter::class,
'params' => [
'format' => '[%datetime%] [%extra.request_id%] %channel%.%level_name% - %message%' . PHP_EOL,
'allow_inline_line_breaks' => true,
],
]; ];
return [ return [
'logger' => [ 'logger' => [
'Shlink' => [ 'Shlink' => [
'name' => 'Shlink', 'type' => LoggerType::FILE->value,
'handlers' => [ ...$common,
'shlink_handler' => [
'name' => Handler\RotatingFileHandler::class,
'params' => [
'level' => Logger::INFO,
'filename' => 'data/log/shlink_log.log',
'max_files' => 30,
'file_permission' => 0666,
],
'formatter' => $formatter,
],
],
'processors' => $processors,
], ],
'Access' => [ 'Access' => [
'name' => 'Access', 'type' => LoggerType::STREAM->value,
'handlers' => [ ...$common,
'access_handler' => [
'name' => Handler\StreamHandler::class,
'params' => [
'level' => Logger::INFO,
'stream' => 'php://stdout',
],
'formatter' => $formatter,
],
],
'processors' => $processors,
], ],
], ],
'dependencies' => [ 'dependencies' => [
'factories' => [ 'factories' => [
'Logger_Shlink' => [DiContainerLoggerFactory::class, 'Shlink'], 'Logger_Shlink' => [LoggerFactory::class, 'Shlink'],
'Logger_Access' => [DiContainerLoggerFactory::class, 'Access'], 'Logger_Access' => [LoggerFactory::class, 'Access'],
], ],
'aliases' => [ 'aliases' => [
'logger' => 'Logger_Shlink', 'logger' => 'Logger_Shlink',

View File

@ -2,33 +2,18 @@
declare(strict_types=1); declare(strict_types=1);
use Monolog\Handler\StreamHandler; use Monolog\Level;
use Monolog\Logger; use Shlinkio\Shlink\Common\Logger\LoggerType;
$isSwoole = extension_loaded('openswoole'); $isSwoole = extension_loaded('openswoole');
// For swoole, send logs to standard output
$handler = $isSwoole
? [
'name' => StreamHandler::class,
'params' => [
'level' => Logger::DEBUG,
'stream' => 'php://stdout',
],
]
: [
'params' => [
'level' => Logger::DEBUG,
],
];
return [ return [
'logger' => [ 'logger' => [
'Shlink' => [ 'Shlink' => [
'handlers' => [ // For swoole, send logs as stream
'shlink_handler' => $handler, 'type' => $isSwoole ? LoggerType::STREAM->value : LoggerType::FILE->value,
], 'level' => Level::Debug->value,
], ],
], ],

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory; use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Laminas\ServiceManager\Factory\InvokableFactory; use Laminas\ServiceManager\Factory\InvokableFactory;
use PhpMiddleware\RequestId; use PhpMiddleware\RequestId;
use Shlinkio\Shlink\Common\Logger\Processor\BackwardsCompatibleMonologProcessorDelegator;
return [ return [
@ -20,6 +21,11 @@ return [
RequestId\RequestIdMiddleware::class => ConfigAbstractFactory::class, RequestId\RequestIdMiddleware::class => ConfigAbstractFactory::class,
RequestId\MonologProcessor::class => ConfigAbstractFactory::class, RequestId\MonologProcessor::class => ConfigAbstractFactory::class,
], ],
'delegators' => [
RequestId\MonologProcessor::class => [
BackwardsCompatibleMonologProcessorDelegator::class,
],
],
], ],
ConfigAbstractFactory::class => [ ConfigAbstractFactory::class => [

View File

@ -8,8 +8,7 @@ use GuzzleHttp\Client;
use Laminas\ConfigAggregator\ConfigAggregator; use Laminas\ConfigAggregator\ConfigAggregator;
use Laminas\Diactoros\Response\EmptyResponse; use Laminas\Diactoros\Response\EmptyResponse;
use Laminas\ServiceManager\Factory\InvokableFactory; use Laminas\ServiceManager\Factory\InvokableFactory;
use Monolog\Handler\StreamHandler; use Monolog\Level;
use Monolog\Logger;
use PHPUnit\Runner\Version; use PHPUnit\Runner\Version;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@ -20,6 +19,7 @@ use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Report\Html\Facade as Html; use SebastianBergmann\CodeCoverage\Report\Html\Facade as Html;
use SebastianBergmann\CodeCoverage\Report\PHP; use SebastianBergmann\CodeCoverage\Report\PHP;
use SebastianBergmann\CodeCoverage\Report\Xml\Facade as Xml; use SebastianBergmann\CodeCoverage\Report\Xml\Facade as Xml;
use Shlinkio\Shlink\Common\Logger\LoggerType;
use function Laminas\Stratigility\middleware; use function Laminas\Stratigility\middleware;
use function Shlinkio\Shlink\Config\env; use function Shlinkio\Shlink\Config\env;
@ -76,16 +76,10 @@ $buildDbConnection = static function (): array {
}; };
}; };
$buildTestLoggerConfig = fn (string $handlerName, string $filename) => [ $buildTestLoggerConfig = static fn (string $filename) => [
'handlers' => [ 'level' => Level::Debug->value,
$handlerName => [ 'type' => LoggerType::STREAM->value,
'name' => StreamHandler::class, 'destination' => sprintf('data/log/api-tests/%s', $filename),
'params' => [
'level' => Logger::DEBUG,
'stream' => sprintf('data/log/api-tests/%s', $filename),
],
],
],
]; ];
return [ return [
@ -183,8 +177,8 @@ return [
], ],
'logger' => [ 'logger' => [
'Shlink' => $buildTestLoggerConfig('shlink_handler', 'shlink.log'), 'Shlink' => $buildTestLoggerConfig('shlink.log'),
'Access' => $buildTestLoggerConfig('access_handler', 'access.log'), 'Access' => $buildTestLoggerConfig('access.log'),
], ],
]; ];

View File

@ -4,22 +4,13 @@ declare(strict_types=1);
namespace Shlinkio\Shlink; namespace Shlinkio\Shlink;
use Monolog\Handler\StreamHandler; use Shlinkio\Shlink\Common\Logger\LoggerType;
use Monolog\Logger;
return [ return [
'logger' => [ 'logger' => [
'Shlink' => [ 'Shlink' => [
'handlers' => [ 'type' => LoggerType::STREAM->value,
'shlink_handler' => [
'name' => StreamHandler::class,
'params' => [
'level' => Logger::INFO,
'stream' => 'php://stdout',
],
],
],
], ],
], ],