Skip to content

Commit 7d3f9c3

Browse files
committed
Add monolog:channels config option to create arbitrary channels, fixes #39
Just define them as: monolog: channels: ["foo", "bar"] And then use in a controller for example as $this->get("monolog.logger.foo");
1 parent 881616d commit 7d3f9c3

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

DependencyInjection/Compiler/LoggerChannelPass.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function process(ContainerBuilder $container)
3232
return;
3333
}
3434

35+
// create channels necessary for the handlers
3536
foreach ($container->findTaggedServiceIds('monolog.logger') as $id => $tags) {
3637
foreach ($tags as $tag) {
3738
if (empty($tag['channel']) || 'app' === $tag['channel']) {
@@ -60,8 +61,15 @@ public function process(ContainerBuilder $container)
6061
}
6162
}
6263

63-
$handlersToChannels = $container->getParameter('monolog.handlers_to_channels');
64+
// create additional channels
65+
foreach ($container->getParameter('monolog.additional_channels') as $chan) {
66+
$loggerId = sprintf('monolog.logger.%s', $chan);
67+
$this->createLogger($chan, $loggerId, $container);
68+
}
69+
$container->getParameterBag()->remove('monolog.additional_channels');
6470

71+
// wire handlers to channels
72+
$handlersToChannels = $container->getParameter('monolog.handlers_to_channels');
6573
foreach ($handlersToChannels as $handler => $channels) {
6674
foreach ($this->processChannels($channels) as $channel) {
6775
try {

DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ public function getConfigTreeBuilder()
174174
$rootNode
175175
->fixXmlConfig('handler')
176176
->children()
177+
->arrayNode('channels')
178+
->canBeUnset()
179+
->prototype('scalar')->end()
180+
->end()
177181
->arrayNode('handlers')
178182
->canBeUnset()
179183
->useAttributeAsKey('name')

DependencyInjection/MonologExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public function load(array $configs, ContainerBuilder $container)
8686
'Monolog\\Handler\\FingersCrossed\\ErrorLevelActivationStrategy',
8787
));
8888
}
89+
90+
$container->setParameter('monolog.additional_channels', isset($config['channels']) ? $config['channels'] : array());
8991
}
9092

9193
/**

0 commit comments

Comments
 (0)