Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
68.18% |
15 / 22 |
|
33.33% |
2 / 6 |
CRAP | |
50.00% |
1 / 2 |
LogProvider | |
66.67% |
14 / 21 |
|
20.00% |
1 / 5 |
12.00 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
clear | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
clearAll | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
add | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
2.01 | |||
create | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
LocalLogProviderItem | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Log; |
6 | |
7 | use PeServer\Core\Log\ILogger; |
8 | use PeServer\Core\Log\ILogProvider; |
9 | use PeServer\Core\Log\Logging; |
10 | use PeServer\Core\ReflectionUtility; |
11 | use PeServer\Core\Throws\ArgumentException; |
12 | use PeServer\Core\Throws\NotImplementedException; |
13 | |
14 | class LogProvider implements ILogProvider |
15 | { |
16 | #region variable |
17 | |
18 | /** |
19 | * ロガー。 |
20 | * |
21 | * @var array<string,LocalLogProviderItem> |
22 | */ |
23 | private array $loggers = []; |
24 | |
25 | #endregion |
26 | |
27 | public function __construct( |
28 | private Logging $logging |
29 | ) { |
30 | } |
31 | |
32 | #region ILogProvider |
33 | |
34 | public function clear(string $name): bool |
35 | { |
36 | if (isset($this->loggers[$name])) { |
37 | unset($this->loggers[$name]); |
38 | return true; |
39 | } |
40 | |
41 | return false; |
42 | } |
43 | |
44 | public function clearAll(): void |
45 | { |
46 | $this->loggers = []; |
47 | } |
48 | |
49 | public function add(string $name, string $logger, int $level, string $format, array $configuration): void |
50 | { |
51 | if (isset($this->loggers[$name])) { |
52 | throw new ArgumentException('$name: ' . $name); |
53 | } |
54 | |
55 | $this->loggers[$name] = new LocalLogProviderItem( |
56 | $logger, |
57 | $level, |
58 | $format, |
59 | $configuration |
60 | ); |
61 | } |
62 | |
63 | public function create(string $header, int $baseTraceIndex): array |
64 | { |
65 | if (empty($this->loggers)) { |
66 | return []; |
67 | } |
68 | |
69 | $result = []; |
70 | |
71 | foreach ($this->loggers as $item) { |
72 | $options = new LogOptions($header, $baseTraceIndex, $item->level, $item->format, $item->configuration); |
73 | $result[] = ReflectionUtility::create($item->loggerClass, ILogger::class, $this->logging, $options); |
74 | } |
75 | |
76 | return $result; |
77 | } |
78 | |
79 | #endregion |
80 | } |
81 | |
82 | /** |
83 | * `LogProvider` 内で持ち運ぶロガー設定。 |
84 | */ |
85 | //phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses |
86 | readonly class LocalLogProviderItem |
87 | { |
88 | /** |
89 | * 生成 |
90 | * |
91 | * @param class-string<ILogger> $loggerClass |
92 | * @param int $level |
93 | * @phpstan-param ILogger::LOG_LEVEL_* $level |
94 | * @param string $format |
95 | * @phpstan-param literal-string $format |
96 | * @param array<string,mixed> $configuration |
97 | */ |
98 | public function __construct( |
99 | public string $loggerClass, |
100 | public int $level, |
101 | public string $format, |
102 | public array $configuration |
103 | ) { |
104 | } |
105 | } |