1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Ekino New Relic bundle. |
7
|
|
|
* |
8
|
|
|
* (c) Ekino - Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Ekino\NewRelicBundle\DependencyInjection\Compiler; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
|
21
|
|
|
class MonologHandlerPass implements CompilerPassInterface |
22
|
|
|
{ |
23
|
|
|
public function process(ContainerBuilder $container): void |
24
|
|
|
{ |
25
|
|
|
if (!$container->hasParameter('ekino.new_relic.monolog.channels') || !$container->hasDefinition('monolog.logger')) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$configuration = $container->getParameter('ekino.new_relic.monolog.channels'); |
30
|
|
|
if (null === $configuration) { |
31
|
|
|
$channels = $this->getChannels($container); |
32
|
|
|
} elseif ('inclusive' === $configuration['type']) { |
33
|
|
|
$channels = $configuration['elements'] ?: $this->getChannels($container); |
34
|
|
|
} else { |
35
|
|
|
$channels = \array_diff($this->getChannels($container), $configuration['elements']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
foreach ($channels as $channel) { |
39
|
|
|
try { |
40
|
|
|
$def = $container->getDefinition('app' === $channel ? 'monolog.logger' : 'monolog.logger.'.$channel); |
41
|
|
|
} catch (InvalidArgumentException $e) { |
42
|
|
|
$msg = 'NewRelicBundle configuration error: The logging channel "'.$channel.'" does not exist.'; |
43
|
|
|
throw new \InvalidArgumentException($msg, 0, $e); |
44
|
|
|
} |
45
|
|
|
$def->addMethodCall('pushHandler', [new Reference('ekino.new_relic.logs_handler')]); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function getChannels(ContainerBuilder $container) |
50
|
|
|
{ |
51
|
|
|
$channels = []; |
52
|
|
|
foreach ($container->getDefinitions() as $id => $definition) { |
53
|
|
|
if ('monolog.logger' === $id) { |
54
|
|
|
$channels[] = 'app'; |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
if (0 === \strpos($id, 'monolog.logger.')) { |
58
|
|
|
$channels[] = \substr($id, 15); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $channels; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|