Skip to content

MonologBundle #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions autoload.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ $loader->registerNamespaces(array(
'Doctrine' => __DIR__.'/vendor/doctrine/lib',
'Zend' => __DIR__.'/vendor/zend/library',
'Assetic' => __DIR__.'/vendor/assetic/src',
'Monolog' => __DIR__.'/vendor/monolog/src',
));
$loader->registerPrefixes(array(
'Swift_' => __DIR__.'/vendor/swiftmailer/lib/classes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<service id="doctrine.dbal.logger.debug" class="%doctrine.dbal.logger.debug_class%" public="false" />

<service id="doctrine.dbal.logger" class="%doctrine.dbal.logger_class%" public="false">
<tag name="monolog.logger" channel="doctrine" />
<argument type="service" id="logger" on-invalid="null" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@

<!-- logger -->
<service id="doctrine.odm.mongodb.logger" class="%doctrine.odm.mongodb.logger_class%">
<tag name="monolog.logger" channel="doctrine" />
<argument type="service" id="logger" on-invalid="null" />
</service>
<service id="doctrine.odm.mongodb.data_collector" class="%doctrine.odm.mongodb.data_collector_class%" public="false">
Expand Down Expand Up @@ -114,6 +115,6 @@
<argument type="service" id="doctrine.odm.mongodb.security.user.document_manager" />
</service>
<service id="doctrine.odm.mongodb.security.user.document_manager" alias="doctrine.odm.mongodb.default_document_manager" public="false" />

</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

<service id="data_collector.logger" class="%data_collector.logger.class%" public="false">
<tag name="data_collector" template="WebProfilerBundle:Collector:logger" id="logger" priority="255" />
<tag name="monolog.logger" channel="profiler" />
<argument type="service" id="logger" on-invalid="ignore" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<services>
<service id="debug.event_dispatcher" class="%debug.event_dispatcher.class%">
<tag name="monolog.logger" channel="event" />
<argument type="service" id="service_container" />
<argument type="service" id="logger" on-invalid="null" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<services>
<service id="profiler" class="%profiler.class%" scope="request">
<tag name="monolog.logger" channel="profiler" />
<argument type="service" id="profiler.storage" />
<argument type="service" id="logger" on-invalid="null" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
</service>

<service id="routing.loader.real" class="%routing.loader.class%">
<tag name="monolog.logger" channel="router" />
<argument type="service" id="controller_name_converter" />
<argument type="service" id="logger" on-invalid="null" />
<argument type="service" id="routing.resolver" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<services>
<service id="templating.debugger" class="%templating.debugger.class%" public="false">
<tag name="monolog.logger" channel="templating" />
<argument type="service" id="logger" on-invalid="null" />
</service>
</services>
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@

<services>
<service id="controller_name_converter" class="%controller_name_converter.class%" public="false">
<tag name="monolog.logger" channel="request" />
<argument type="service" id="kernel" />
<argument type="service" id="logger" on-invalid="ignore" />
</service>

<service id="controller_resolver" class="%controller_resolver.class%" public="false">
<tag name="monolog.logger" channel="request" />
<argument type="service" id="service_container" />
<argument type="service" id="controller_name_converter" />
<argument type="service" id="logger" on-invalid="ignore" />
</service>

<service id="request_listener" class="%request_listener.class%">
<tag name="kernel.listener" event="core.request" method="handle" />
<tag name="monolog.logger" channel="request" />
<argument type="service" id="service_container" />
<argument type="service" id="router" />
<argument type="service" id="logger" on-invalid="ignore" />
Expand All @@ -38,6 +41,7 @@
</service>

<service id="exception_listener" class="%exception_listener.class%">
<tag name="monolog.logger" channel="request" />
<tag name="kernel.listener" event="core.exception" method="handle" priority="-128" />
<argument>%exception_listener.controller%</argument>
<argument type="service" id="logger" on-invalid="null" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Definition;
use Monolog\Logger;

/**
* Replaces the default logger by another one with its own channel for tagged services.
*
* @author Christophe Coevoet <stof@notk.org>
*/
class DebugHandlerPass implements CompilerPassInterface
{
protected $channels = array();

public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('monolog.logger_prototype') || !$container->hasDefinition('profiler')) {
return;
}

$debugHandler = new Definition('%monolog.handler.debug.class%', array(Logger::DEBUG, true));
$container->setDefinition('monolog.handler.debug', $debugHandler);
$container->getDefinition('monolog.logger_prototype')->addMethodCall('pushHandler', array (new Reference('monolog.handler.debug')));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\DefinitionDecorator;

/**
* Replaces the default logger by another one with its own channel for tagged services.
*
* @author Christophe Coevoet <stof@notk.org>
*/
class LoggerChannelPass implements CompilerPassInterface
{
protected $channels = array();

public function process(ContainerBuilder $container)
{
if (false === $container->hasDefinition('monolog.logger')) {
return;
}

foreach ($container->findTaggedServiceIds('monolog.logger') as $id => $tags) {
foreach ($tags as $tag) {
if (!empty ($tag['channel']) && 'app' !== $tag['channel']) {
$definition = $container->getDefinition($id);
$loggerId = sprintf('monolog.logger.%s', $tag['channel']);
$this->createLogger($tag['channel'], $loggerId, $container);
foreach ($definition->getArguments() as $index => $argument) {
if ($argument instanceof Reference && 'logger' === (string) $argument) {
$definition->setArgument($index, new Reference($loggerId, $argument->getInvalidBehavior(), $argument->isStrict()));
}
}
}
}
}
}

protected function createLogger($channel, $loggerId, ContainerBuilder $container)
{
if (!in_array($channel, $this->channels)) {
$logger = new DefinitionDecorator('monolog.logger_prototype');
$logger->setArgument(0, $channel);
$container->setDefinition($loggerId, $logger);
array_push($this->channels, $channel);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MonologBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

/**
* This class contains the configuration information for the bundle
*
* This information is solely responsible for how the different configuration
* sections are normalized, and merged.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class Configuration
{
/**
* Generates the configuration tree.
*
* @return \Symfony\Component\Config\Definition\NodeInterface
*/
public function getConfigTree()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('monolog', 'array');

$handlersPrototype = $rootNode
->fixXmlConfig('handler')
->arrayNode('handlers')
->canBeUnset()
->performNoDeepMerging()
->useAttributeAsKey('name')
->prototype('array')
->scalarNode('action_level')->end() // fingerscrossed specific
->scalarNode('buffer_size')->end() // fingerscrossed specific
->builder($this->getHandlerSubnode())
->validate()
->ifTrue(function($v) { return 'fingerscrossed' === $v['type'] && !isset($v['handler']); })
->thenInvalid('The handler has to be specified to use a FingersCrossedHandler')
->end()
;
$this->addHandlerSection($handlersPrototype);

return $treeBuilder->buildTree();
}

private function addHandlerSection(NodeBuilder $node)
{
$node
->performNoDeepMerging()
->scalarNode('type')
->isRequired()
->treatNullLike('null')
->beforeNormalization()
->always()
->then(function($v) { return strtolower($v); })
->end()
->end()
->scalarNode('level')->defaultValue('DEBUG')->end()
->booleanNode('bubble')->defaultFalse()->end()
->scalarNode('path')->end() // stream specific
;
}

private function getHandlerSubnode()
{
$node = new NodeBuilder('handler', 'array');
$this->addHandlerSection($node);

return $node;
}
}
Loading