Skip to content

Commit

Permalink
Improvements to Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed May 24, 2024
1 parent d869a25 commit 7d62336
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 97 deletions.
2 changes: 1 addition & 1 deletion src/Discovery/LogHandlerDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 21 additions & 11 deletions src/Log/Channels/AppendLogChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
35 changes: 23 additions & 12 deletions src/Log/Channels/DailyLogChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
25 changes: 0 additions & 25 deletions src/Log/Channels/LogChannel.php

This file was deleted.

37 changes: 15 additions & 22 deletions src/Log/GenericLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<class-string, Monolog> */
private array $drivers = [];

public function __construct(
private LogConfig $logConfig,
private Container $container,
/** @var array<class-string, Monolog> $drivers */
private array $drivers = [],
private readonly LogConfig $logConfig,
) {

}

public function emergency(Stringable|string $message, array $context = []): void
Expand Down Expand Up @@ -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];
}
}
22 changes: 22 additions & 0 deletions src/Log/LogChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Tempest\Log;

use Monolog\Handler\HandlerInterface;
use Monolog\Level;
use Monolog\Processor\ProcessorInterface;

interface LogChannel
{
/**
* @return array<array-key, HandlerInterface>
*/
public function getHandlers(Level $level): array;

/**
* @return array<array-key, ProcessorInterface>
*/
public function getProcessors(): array;
}
7 changes: 0 additions & 7 deletions src/Log/LogConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<class-string<LogChannel>, array|string> */
public array $channelsConfig = [],
/** @var class-string<LogChannel> */
public string $channel = AppendLogChannel::class,
public string $prefix = 'tempest',
) {
}
Expand Down
1 change: 0 additions & 1 deletion src/Log/LoggerInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public function initialize(Container $container): LoggerInterface
{
return new GenericLogger(
$container->get(LogConfig::class),
$container,
);
}
}
24 changes: 6 additions & 18 deletions tests/Unit/Log/GenericLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');

Expand Down

0 comments on commit 7d62336

Please sign in to comment.