Skip to content

Latest commit

 

History

History
78 lines (62 loc) · 2.2 KB

logger.md

File metadata and controls

78 lines (62 loc) · 2.2 KB

Connecting with logger

Yii DB uses PSR-3 for logging. You can configure a logger that implements Psr\Log\LoggerInterface::class in the DI container.

In the following example, you configure Yii Logging Library with a file target.

Create a file config/common/di/logger.php:

use Psr\Log\LoggerInterface;
use Yiisoft\Definitions\ReferencesArray;
use Yiisoft\Log\Logger;
use Yiisoft\Log\Target\File\FileTarget;

return [
    LoggerInterface::class => [
        'class' => Logger::class,
        '__construct()' => [
            'targets' => ReferencesArray::from([FileTarget::class]),
        ],
    ],
];

Depending on used DBMS, create a file with database connection configuration. For example, when using PostgreSQL, it will be config/common/di/db-pgsql.php:

use Psr\Log\LoggerInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Pgsql\Connection;
use Yiisoft\Db\Pgsql\Driver;
use Yiisoft\Definitions\Reference;

/** @var array $params */

return [
    ConnectionInterface::class => [
        'class' => Connection::class,
        '__construct()' => [
            'driver' => new Driver(
                $params['yiisoft/db-pgsql']['dsn'],
                $params['yiisoft/db-pgsql']['username'],
                $params['yiisoft/db-pgsql']['password'],
            ),
        ],
        'setLogger()' => [Reference::to(LoggerInterface::class)],        
    ],
];

For other DBMS refer to "Create connecton" section.

Advanced usage of Logger

If you need to redefine logger messages or increase/decrease logging level:

  1. Create a custom logger class
  2. Use the context to detect type of the message in the "log" method
use Yiisoft\Db\Driver\Pdo\LogType;

class MyLogger extends ParentLoggerClass implements LoggerInterface
{
    public function log($level, string|\Stringable $message, array $context = []): void
    {
        if ($context['type'] === LogType::QUERY) {
            ... your logic here
        }    
    }
    
    // implements other methods of LoggerInterface without changes
}