This is not as easy as it seems. Processor are either registered per logger or per handler.
If the processor is registered in the logger, there is no way of knowing which handler the processor went through, as all handlers are executed after all processors were executed. If you register the processor on a handler, you always know which handler is executed, but registering a processor for every handler manually is quite cumbersome.
In order to solve this, you could add a CompilerPass which does the manual work for you.
Lets assume we have this processor
class HandlerProcessor
{
/**
* @var string
*/
private $handlerName;
public function __construct($handlerName)
{
$this->handlerName = $handlerName;
}
public function processRecord(array $record)
{
$record['extra']['handler_name'] = $this->handlerName;
return $record;
}
}
With this processor you can then create a compiler pass, which creates all the definitions for the different handlers monolog created
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
class HandlerProcessorPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasParameter('monolog.handlers_to_channels')) {
return;
}
foreach ($container->getParameter('monolog.handlers_to_channels') as $handlerId => $channels) {
$definition = new Definition(HandlerProcessor::class);
$handlerDefinition = $container->getDefinition($handlerId);
$handlerName = $handlerDefinition->getClass();
$definition->addArgument($handlerName);
$handlerProcessorId = 'handler_processor.' . $handlerId;
$container->setDefinition($handlerProcessorId, $definition);
$handlerDefinition->addMethodCall('pushProcessor', array(new Reference($handlerProcessorId)));
}
}
}
As compiler passes can be quite confusing if you never used them before, lets go line by line:
if (!$container->hasParameter('monolog.handlers_to_channels')) {
return;
}
This checks if the monolog bundle is registered. The parameter monolog.handlers_to_channels
is added to the container in the MonologExtension. The parameter itself contains an array where the key is the name of the handler id and the value is an array with all channels this handler is registered to (or null if this handler is valid for all channels)
foreach ($container->getParameter('monolog.handlers_to_channels') as $handlerId => $channels) {
$definition = new Definition(HandlerProcessor::class);
$handlerDefinition = $container->getDefinition($handlerId);
Here we create a new definition of our HandlerProcessor
and get the definition of the current processed handler.
$handlerName = $handlerDefinition->getClass();
$definition->addArgument($handlerName);
This will fetch the class name of the handler (e.g. Monolog\Handler\SlackHandler) and adds it as a constructor argument to the HandlerProcessor
$handlerProcessorId = 'handler_processor.' . $handlerId;
$container->setDefinition($handlerProcessorId, $definition);
$handlerDefinition->addMethodCall('pushProcessor', array(new Reference($handlerProcessorId)));
And last but not least we create a new ID for the processor definition we just created at the beginning of the foreach loop and add it with the method call pushProcessor
to our newly created processor.
Now that everything is set up, you have to register this compiler pass in your container. I assume you are using symfony 2.x so you have to do that in your AppBundle
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AppBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new HandlerProcessorPass());
}
}
Here are some additional links which might be helpful: