0
votes

I want to create my own handler for monolog using this tutorial.

Now as my handler required PDO connection like this

$logger->pushHandler(new LogHandler(new PDO("mysql:host=$host;dbname=$dbname", $user, $pass)));
$logger->addInfo('My logger is now ready');

Then this guy told me insert that connection as service

LIke this

doctrine.dbal.default.wrapped_connection:
    factory_service: doctrine.dbal.default_connection
    factory_method: getWrappedConnection
    class: PDO

my.monologhandler.pdo:
    class: sojeans\BackBundle\Monolog\Handler\PDOHandler
    arguments:
        - '@doctrine.dbal.default.wrapped_connection'
    tags:
        - { name: log_handler }

But i am getting circular reference error so other guy told to do this

there is a circular reference here: you need to create the doctrine.dbal.default_connection service to create the doctrine.dbal.default.wrapped_connection`` (as it is used a factory service) and this service uses the logger.

But I don't know how to do that.

1
Do you have the doctrine bundle configured in your config.yml ? - AdrienBrault

1 Answers

1
votes

Try to add a new connection to your doctrine configuration that has no debug nor profiling:

doctrine:
    dbal:
        connections:
            default:
                driver:   %database_driver%
                host:     %database_host%
                port:     %database_port%
                dbname:   %database_name%
                user:     %database_user%
                password: %database_password%
                charset:  UTF8
            logger:
                driver:   %database_driver%
                host:     %database_host%
                port:     %database_port%
                dbname:   %database_name%
                user:     %database_user%
                password: %database_password%
                charset:  UTF8
                logging:   false
                profiling: false

Then use the new logger_connection services:

doctrine.dbal.logger.wrapped_connection:
    factory_service: doctrine.dbal.logger_connection
    factory_method: getWrappedConnection
    class: PDO

my.monologhandler.pdo:
    class: sojeans\BackBundle\Monolog\Handler\PDOHandler
    arguments:
        - @doctrine.dbal.logger.wrapped_connection
    tags:
        - { name: log_handler }

Let me know if this works.