ZF2 Zend\Log + Doctrine2
Asked Answered
C

1

5

I do not know how to configure Zend \ Log with Doctrine2. Only allows you to write directly to the database via a connection adapter or write to a file.

Culminant answered 28/12, 2013 at 12:23 Comment(0)
S
7

May be it's too late to answer this question but better late than never.

I've found a good post which explains how to create a basic SQL Logger for ZF2 and Doctrine.

The approach is pretty simple :

1. Creating Logger class : Create the following class in your Module/Application/Log folder :

    <?php
    namespace Application\Log;

    use Zend\Log\Logger;  
    use Doctrine\DBAL\Logging\DebugStack;

    class SqlLogger extends DebugStack  
    {
        protected $logger;
        
        public function __construct(Logger $logger)
        {
            $this->logger = $logger;
        }

        public function stopQuery()
        {
            parent::stopQuery();
            $q = $this->queries[$this->currentQuery];
            $message = "Executed Query:  " . print_r($q, true);
            $this->logger->info($message);
        }
    }

The stopQuery() function which is called by Doctrine when it finiches sending the query to the database server, is overrided so that it could write the current query to the Logger object.

2. Configuring the Logger : Add the following code in your config/autoload/global.php file, to make the Logger accessible to the Service Manager using the name my_sql_logger :

        'service_manager' => array(
            'factories' => array(
                'my_sql_logger' => function($sm) {
                    $log = new \Zend\Log\Logger();
                    $writer = new \Zend\Log\Writer\Stream('./data/logs/sql.log');
                    $log->addWriter($writer);

                    $sqllog = new \Application\Log\SqlLogger($log);
                    return $sqllog;
                },
            )
        ),
        

The Logger will write data to the data/logs/sql.log file. So, make sure that data/logs folder exists in your application root directory.

3. Configuring Doctrine : Now you need to tell Doctrine to use the created Logger. Just add the following code to your Doctrine configuration :

    return array(  
        'doctrine' => array(
             /*--------Add this code------------*/
            'sql_logger_collector' => array(
                'orm_default' => array(
                    'sql_logger' => 'my_sql_logger',
                ),
            ),
            /*---------------------------------*/
            'connection' => array(
                'orm_default' => array(
                    'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                    'params' => array(
                        'host' => 'localhost',
                        'port' => '3306',
                        'user' => 'username',
                        'password' => 'password',
                        'dbname' => 'dbname',
                    ),
                ),
            ),
        ),
    );

With the above configuration of Zend\Log and Doctrine2, you'll get all the query data logged in the data/log/sql.log file.

Please see this Sql Logger for ZF2 and Doctrine for more details.

Spillar answered 12/8, 2014 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.