Skip to content

Commit 7f1451e

Browse files
committed
refactor: simply configuration
1 parent 0757d78 commit 7f1451e

File tree

9 files changed

+205
-47
lines changed

9 files changed

+205
-47
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Tempest\Log\Config;
4+
5+
use Tempest\Log\Channels\DailyLogChannel;
6+
use Tempest\Log\LogConfig;
7+
use Tempest\Log\LogLevel;
8+
9+
final class DailyLogConfig implements LogConfig
10+
{
11+
public array $channels {
12+
get => [
13+
new DailyLogChannel(
14+
path: $this->path,
15+
maxFiles: $this->maxFiles,
16+
minimumLogLevel: $this->minimumLogLevel,
17+
lockFilesDuringWrites: $this->lockFilesDuringWrites,
18+
bubble: $this->bubble,
19+
filePermission: $this->filePermission,
20+
),
21+
];
22+
}
23+
24+
/**
25+
* A logging configuration that creates a new log file each day and retains a maximum number of files.
26+
*
27+
* @param string $path The base log file name.
28+
* @param int $maxFiles The maximal amount of files to keep (0 means unlimited)
29+
* @param string $prefix A descriptive name attached to all log messages.
30+
* @param LogLevel $minimumLogLevel The minimum log level to record.
31+
* @param bool $lockFilesDuringWrites Whether to try to lock log file before doing any writes.
32+
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
33+
* @param null|int $filePermission Optional file permissions (default (0644) are only for owner read/write)
34+
*/
35+
public function __construct(
36+
private(set) string $path,
37+
private(set) string $prefix,
38+
private(set) int $maxFiles = 31,
39+
private(set) LogLevel $minimumLogLevel = LogLevel::DEBUG,
40+
private(set) bool $lockFilesDuringWrites = false,
41+
private(set) bool $bubble = true,
42+
private(set) ?int $filePermission = null,
43+
) {}
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Tempest\Log\Config;
4+
5+
use Tempest\Log\LogChannel;
6+
use Tempest\Log\LogConfig;
7+
8+
final class MultipleChannelsLogConfig implements LogConfig
9+
{
10+
/**
11+
* A logging configuration that uses multiple log channels.
12+
*
13+
* @param LogChannel[] $channels The log channels to which log messages will be sent.
14+
* @param string $prefix A descriptive name attached to all log messages.
15+
*/
16+
public function __construct(
17+
private(set) array $channels,
18+
private(set) string $prefix,
19+
) {}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Tempest\Log\Config;
4+
5+
use Tempest\Log\LogConfig;
6+
7+
final class NullLogConfig implements LogConfig
8+
{
9+
public array $channels {
10+
get => [];
11+
}
12+
13+
/**
14+
* A logging configuration that does not log anything.
15+
*/
16+
public function __construct(
17+
private(set) string $prefix = 'tempest',
18+
) {}
19+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Tempest\Log\Config;
4+
5+
use Tempest\Log\Channels\AppendLogChannel;
6+
use Tempest\Log\LogConfig;
7+
use Tempest\Log\LogLevel;
8+
9+
final class SimpleLogConfig implements LogConfig
10+
{
11+
public array $channels {
12+
get => [
13+
new AppendLogChannel(
14+
path: $this->path,
15+
useLocking: $this->useLocking,
16+
minimumLogLevel: $this->minimumLogLevel,
17+
bubble: $this->bubble,
18+
filePermission: $this->filePermission,
19+
),
20+
];
21+
}
22+
23+
/**
24+
* A basic logging configuration that appends all logs to a single file.
25+
*
26+
* @param string $path The log file path.
27+
* @param bool $useLocking Whether to try to lock log file before doing any writes.
28+
* @param LogLevel $minimumLogLevel The minimum log level to record.
29+
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
30+
* @param null|int $filePermission Optional file permissions (default (0644) are only for owner read/write).
31+
*/
32+
public function __construct(
33+
private(set) string $path,
34+
private(set) string $prefix,
35+
private(set) bool $useLocking = false,
36+
private(set) LogLevel $minimumLogLevel = LogLevel::DEBUG,
37+
private(set) bool $bubble = true,
38+
private(set) ?int $filePermission = null,
39+
) {}
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Tempest\Log\Config;
4+
5+
use Tempest\Log\Channels\WeeklyLogChannel;
6+
use Tempest\Log\LogConfig;
7+
use Tempest\Log\LogLevel;
8+
9+
final class WeeklyLogConfig implements LogConfig
10+
{
11+
public array $channels {
12+
get => [
13+
new WeeklyLogChannel(
14+
path: $this->path,
15+
maxFiles: $this->maxFiles,
16+
lockFilesDuringWrites: $this->lockFilesDuringWrites,
17+
minimumLogLevel: $this->minimumLogLevel,
18+
bubble: $this->bubble,
19+
filePermission: $this->filePermission,
20+
),
21+
];
22+
}
23+
24+
/**
25+
* A logging configuration that creates a new log file each week and retains a maximum number of files.
26+
*
27+
* @param string $path The base log file name.
28+
* @param int $maxFiles The maximal amount of files to keep.
29+
* @param string $prefix A descriptive name attached to all log messages.
30+
* @param LogLevel $minimumLogLevel The minimum log level to record.
31+
* @param bool $lockFilesDuringWrites Whether to try to lock log file before doing any writes.
32+
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not.
33+
* @param null|int $filePermission Optional file permissions (default (0644) are only for owner read/write).
34+
*/
35+
public function __construct(
36+
private(set) string $path,
37+
private(set) string $prefix,
38+
private(set) int $maxFiles = 5,
39+
private(set) LogLevel $minimumLogLevel = LogLevel::DEBUG,
40+
private(set) bool $lockFilesDuringWrites = false,
41+
private(set) bool $bubble = true,
42+
private(set) ?int $filePermission = null,
43+
) {}
44+
}

packages/log/src/LogConfig.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44

55
namespace Tempest\Log;
66

7-
final class LogConfig
7+
interface LogConfig
88
{
99
/**
10-
* @param LogChannel[] $channels
11-
* @param string $prefix A descriptive name attached to all log messages.
10+
* A descriptive name attached to all log messages.
1211
*/
13-
public function __construct(
14-
public array $channels = [],
15-
public string $prefix = 'tempest',
16-
) {}
12+
public string $prefix {
13+
get;
14+
}
15+
16+
/**
17+
* The log channels to which log messages will be sent.
18+
*
19+
* @var LogChannel[]
20+
*/
21+
public array $channels {
22+
get;
23+
}
1724
}

packages/log/src/TailLogsCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Tempest\Container\Tag;
1212
use Tempest\Highlight\Highlighter;
1313
use Tempest\Log\Channels\AppendLogChannel;
14+
use Tempest\Log\Channels\SimpleLogConfig;
1415
use Tempest\Log\LogConfig;
1516
use Tempest\Support\Filesystem;
1617

packages/log/src/logging.config.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55
namespace Tempest\Config;
66

77
use Tempest;
8-
use Tempest\Log\Channels\DailyLogChannel;
9-
use Tempest\Log\LogConfig;
8+
use Tempest\Log\Config\DailyLogConfig;
109

11-
return new LogConfig(
12-
channels: [
13-
new DailyLogChannel(
14-
path: Tempest\internal_storage_path('logs', 'tempest.log'),
15-
maxFiles: Tempest\env('LOG_MAX_FILES', default: 31),
16-
),
17-
],
10+
return new DailyLogConfig(
11+
path: Tempest\internal_storage_path('logs', 'tempest.log'),
12+
prefix: Tempest\env('APPLICATION_NAME', default: 'tempest'),
13+
maxFiles: Tempest\env('LOG_MAX_FILES', default: 31),
1814
);

tests/Integration/Log/GenericLoggerTest.php

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
use Tempest\DateTime\Duration;
1515
use Tempest\EventBus\EventBus;
1616
use Tempest\Log\Channels\AppendLogChannel;
17-
use Tempest\Log\Channels\DailyLogChannel;
18-
use Tempest\Log\Channels\WeeklyLogChannel;
17+
use Tempest\Log\Config\DailyLogConfig;
18+
use Tempest\Log\Config\MultipleChannelsLogConfig;
19+
use Tempest\Log\Config\NullLogConfig;
20+
use Tempest\Log\Config\SimpleLogConfig;
21+
use Tempest\Log\Config\WeeklyLogConfig;
1922
use Tempest\Log\GenericLogger;
20-
use Tempest\Log\LogConfig;
2123
use Tempest\Log\LogLevel;
2224
use Tempest\Log\MessageLogged;
2325
use Tempest\Support\Filesystem;
@@ -41,15 +43,11 @@ protected function cleanup(): void
4143
}
4244

4345
#[Test]
44-
public function append_log_channel(): void
46+
public function simple_log_config(): void
4547
{
4648
$filePath = __DIR__ . '/logs/tempest.log';
4749

48-
$config = new LogConfig(
49-
channels: [
50-
new AppendLogChannel($filePath),
51-
],
52-
);
50+
$config = new SimpleLogConfig($filePath, prefix: 'tempest');
5351

5452
$logger = new GenericLogger($config, $this->container->get(EventBus::class));
5553
$logger->info('test');
@@ -59,15 +57,11 @@ public function append_log_channel(): void
5957
}
6058

6159
#[Test]
62-
public function daily_log_channel(): void
60+
public function daily_log_config(): void
6361
{
6462
$clock = $this->clock();
6563
$filePath = __DIR__ . '/logs/tempest-' . date('Y-m-d') . '.log';
66-
$config = new LogConfig(
67-
channels: [
68-
new DailyLogChannel(__DIR__ . '/logs/tempest.log'),
69-
],
70-
);
64+
$config = new DailyLogConfig(__DIR__ . '/logs/tempest.log', prefix: 'tempest');
7165

7266
$logger = new GenericLogger($config, $this->container->get(EventBus::class));
7367
$logger->info('test');
@@ -85,14 +79,10 @@ public function daily_log_channel(): void
8579
}
8680

8781
#[Test]
88-
public function weekly_log_channel(): void
82+
public function weekly_log_config(): void
8983
{
9084
$filePath = __DIR__ . '/logs/tempest-' . date('Y-W') . '.log';
91-
$config = new LogConfig(
92-
channels: [
93-
new WeeklyLogChannel(__DIR__ . '/logs/tempest.log'),
94-
],
95-
);
85+
$config = new WeeklyLogConfig(__DIR__ . '/logs/tempest.log', prefix: 'tempest');
9686

9787
$logger = new GenericLogger($config, $this->container->get(EventBus::class));
9888
$logger->info('test');
@@ -107,11 +97,12 @@ public function multiple_same_log_channels(): void
10797
$filePath = __DIR__ . '/logs/multiple-tempest1.log';
10898
$secondFilePath = __DIR__ . '/logs/multiple-tempest2.log';
10999

110-
$config = new LogConfig(
100+
$config = new MultipleChannelsLogConfig(
111101
channels: [
112102
new AppendLogChannel($filePath),
113103
new AppendLogChannel($secondFilePath),
114104
],
105+
prefix: 'tempest',
115106
);
116107

117108
$logger = new GenericLogger($config, $this->container->get(EventBus::class));
@@ -131,11 +122,9 @@ public function multiple_same_log_channels(): void
131122
public function log_levels(mixed $level, string $expected): void
132123
{
133124
$filePath = __DIR__ . '/logs/tempest.log';
134-
$config = new LogConfig(
125+
$config = new SimpleLogConfig(
126+
path: $filePath,
135127
prefix: 'tempest',
136-
channels: [
137-
new AppendLogChannel($filePath),
138-
],
139128
);
140129

141130
$logger = new GenericLogger($config, $this->container->get(EventBus::class));
@@ -157,19 +146,17 @@ public function message_logged_emitted(LogLevel $level, string $_expected): void
157146
$this->assertSame(['foo' => 'bar'], $event->context);
158147
});
159148

160-
$logger = new GenericLogger(new LogConfig(), $eventBus);
149+
$logger = new GenericLogger(new NullLogConfig(), $eventBus);
161150
$logger->log($level, 'This is a log message of level: ' . $level->value, context: ['foo' => 'bar']);
162151
}
163152

164153
#[Test]
165154
public function different_log_levels(): void
166155
{
167156
$filePath = __DIR__ . '/logs/tempest.log';
168-
$config = new LogConfig(
157+
$config = new SimpleLogConfig(
158+
path: $filePath,
169159
prefix: 'tempest',
170-
channels: [
171-
new AppendLogChannel($filePath),
172-
],
173160
);
174161

175162
$logger = new GenericLogger($config, $this->container->get(EventBus::class));

0 commit comments

Comments
 (0)