diff --git a/src/Discovery/LogHandlerDiscovery.php b/src/Discovery/LogHandlerDiscovery.php index b90aa7f..4377da5 100644 --- a/src/Discovery/LogHandlerDiscovery.php +++ b/src/Discovery/LogHandlerDiscovery.php @@ -6,7 +6,7 @@ use ReflectionClass; use Tempest\Container\Container; -use Tempest\Log\Channels\LogChannel; +use Tempest\Log\LogChannel; use Tempest\Log\LogConfig; final readonly class LogHandlerDiscovery implements Discovery diff --git a/src/Log/Channels/AppendLogChannel.php b/src/Log/Channels/AppendLogChannel.php index af91bce..995160d 100644 --- a/src/Log/Channels/AppendLogChannel.php +++ b/src/Log/Channels/AppendLogChannel.php @@ -4,25 +4,35 @@ namespace Tempest\Log\Channels; -use Monolog\Handler\HandlerInterface; use Monolog\Handler\StreamHandler; use Monolog\Level; use Monolog\Processor\PsrLogMessageProcessor; +use Tempest\Log\LogChannel; -final class AppendLogChannel implements LogChannel +final readonly class AppendLogChannel implements LogChannel { - public function handler(Level $level, array $config): HandlerInterface + public function __construct( + private string $path, + private bool $bubble = true, + private ?int $filePermission = null, + private bool $useLocking = false, + ) { + } + + public function getHandlers(Level $level): array { - return new StreamHandler( - stream: $config['path'] ?? 'logs/tempest.log', - level: $level, - bubble: $config['bubble'] ?? true, - filePermission: $config['file_permission'] ?? null, - useLocking: $config['use_locking'] ?? false - ); + return [ + new StreamHandler( + stream: $this->path, + level: $level, + bubble: $this->bubble, + filePermission: $this->filePermission, + useLocking: $this->useLocking, + ), + ]; } - public function processor(array $config): array + public function getProcessors(): array { return [ new PsrLogMessageProcessor(), diff --git a/src/Log/Channels/DailyLogChannel.php b/src/Log/Channels/DailyLogChannel.php index 3ff8d32..e2a5559 100644 --- a/src/Log/Channels/DailyLogChannel.php +++ b/src/Log/Channels/DailyLogChannel.php @@ -4,26 +4,37 @@ namespace Tempest\Log\Channels; -use Monolog\Handler\HandlerInterface; use Monolog\Handler\RotatingFileHandler; use Monolog\Level; use Monolog\Processor\PsrLogMessageProcessor; +use Tempest\Log\LogChannel; -final class DailyLogChannel implements LogChannel +final readonly class DailyLogChannel implements LogChannel { - public function handler(Level $level, array $config): HandlerInterface + public function __construct( + private string $path, + private int $maxFiles = 30, + private bool $bubble = true, + private ?int $filePermission = null, + private bool $useLocking = false, + ) { + } + + public function getHandlers(Level $level): array { - return new RotatingFileHandler( - filename: $config['path'] ?? 'logs/tempest.log', - maxFiles: $config['rotation'] ?? 30, - level: $level, - bubble: $config['bubble'] ?? true, - filePermission: $config['file_permission'] ?? null, - useLocking: $config['use_locking'] ?? false - ); + return [ + new RotatingFileHandler( + filename: $this->path, + maxFiles: $this->maxFiles, + level: $level, + bubble: $this->bubble, + filePermission: $this->filePermission, + useLocking: $this->useLocking, + ), + ]; } - public function processor(array $config): array + public function getProcessors(): array { return [ new PsrLogMessageProcessor(), diff --git a/src/Log/Channels/LogChannel.php b/src/Log/Channels/LogChannel.php deleted file mode 100644 index e419887..0000000 --- a/src/Log/Channels/LogChannel.php +++ /dev/null @@ -1,25 +0,0 @@ -|HandlerInterface - */ - public function handler(Level $level, array $config): array|HandlerInterface; - - /** - * @return ProcessorInterface|array - */ - public function processor(array $config): array|ProcessorInterface; -} diff --git a/src/Log/GenericLogger.php b/src/Log/GenericLogger.php index 6b20cd7..b3c6d06 100644 --- a/src/Log/GenericLogger.php +++ b/src/Log/GenericLogger.php @@ -8,19 +8,15 @@ use Monolog\Logger as Monolog; use Psr\Log\LoggerInterface; use Stringable; -use Tempest\Container\Container; -use Tempest\Log\Channels\LogChannel; -use Tempest\Support\ArrayHelper; final class GenericLogger implements LoggerInterface { + /** @var array */ + private array $drivers = []; + public function __construct( - private LogConfig $logConfig, - private Container $container, - /** @var array $drivers */ - private array $drivers = [], + private readonly LogConfig $logConfig, ) { - } public function emergency(Stringable|string $message, array $context = []): void @@ -72,24 +68,21 @@ public function log($level, Stringable|string $message, array $context = []): vo private function writeLog(Level $level, string $message, array $context): void { - $this->resolveDriver($this->logConfig->channel, $level)->log($level, $message, $context); + foreach ($this->logConfig->channels as $channel) { + $this->resolveDriver($channel, $level)->log($level, $message, $context); + } } - private function resolveDriver(string $channelName, Level $level): Monolog + private function resolveDriver(LogChannel $channel, Level $level): Monolog { - if (isset($this->drivers[$channelName])) { - return $this->drivers[$channelName]; + if (! isset($this->drivers[$channel::class])) { + $this->drivers[$channel::class] = new Monolog( + name: $this->logConfig->prefix, + handlers: $channel->getHandlers($level), + processors: $channel->getProcessors(), + ); } - /** @var LogChannel $channel */ - $channel = $this->container->get($channelName); - - $config = $this->logConfig->channelsConfig[$channelName] ?? []; - - return $this->drivers[$channelName] = new Monolog( - name: $this->logConfig->prefix, - handlers: ArrayHelper::wrap($channel->handler($level, $config)), - processors: ArrayHelper::wrap($channel->processor($config)), - ); + return $this->drivers[$channel::class]; } } diff --git a/src/Log/LogChannel.php b/src/Log/LogChannel.php new file mode 100644 index 0000000..cf51bee --- /dev/null +++ b/src/Log/LogChannel.php @@ -0,0 +1,22 @@ + + */ + public function getHandlers(Level $level): array; + + /** + * @return array + */ + public function getProcessors(): array; +} diff --git a/src/Log/LogConfig.php b/src/Log/LogConfig.php index 92ef3bc..6037d41 100644 --- a/src/Log/LogConfig.php +++ b/src/Log/LogConfig.php @@ -4,18 +4,11 @@ namespace Tempest\Log; -use Tempest\Log\Channels\AppendLogChannel; -use Tempest\Log\Channels\LogChannel; - final class LogConfig { public function __construct( /** @var LogChannel[] */ public array $channels = [], - /** @var array, array|string> */ - public array $channelsConfig = [], - /** @var class-string */ - public string $channel = AppendLogChannel::class, public string $prefix = 'tempest', ) { } diff --git a/src/Log/LoggerInitializer.php b/src/Log/LoggerInitializer.php index edf1c99..21c4b05 100644 --- a/src/Log/LoggerInitializer.php +++ b/src/Log/LoggerInitializer.php @@ -16,7 +16,6 @@ public function initialize(Container $container): LoggerInterface { return new GenericLogger( $container->get(LogConfig::class), - $container, ); } } diff --git a/tests/Unit/Log/GenericLoggerTest.php b/tests/Unit/Log/GenericLoggerTest.php index 0bb2c6a..ee07adb 100644 --- a/tests/Unit/Log/GenericLoggerTest.php +++ b/tests/Unit/Log/GenericLoggerTest.php @@ -21,18 +21,12 @@ public function test_append_log_channel_works(): void $filePath = __DIR__ . '/logs/tempest.log'; $config = new LogConfig( - channelsConfig: [ - AppendLogChannel::class => [ - 'path' => $filePath, - ], + channels: [ + new AppendLogChannel($filePath), ], - channel: AppendLogChannel::class, ); - $logger = new GenericLogger( - $config, - $this->container, - ); + $logger = new GenericLogger($config); $logger->info('test'); @@ -46,18 +40,12 @@ public function test_daily_log_channel_works(): void $filePath = __DIR__ . '/logs/tempest-' . date('Y-m-d') . '.log'; $config = new LogConfig( - channelsConfig: [ - DailyLogChannel::class => [ - 'path' => __DIR__ . '/logs/tempest.log', - ], + channels: [ + new DailyLogChannel(__DIR__ . '/logs/tempest.log'), ], - channel: DailyLogChannel::class ); - $logger = new GenericLogger( - $config, - $this->container, - ); + $logger = new GenericLogger($config); $logger->info('test');