Merge pull request #2469 from acelaya-forks/feature/logs-encoding

Allow logs format to be configured as console or JSON
This commit is contained in:
Alejandro Celaya
2025-07-24 10:01:36 +02:00
committed by GitHub
8 changed files with 28 additions and 11 deletions

View File

@@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
* `mobile`: Will match any mobile devices with either Android or iOS. * `mobile`: Will match any mobile devices with either Android or iOS.
* [#2093](https://github.com/shlinkio/shlink/issues/2093) Add `REDIRECT_CACHE_LIFETIME` env var and corresponding config option, so that it is possible to set the `Cache-Control` visibility directive (`public` or `private`) when the `REDIRECT_STATUS_CODE` has been set to `301` or `308`. * [#2093](https://github.com/shlinkio/shlink/issues/2093) Add `REDIRECT_CACHE_LIFETIME` env var and corresponding config option, so that it is possible to set the `Cache-Control` visibility directive (`public` or `private`) when the `REDIRECT_STATUS_CODE` has been set to `301` or `308`.
* [#2323](https://github.com/shlinkio/shlink/issues/2323) Add `LOGS_FORMAT` env var and corresponding config option, to allow the logs generated by Shlink to be in console or JSON formats.
### Changed ### Changed
* [#2406](https://github.com/shlinkio/shlink/issues/2406) Remove references to bootstrap from error templates, and instead inline the very minimum required styles. * [#2406](https://github.com/shlinkio/shlink/issues/2406) Remove references to bootstrap from error templates, and instead inline the very minimum required styles.

View File

@@ -43,11 +43,11 @@
"pagerfanta/core": "^3.8", "pagerfanta/core": "^3.8",
"ramsey/uuid": "^4.7", "ramsey/uuid": "^4.7",
"shlinkio/doctrine-specification": "^2.2", "shlinkio/doctrine-specification": "^2.2",
"shlinkio/shlink-common": "dev-main#7469270 as 7.1", "shlinkio/shlink-common": "dev-main#2c9387c as 7.1",
"shlinkio/shlink-config": "^4.0", "shlinkio/shlink-config": "^4.0",
"shlinkio/shlink-event-dispatcher": "^4.2", "shlinkio/shlink-event-dispatcher": "^4.2",
"shlinkio/shlink-importer": "^5.6", "shlinkio/shlink-importer": "^5.6",
"shlinkio/shlink-installer": "dev-develop#7f9147b as 9.6", "shlinkio/shlink-installer": "dev-develop#1c775a9 as 9.6",
"shlinkio/shlink-ip-geolocation": "^4.3", "shlinkio/shlink-ip-geolocation": "^4.3",
"shlinkio/shlink-json": "^1.2", "shlinkio/shlink-json": "^1.2",
"spiral/roadrunner": "^2025.1", "spiral/roadrunner": "^2025.1",

View File

@@ -13,6 +13,7 @@ return [
'enabled_options' => [ 'enabled_options' => [
Option\Server\RuntimeConfigOption::class, Option\Server\RuntimeConfigOption::class,
Option\Server\MemoryLimitConfigOption::class, Option\Server\MemoryLimitConfigOption::class,
Option\Server\LogsFormatConfigOption::class,
Option\Database\DatabaseDriverConfigOption::class, Option\Database\DatabaseDriverConfigOption::class,
Option\Database\DatabaseNameConfigOption::class, Option\Database\DatabaseNameConfigOption::class,
Option\Database\DatabaseHostConfigOption::class, Option\Database\DatabaseHostConfigOption::class,

View File

@@ -23,11 +23,16 @@ use function Shlinkio\Shlink\Config\runningInRoadRunner;
return (static function (): array { return (static function (): array {
$isDev = EnvVars::isDevEnv(); $isDev = EnvVars::isDevEnv();
$common = [ $format = EnvVars::LOGS_FORMAT->loadFromEnv();
$buildCommonConfig = static fn (bool $addNewLine = false) => [
'level' => $isDev ? Level::Debug->value : Level::Info->value, 'level' => $isDev ? Level::Debug->value : Level::Info->value,
'processors' => [RequestIdMiddleware::class], 'processors' => [RequestIdMiddleware::class],
'line_format' => 'formatter' => [
'[%datetime%] [%extra.' . RequestIdMiddleware::ATTRIBUTE . '%] %channel%.%level_name% - %message%', 'type' => $format,
'add_new_line' => $addNewLine,
'line_format' =>
'[%datetime%] [%extra.' . RequestIdMiddleware::ATTRIBUTE . '%] %channel%.%level_name% - %message%',
],
]; ];
// In dev env or the docker container, stream Shlink logs to stderr, otherwise send them to a file // In dev env or the docker container, stream Shlink logs to stderr, otherwise send them to a file
@@ -39,16 +44,15 @@ return (static function (): array {
'Shlink' => $useStreamForShlinkLogger ? [ 'Shlink' => $useStreamForShlinkLogger ? [
'type' => LoggerType::STREAM->value, 'type' => LoggerType::STREAM->value,
'destination' => 'php://stderr', 'destination' => 'php://stderr',
...$common, ...$buildCommonConfig(),
] : [ ] : [
'type' => LoggerType::FILE->value, 'type' => LoggerType::FILE->value,
...$common, ...$buildCommonConfig(),
], ],
'Access' => [ 'Access' => [
'type' => LoggerType::STREAM->value, 'type' => LoggerType::STREAM->value,
'destination' => 'php://stderr', 'destination' => 'php://stderr',
'add_new_line' => ! runningInRoadRunner(), ...$buildCommonConfig(! runningInRoadRunner()),
...$common,
], ],
], ],

View File

@@ -30,13 +30,17 @@ jobs:
prefetch: 10 prefetch: 10
logs: logs:
encoding: console
mode: development mode: development
channels: channels:
http: http:
mode: 'off' # Disable logging as Shlink handles it internally mode: 'off' # Disable logging as Shlink handles it internally
server: server:
encoding: console
level: info level: info
metrics: metrics:
encoding: console
level: debug level: debug
jobs: jobs:
encoding: console
level: debug level: debug

View File

@@ -35,15 +35,16 @@ jobs:
prefetch: 10 prefetch: 10
logs: logs:
encoding: json encoding: console
mode: development mode: development
channels: channels:
http: http:
mode: 'off' # Disable logging as Shlink handles it internally mode: 'off' # Disable logging as Shlink handles it internally
server: server:
encoding: json encoding: console
level: info level: info
metrics: metrics:
level: panic level: panic
jobs: jobs:
encoding: console
level: panic level: panic

View File

@@ -28,11 +28,14 @@ jobs:
prefetch: 10 prefetch: 10
logs: logs:
encoding: ${LOGS_FORMAT:-console}
mode: production mode: production
channels: channels:
http: http:
mode: 'off' # Disable logging as Shlink handles it internally mode: 'off' # Disable logging as Shlink handles it internally
server: server:
encoding: ${LOGS_FORMAT:-console}
level: info level: info
jobs: jobs:
encoding: ${LOGS_FORMAT:-console}
level: debug level: debug

View File

@@ -91,6 +91,7 @@ enum EnvVars: string
case CORS_ALLOW_CREDENTIALS = 'CORS_ALLOW_CREDENTIALS'; case CORS_ALLOW_CREDENTIALS = 'CORS_ALLOW_CREDENTIALS';
case CORS_MAX_AGE = 'CORS_MAX_AGE'; case CORS_MAX_AGE = 'CORS_MAX_AGE';
case TRUSTED_PROXIES = 'TRUSTED_PROXIES'; case TRUSTED_PROXIES = 'TRUSTED_PROXIES';
case LOGS_FORMAT = 'LOGS_FORMAT';
/** @deprecated Use REDIRECT_EXTRA_PATH */ /** @deprecated Use REDIRECT_EXTRA_PATH */
case REDIRECT_APPEND_EXTRA_PATH = 'REDIRECT_APPEND_EXTRA_PATH'; case REDIRECT_APPEND_EXTRA_PATH = 'REDIRECT_APPEND_EXTRA_PATH';
@@ -196,6 +197,8 @@ enum EnvVars: string
self::CORS_ALLOW_CREDENTIALS => false, self::CORS_ALLOW_CREDENTIALS => false,
self::CORS_MAX_AGE => 3600, self::CORS_MAX_AGE => 3600,
self::LOGS_FORMAT => 'console',
default => null, default => null,
}; };
} }