0

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?

ShahRokh
  • 1,005
  • 14
  • 31

3 Answers3

0

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.

Dr.X
  • 853
  • 9
  • 15
0

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
Rein Baarsma
  • 1,466
  • 13
  • 22
0

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;
}
user1005064
  • 115
  • 1
  • 7