diff --git a/composer.json b/composer.json index 719d392d..2bce658c 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,7 @@ "php-middleware/request-id": "^4.1", "pugx/shortid-php": "^1.1", "ramsey/uuid": "^4.7", - "shlinkio/shlink-common": "dev-main#29dd933 as 5.5", + "shlinkio/shlink-common": "dev-main#88a34f1 as 5.5", "shlinkio/shlink-config": "^2.4", "shlinkio/shlink-event-dispatcher": "dev-main#8c677ae as 3.0", "shlinkio/shlink-importer": "dev-main#6b63b12 as 5.1", diff --git a/config/autoload/logger.global.php b/config/autoload/logger.global.php index 2da1eda3..1820c480 100644 --- a/config/autoload/logger.global.php +++ b/config/autoload/logger.global.php @@ -4,51 +4,63 @@ declare(strict_types=1); namespace Shlinkio\Shlink; +use Laminas\ServiceManager\Factory\InvokableFactory; use Monolog\Level; use Monolog\Logger; use PhpMiddleware\RequestId; use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; use Shlinkio\Shlink\Common\Logger\LoggerFactory; use Shlinkio\Shlink\Common\Logger\LoggerType; +use Shlinkio\Shlink\Common\Middleware\AccessLogMiddleware; -$common = [ - 'level' => Level::Info->value, - 'processors' => [RequestId\MonologProcessor::class], - 'line_format' => '[%datetime%] [%extra.request_id%] %channel%.%level_name% - %message%', -]; +use function Shlinkio\Shlink\Config\runningInRoadRunner; -return [ +return (static function (): array { + $common = [ + 'level' => Level::Info->value, + 'processors' => [RequestId\MonologProcessor::class], + 'line_format' => '[%datetime%] [%extra.request_id%] %channel%.%level_name% - %message%', + ]; - 'logger' => [ - 'Shlink' => [ - 'type' => LoggerType::FILE->value, - ...$common, - ], - 'Access' => [ - 'type' => LoggerType::STREAM->value, - ...$common, - ], - ], + return [ - 'dependencies' => [ - 'factories' => [ - 'Logger_Shlink' => [LoggerFactory::class, 'Shlink'], - 'Logger_Access' => [LoggerFactory::class, 'Access'], - ], - 'aliases' => [ - 'logger' => 'Logger_Shlink', - Logger::class => 'Logger_Shlink', - LoggerInterface::class => 'Logger_Shlink', - ], - ], - - 'mezzio-swoole' => [ - 'swoole-http-server' => [ - 'logger' => [ - 'logger-name' => 'Logger_Access', - 'format' => '%u "%r" %>s %B', + 'logger' => [ + 'Shlink' => [ + 'type' => LoggerType::FILE->value, + ...$common, + ], + 'Access' => [ + 'type' => LoggerType::STREAM->value, + 'destination' => 'php://stderr', + 'add_new_line' => ! runningInRoadRunner(), + ...$common, ], ], - ], -]; + 'dependencies' => [ + 'factories' => [ + 'Logger_Shlink' => [LoggerFactory::class, 'Shlink'], + 'Logger_Access' => [LoggerFactory::class, 'Access'], + NullLogger::class => InvokableFactory::class, + ], + 'aliases' => [ + 'logger' => 'Logger_Shlink', + Logger::class => 'Logger_Shlink', + LoggerInterface::class => 'Logger_Shlink', + AccessLogMiddleware::LOGGER_SERVICE_NAME => 'Logger_Access', + ], + ], + + 'mezzio-swoole' => [ + 'swoole-http-server' => [ + 'logger' => [ + // Let's disable mezio-swoole access logging, so that we can provide our own implementation, + // consistent for roadrunner and openswoole + 'logger-name' => NullLogger::class, + ], + ], + ], + + ]; +})(); diff --git a/config/autoload/logger.local.php.dist b/config/autoload/logger.local.php.dist index 919f6cdb..fe2c8c54 100644 --- a/config/autoload/logger.local.php.dist +++ b/config/autoload/logger.local.php.dist @@ -5,16 +5,12 @@ declare(strict_types=1); use Monolog\Level; use Shlinkio\Shlink\Common\Logger\LoggerType; -use function Shlinkio\Shlink\Config\runningInOpenswoole; - -$logToStream = runningInOpenswoole(); - return [ 'logger' => [ 'Shlink' => [ - // For openswoole, send logs as stream - 'type' => $logToStream ? LoggerType::STREAM->value : LoggerType::FILE->value, + 'type' => LoggerType::STREAM->value, + 'destination' => 'php://stderr', 'level' => Level::Debug->value, ], ], diff --git a/config/autoload/middleware-pipeline.global.php b/config/autoload/middleware-pipeline.global.php index 25db6b7b..cb8045e9 100644 --- a/config/autoload/middleware-pipeline.global.php +++ b/config/autoload/middleware-pipeline.global.php @@ -9,6 +9,7 @@ use Mezzio\ProblemDetails; use Mezzio\Router; use PhpMiddleware\RequestId\RequestIdMiddleware; use RKA\Middleware\IpAddress; +use Shlinkio\Shlink\Common\Middleware\AccessLogMiddleware; use Shlinkio\Shlink\Common\Middleware\ContentLengthMiddleware; return [ @@ -16,6 +17,7 @@ return [ 'middleware_pipeline' => [ 'error-handler' => [ 'middleware' => [ + AccessLogMiddleware::class, ContentLengthMiddleware::class, RequestIdMiddleware::class, ErrorHandler::class, diff --git a/config/roadrunner/.rr.dev.yml b/config/roadrunner/.rr.dev.yml index cc0bbf29..9038c07e 100644 --- a/config/roadrunner/.rr.dev.yml +++ b/config/roadrunner/.rr.dev.yml @@ -31,7 +31,7 @@ logs: mode: development channels: http: - level: debug + mode: 'off' # Disable logging as Shlink handles it internally server: level: debug metrics: diff --git a/config/roadrunner/.rr.yml b/config/roadrunner/.rr.yml index d44801ee..b4074f96 100644 --- a/config/roadrunner/.rr.yml +++ b/config/roadrunner/.rr.yml @@ -31,6 +31,6 @@ logs: mode: production channels: http: - level: info # Log all http requests, set to info to disable + mode: 'off' # Disable logging as Shlink handles it internally server: level: debug # Everything written to worker stderr is logged diff --git a/config/test/test_config.global.php b/config/test/test_config.global.php index ac62f8a6..1beed0e3 100644 --- a/config/test/test_config.global.php +++ b/config/test/test_config.global.php @@ -121,6 +121,7 @@ $buildTestLoggerConfig = static fn (string $filename) => [ 'level' => Level::Debug->value, 'type' => LoggerType::STREAM->value, 'destination' => sprintf('data/log/api-tests/%s', $filename), + 'add_new_line' => true, ]; return [ diff --git a/docker/config/shlink_in_docker.local.php b/docker/config/shlink_in_docker.local.php index 9dc99351..2d5d6a06 100644 --- a/docker/config/shlink_in_docker.local.php +++ b/docker/config/shlink_in_docker.local.php @@ -6,14 +6,12 @@ namespace Shlinkio\Shlink; use Shlinkio\Shlink\Common\Logger\LoggerType; -use function Shlinkio\Shlink\Config\runningInRoadRunner; - return [ 'logger' => [ 'Shlink' => [ 'type' => LoggerType::STREAM->value, - 'destination' => runningInRoadRunner() ? 'php://stderr' : 'php://stdout', + 'destination' => 'php://stderr', ], ],