phoenix icon indicating copy to clipboard operation
phoenix copied to clipboard

Add the connection name to logs

Open sylfabre opened this issue 2 years ago • 3 comments

Hello

First, thank you for this amazing package!

I'm using the bundle with several connections and here is a suggestion after playing with logs.

Logs with the dedicated Doctrine\DBAL\Logging\Middleware are lacking the connection name used by the bundle. Right now, it is impossible to know which connection is related to a given log.

Thanks!

sylfabre avatar Jul 25 '23 13:07 sylfabre

I think this would make sense :+1: But this would probably involve contributing to the DBAL Middleware code to make it possible to pass additional information (like the connection name) into the logging context?

dmaicher avatar Jul 26 '23 13:07 dmaicher

We could perhaps inject logger to Middleware with certain monolog processor attached

ostrolucky avatar Jul 26 '23 13:07 ostrolucky

Or maybe a psr logger decorator could also do the job? :thinking:

use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;

class ConnectionNameAwareLogger extends AbstractLogger
{
    public function __construct(private LoggerInterface $decoratedLogger, private string $connectionName)
    {
    }

    public function log($level, \Stringable|string $message, array $context = []): void
    {
        $this->decoratedLogger->log($level, $message, ['doctrine.dbal.connection_name' => $this->connectionName] + $context);
    }
}

dmaicher avatar Aug 02 '23 11:08 dmaicher