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);
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);
I solved my problem like this:
monolog.yaml:
...
channels: ["!messenger"]
...
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"