ShahRokh
ShahRokh

Reputation: 1025

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?

Upvotes: 0

Views: 1581

Answers (2)

Rein Baarsma
Rein Baarsma

Reputation: 1536

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

Upvotes: 0

Dmitrijs Čornobiļs
Dmitrijs Čornobiļs

Reputation: 953

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.

Upvotes: 0

Related Questions