'Save monolog in mongodb in symfony 4

I want to add monolog in mongodb with default handler(MongoDBHandler) in Symfony 4.

my monolog.yaml file in dev folder

monolog:
    handlers:
        mongo:
             type: mongo
             mongo:
                id: monolog.logger.mongo
                host: '%env(MONGODB_URL)%'
                database: '%env(MONGODB_DB)%'
                collection: logs

my services.yaml

services:
    monolog.logger.mongo:
        class: Monolog\Handler\MongoDBHandler
        arguments: ['@doctrine_mongodb']

my doctrine_mongodb.yaml

doctrine_mongodb:
    auto_generate_proxy_classes: '%kernel.debug%'
    auto_generate_hydrator_classes: '%kernel.debug%'
    connections:
        default:
            server: '%env(MONGODB_URL)%'
            options:
                    db: '%env(MONGODB_DB)%'
        log:
                server: '%env(MONGODB_URL)%'
                options:
                    db: '%env(MONGODB_DB)%'
                    connect: true
    default_database: '%env(MONGODB_DB)%'
    document_managers:
        log:
            auto_mapping: false
            logging: false

But doesn't work.

one of the errors:

Cannot autowire service "monolog.logger.mongo": argument "$database" of method "Monolog\Handler\MongoDBHandler::__construct()" is type-hinted "string", you should configure its value explicitly.

While i use database option in monolog config.

Is there any document?



Solution 1:[1]

Another way to enable mongodb for monolog is:

monolog:
    handlers:
        mongo:
             type: mongo
             mongo:
                host: '%env(MONGODB_URL)%'
                user: myuser
                pass: mypass
                database: '%env(MONGODB_DB)%'
                collection: logs

, So it mean you need to remove id field and add user and pass instead.

Solution 2:[2]

If you use doctrine mongodb already, it's possible to re-use it's connection, avoiding more ENV vars to separate the DSN:

monolog:
    handlers:
        mongo:
            type: mongo
            mongo:
                id: "doctrine_mongodb.odm.default_connection"
                database: "%env(MONGODB_DB)%"
                collection: MyLogDocument # Keeping this the same, allows you to simply use a doctrine repository to access the documents in your app if needed
            level: debug

Solution 3:[3]

I get the following error:

Attempted to load class "MongoClient" from the global namespace. Did you forget a "use" statement?

protected function getMonolog_Handler_MongoService()
{

$this->privates['monolog.handler.mongo'] = $instance = new \Monolog\Handler\MongoDBHandler(new \MongoClient('mongodb://admin:pass@localhost:27017'), 'monolog', 'logs', 100, true);

$instance->pushProcessor(($this->privates['monolog.processor.psr_log_message'] ?? ($this->privates['monolog.processor.psr_log_message'] = new \Monolog\Processor\PsrLogMessageProcessor())));

return $instance;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dr.X
Solution 2 Rein Baarsma
Solution 3 user1005064