1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Monolog\Bootloader; |
6
|
|
|
|
7
|
|
|
use Monolog\Formatter\LineFormatter; |
8
|
|
|
use Monolog\Handler\HandlerInterface; |
9
|
|
|
use Monolog\Handler\RotatingFileHandler; |
10
|
|
|
use Monolog\Logger; |
11
|
|
|
use Monolog\ResettableInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
14
|
|
|
use Spiral\Boot\EnvironmentInterface; |
15
|
|
|
use Spiral\Boot\FinalizerInterface; |
16
|
|
|
use Spiral\Config\ConfiguratorInterface; |
17
|
|
|
use Spiral\Config\Patch\Append; |
18
|
|
|
use Spiral\Core\Container; |
19
|
|
|
use Spiral\Logger\LogsInterface; |
20
|
|
|
use Spiral\Monolog\Config\MonologConfig; |
21
|
|
|
use Spiral\Monolog\LogFactory; |
22
|
|
|
|
23
|
|
|
final class MonologBootloader extends Bootloader implements Container\SingletonInterface |
24
|
|
|
{ |
25
|
|
|
protected const SINGLETONS = [ |
26
|
|
|
LogsInterface::class => LogFactory::class, |
27
|
|
|
LoggerInterface::class => Logger::class, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
protected const BINDINGS = [ |
31
|
|
|
'log.rotate' => [self::class, 'logRotate'], |
32
|
|
|
]; |
33
|
|
|
|
34
|
19 |
|
public function __construct( |
35
|
|
|
private readonly ConfiguratorInterface $config |
36
|
|
|
) { |
37
|
19 |
|
} |
38
|
|
|
|
39
|
19 |
|
public function init(Container $container, FinalizerInterface $finalizer, EnvironmentInterface $env): void |
40
|
|
|
{ |
41
|
19 |
|
$finalizer->addFinalizer(static function (bool $terminate) use ($container): void { |
42
|
3 |
|
if ($terminate) { |
43
|
1 |
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
if ($container->hasInstance(LoggerInterface::class)) { |
47
|
2 |
|
$logger = $container->get(LoggerInterface::class); |
48
|
|
|
|
49
|
2 |
|
if ($logger instanceof ResettableInterface) { |
50
|
2 |
|
$logger->reset(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
if ($container->hasInstance(LogsInterface::class)) { |
55
|
2 |
|
$factory = $container->get(LogsInterface::class); |
56
|
|
|
|
57
|
2 |
|
if ($factory instanceof ResettableInterface) { |
58
|
1 |
|
$factory->reset(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
19 |
|
}); |
62
|
|
|
|
63
|
19 |
|
$this->config->setDefaults(MonologConfig::CONFIG, [ |
64
|
19 |
|
'default' => $env->get('MONOLOG_DEFAULT_CHANNEL', MonologConfig::DEFAULT_CHANNEL), |
65
|
19 |
|
'globalLevel' => Logger::DEBUG, |
66
|
19 |
|
'handlers' => [], |
67
|
19 |
|
]); |
68
|
|
|
|
69
|
19 |
|
$container->bindInjector(Logger::class, LogFactory::class); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function addHandler(string $channel, HandlerInterface $handler): void |
73
|
|
|
{ |
74
|
|
|
if (!isset($this->config->getConfig(MonologConfig::CONFIG)['handlers'][$channel])) { |
75
|
|
|
$this->config->modify(MonologConfig::CONFIG, new Append('handlers', $channel, [])); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->config->modify( |
79
|
|
|
MonologConfig::CONFIG, |
80
|
|
|
new Append( |
81
|
|
|
'handlers.' . $channel, |
82
|
|
|
null, |
83
|
|
|
$handler |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function logRotate( |
89
|
|
|
string $filename, |
90
|
|
|
int $level = Logger::DEBUG, |
91
|
|
|
int $maxFiles = 0, |
92
|
|
|
bool $bubble = false |
93
|
|
|
): HandlerInterface { |
94
|
1 |
|
$handler = new RotatingFileHandler( |
95
|
1 |
|
$filename, |
96
|
1 |
|
$maxFiles, |
97
|
1 |
|
$level, |
98
|
1 |
|
$bubble, |
99
|
1 |
|
null, |
100
|
1 |
|
true |
101
|
1 |
|
); |
102
|
|
|
|
103
|
1 |
|
return $handler->setFormatter( |
104
|
1 |
|
new LineFormatter("[%datetime%] %level_name%: %message% %context%\n") |
105
|
1 |
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|