andrew
andrew

Reputation: 21

How to make custom sql log in symfony 2 and doctrine 2?

I need to add sql logging to work in native WebProfileBundle. When I do one default connection in application config, I see the sql queries in my log. But my application uses many connections to many db servers, so I can't add all the possible connections to config file.

I create runtime connections, i.e.:

$config = array(
        'user' => 'user1',
        'password' => 'pass1',
        'driver' => 'pdo_mysql',
        'port' => 3306,
);
$conn = DriverManager::getConnection($config);

then I think, should be command something like this

$conn->getConfiguration()->getSQLLogger($someLoggerObject);

I've tried to solve this problem with DependencyInjection, took DoctrineBundle as example. But have no luck.

Any help with live code or link to proper documentation would be great

Upvotes: 2

Views: 1306

Answers (1)

Hernan Rajchert
Hernan Rajchert

Reputation: 926

I have this in my conf:

$config = new Configuration;
/** configuration stuff */
$config->setSQLLogger(MyLogger::getInstance());

$connectionOptions = array(
            'driver' => 'pdo_mysql',
            'user' => DB_USER,
            'password' => DB_PASS,
            'host' => DB_HOST,
            'dbname' => DB_NAME,
            'charset' => 'utf8',
            'driverOptions' => array(
                    'charset' => 'utf8'
            )
    );
$em = EntityManager::create($connectionOptions, $config);

I use Zend + Doctrine2 and that is in my bootstrap, my logger is a singleton (thats why getInstance). I've never user DriverManager, but i hope this helps.

Upvotes: 1

Related Questions