2

How I can disable log of type INFO sent by SendMessageMiddleware of Symfony Messenger component?

symfony/messenger/Middleware/SendMessageMiddleware.php:

$this->logger->info('Received message {class}', $context);

https://github.com/symfony/messenger/blob/9c036a45bcf837dc38f0db93095a45a21096dd63/Middleware/SendMessageMiddleware.php#L57

yivi
  • 42,438
  • 18
  • 116
  • 138
Bruno Correa
  • 91
  • 1
  • 8
  • My app need this log level. Maybe this logging should be 'debug' level (on SF component). Well, is possible do channel separately log of SF Messenger? Then changing the level, 'warning' level by example, how I can do settings? – Bruno Correa Mar 02 '20 at 17:27
  • 1
    Thanks for help. I excluded "messenger" channel in my handler, is solved the problem! Simple. (monolog.yaml: `channels: ["!messenger"]`) – Bruno Correa Mar 02 '20 at 18:21

2 Answers2

7

I solved my problem like this:

monolog.yaml:

    ...
    channels: ["!messenger"]
    ...
Bruno Correa
  • 91
  • 1
  • 8
0

It is possible to change dynamically the log level of each log sent to Monolog.

To change the log legel of Symfony Messenger from the current INFO to, for example, DEBUG, you have to create a Monolog Processor.

The process is really similar to what explained in the Symfony documentation in How to Add extra Data to Log Messages via a Processor.

You have to create a class like this:

<?php

namespace App\Monolog;

use Monolog\Logger;
use Monolog\Processor\ProcessorInterface;

class MessengerInfoToDebugLogLevel implements ProcessorInterface
{
    public function __invoke(array $record): array
    {
        $channel = $record['channel'] ?? null;
        if (null === $channel) {
            return $record;
        }

        if ('messenger' !== $channel) {
            return $record;
        }

        $record['level'] = Logger::DEBUG;
        $record['level_name'] = Logger::getLevelName(Logger::DEBUG);

        return $record;
    }
}

Then you register it as a service and tag it with monolog.processor:

# I put it here, but you can put it where you like most
# config/packages/dev/messenger.yaml

services:
    App\Monolog\MessengerInfoToDebugLogLevel:
        tags:
            - { name: monolog.processor }

Now all logs from messenger are downgraded to DEBUG logs.

The same can be done also with the Symfony HttpClient to downgrade its log messages (that are at INFO, too):

<?php

namespace App\Monolog;

use Monolog\Logger;
use Monolog\Processor\ProcessorInterface;

class HttpClientInfoToDebugLogLevel implements ProcessorInterface
{
    public function __invoke(array $record): array
    {
        $channel = $record['channel'] ?? null;
        if (null === $channel) {
            return $record;
        }

        if ('http_client' !== $channel) {
            return $record;
        }

        $record['level'] = Logger::DEBUG;
        $record['level_name'] = Logger::getLevelName(Logger::DEBUG);

        return $record;
    }
}

Then you register it as a service and tag it with monolog.processor:

# I put it here, but you can put it where you like most
# config/packages/dev/http_client.yaml

services:
    App\Monolog\HttpClientInfoToDebugLogLevel:
        tags:
            - { name: monolog.processor }

This solution was inspired by Matthias Noback's "Dynamically changing the log level in Symfony apps"

Aerendir
  • 6,152
  • 9
  • 55
  • 108