Lines
100.00%
18 / 18
Methods
100.00%
3 / 3
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| createStdoutLogger | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 1 | ||
| createFormatter | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 3 | ||
| 16 | final readonly class LoggerFactory | |
| 17 | { | |
| 18 | private const string LINE_FORMAT = "%datetime% %level_name% %message% %context% %extra%\n"; | |
| 19 | ||
| 20 | private const string LINE_DATE_FORMAT = 'Y-m-d H:i:s.vP'; | |
| 21 | ||
| 22 | public function __construct( | |
| 23 | private string $logStreamUrl = 'php://stdout', | |
| 24 | private string $logFallbackStreamUrl = 'php://stderr', | |
| 25 | ) {} | |
| 26 | ||
| 27 | public function createStdoutLogger(LogLevel $level = LogLevel::Info, LogFormat $format = LogFormat::Line): LoggerInterface | |
| 28 | { | |
| 29 | $threshold = Level::fromName($level->name); | |
| 30 | // A failed write must not throw out of a log call, e.g. while the broker is stopping. | |
| 31 | $handler = new FallbackGroupHandler([ | |
| 32 | new StreamHandler($this->logStreamUrl, $threshold), | |
| 33 | new StreamHandler($this->logFallbackStreamUrl, $threshold), | |
| 34 | ]); | |
| 35 | $handler->setFormatter($this->createFormatter($format)); | |
| 36 | ||
| 37 | return new Logger('stewart') | |
| 38 | ->pushHandler($handler) | |
| 39 | ->pushProcessor(new ExceptionContextProcessor()) | |
| 40 | ->pushProcessor(new ProcessTimeZoneProcessor()); | |
| 41 | } | |
| 42 | ||
| 43 | private function createFormatter(LogFormat $format): FormatterInterface | |
| 44 | { | |
| 45 | return match ($format) { | |
| 46 | LogFormat::Json => new JsonFormatter(), | |
| 47 | LogFormat::Line => new StackTraceLineFormatter( | |
| 48 | format: self::LINE_FORMAT, | |
| 49 | dateFormat: self::LINE_DATE_FORMAT, | |
| 50 | allowInlineLineBreaks: true, | |
| 51 | ignoreEmptyContextAndExtra: true, | |
| 52 | ), | |
| 53 | }; | |
| 54 | } | |
| 55 | } |