-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMonologExtension.php
145 lines (121 loc) · 5.15 KB
/
MonologExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php declare(strict_types = 1);
namespace Contributte\Monolog\DI;
use Contributte\Monolog\Exception\Logic\InvalidArgumentException;
use Contributte\Monolog\Exception\Logic\InvalidStateException;
use Contributte\Monolog\LoggerHolder;
use Contributte\Monolog\LoggerManager;
use Contributte\Monolog\Tracy\LazyTracyLogger;
use Monolog\Handler\PsrHandler;
use Monolog\Logger;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\Statement;
use Nette\PhpGenerator\ClassType;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nette\Utils\Strings;
use Tracy\Bridges\Psr\PsrToTracyLoggerAdapter;
use Tracy\Bridges\Psr\TracyToPsrLoggerAdapter;
use Tracy\Debugger;
class MonologExtension extends CompilerExtension
{
public function getConfigSchema(): Schema
{
return Expect::structure([
'channel' => Expect::arrayOf(Expect::structure([
'handlers' => Expect::array()->required()->min(1),
'processors' => Expect::array()->default([]),
])->castTo('array'))->required()->min(1),
'hook' => Expect::structure([
'fromTracy' => Expect::bool()->default(true),
'toTracy' => Expect::bool()->default(true),
])->castTo('array'),
'holder' => Expect::structure([
'enabled' => Expect::bool()->default(false),
])->castTo('array'),
'manager' => Expect::structure([
'enabled' => Expect::bool()->default(false),
])->castTo('array'),
])->castTo('array');
}
public function loadConfiguration(): void
{
$config = (array) $this->getConfig();
$builder = $this->getContainerBuilder();
if (!isset($config['channel']['default'])) {
throw new InvalidStateException(sprintf('%s.channel.default is required.', $this->name));
}
if ($config['manager']['enabled']) {
$builder->addDefinition($this->prefix('manager'))
->setFactory(LoggerManager::class, [
$this->prefix('logger'),
]);
}
$tracyHandler = null;
if (class_exists(Debugger::class) && $config['hook']['toTracy'] && $builder->hasDefinition('tracy.logger')) {
$tracyAdapter = new Statement(TracyToPsrLoggerAdapter::class, ['@tracy.logger']);
$tracyHandler = new Statement(PsrHandler::class, [$tracyAdapter]);
}
foreach ($config['channel'] as $name => $channel) {
if (!is_string($name)) {
throw new InvalidArgumentException(sprintf('%s.channel.%s name must be a string', $this->name, (string) $name));
}
if ($tracyHandler !== null) {
$channel['handlers']['tracy'] = $tracyHandler;
}
// Register handlers same way as services (setup, arguments, type etc.)
foreach ($channel['handlers'] as $handlerKey => $handlerValue) {
// Don't register handler as service, it's already registered service
if (is_string($handlerValue) && Strings::startsWith($handlerValue, '@')) {
continue;
}
$handlerName = $this->prefix('logger.' . $name . '.handler.' . $handlerKey);
$this->compiler->loadDefinitionsFromConfig([$handlerName => $handlerValue]);
$builder->getDefinition($handlerName)->setAutowired(false);
$channel['handlers'][$handlerKey] = '@' . $handlerName;
}
// Register processors same way as services (setup, arguments, type etc.)
foreach ($channel['processors'] as $processorKey => $processorValue) {
// Don't register processor as service, it's already registered service
if (is_string($processorValue) && Strings::startsWith($processorValue, '@')) {
continue;
}
$processorName = $this->prefix('logger.' . $name . '.processor.' . $processorKey);
$this->compiler->loadDefinitionsFromConfig([$processorName => $processorValue]);
$builder->getDefinition($processorName)->setAutowired(false);
$channel['processors'][$processorKey] = '@' . $processorName;
}
$logger = $builder->addDefinition($this->prefix('logger.' . $name))
->setFactory(Logger::class, [
$name,
$channel['handlers'],
$channel['processors'],
]);
// Only default logger is autowired
if ($name !== 'default') {
$logger->setAutowired(false);
}
}
if (class_exists(Debugger::class) && $config['hook']['fromTracy'] && $builder->hasDefinition('tracy.logger')) {
$builder->getDefinition('tracy.logger')
->setAutowired(false);
$builder->addDefinition($this->prefix('psrToTracyAdapter'))
->setFactory(PsrToTracyLoggerAdapter::class)
->setAutowired(false);
$builder->addDefinition($this->prefix('psrToTracyLazyAdapter'))
->setFactory(LazyTracyLogger::class, [$this->prefix('psrToTracyAdapter')]);
}
}
public function afterCompile(ClassType $class): void
{
$builder = $this->getContainerBuilder();
$config = (array) $this->getConfig();
$initialize = $class->getMethod('initialize');
if (class_exists(Debugger::class) && $config['hook']['fromTracy'] && $builder->hasDefinition('tracy.logger')) {
$initialize->addBody('$this->getService("tracy.logger");'); // Create original Tracy\Logger service to prevent psrToTracyLazyAdapter contain itself - workaround for Tracy\ILogger service created statically
$initialize->addBody(Debugger::class . '::setLogger($this->getService(?));', [$this->prefix('psrToTracyLazyAdapter')]);
}
if ($config['holder']['enabled']) {
$initialize->addBody(LoggerHolder::class . '::setLogger(?, $this);', [$this->prefix('logger.default')]);
}
}
}