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