Search code examples
mongodbsymfonymonolog

Using MongoDB in Symfony Monolog Causes Error


The error I'm getting is like the following:

Screenshot of terminal that shows error in detail


Some information that you should know:

  1. I'm using Symfony 5

  2. I'm able to connect to mongo db using MongoDB Compass and in terminal using mongo command

  3. What packages I have installed are the following:

    • "mongodb/mongodb": "^1.6"
    • "monolog/monolog": "^2.0"
    • "symfony/monolog-bundle": "^3.5"
  4. My configuration file monolog.yaml (config/packages/dev/monolog.yaml) is like the following:

monolog:
    handlers:
        mongo:
            type: mongo
            mongo:
                host: localhost

Thank you.


Solution

  • I had installed mongodb with brew. And I figured out MongoDB\Client class was not recognized. Then I found that we can install mongo driver manually.

    1. I followed the steps here : https://www.php.net/manual/en/mongodb.installation.manual.php

    2. I've changed my monolog configuration file monolog.yaml as

    monolog:
      handlers:
        mongodb:
          type: mongo
          mongo:
            id: mongolog
    
    1. I added MongoDB\Client as service into service configuration file services.yaml
    services:
        ...
        mongolog:
            class: MongoDB\Client
    

    And after doing things above, it's worked.