Seldaek / monolog

Sends your logs to files, sockets, inboxes, databases and various web services
https://seldaek.github.io/monolog/
MIT License
21.03k stars 1.9k forks source link

Fatal error Attempted to load class "MongoClient" from the global namespace #1423

Closed rbaarsma closed 4 years ago

rbaarsma commented 4 years ago

Monolog version 1

As stated in https://github.com/Seldaek/monolog/issues/962, the \MongoClient is deprecated in php 7 and should no longer be used.

Unfortunately when using Symfony 4, we are still bound to monolog 1 (because of the monolog-bridge requiring monolog 1.x). And the hard dependence (without any class_exists checking) is causing a hard crash when attempting to use the MonologDBHandler.

I propose a simple class_exists solution, so the 1.x branch (and Symfony 4) can use the MongoDBHandler without needing to install a deprecated and outdated mongo client.

So for example:

if ((class_exists('\MongoClient') && $mongo instanceof \MongoClient) || $mongo instanceof \Mongo || $mongo instanceof \MongoDB\Client) {
    throw new \InvalidArgumentException('MongoClient, Mongo or MongoDB\Client instance required');
}
Seldaek commented 4 years ago

What error are you seeing exactly? Because AFAIK, instanceof does not trigger autoloading or anything, it just checks whether the object matches the given class, and adding class_exists() should not do anything.

rbaarsma commented 4 years ago

The exact error is in the title of the bug report: Fatal error Attempted to load class "MongoClient" from the global namespace.

I'm fairly sure the line number of the error corresponded with the line where the instanceof was used.

I just assumed it comes from the instance of check.. I've since worked around this issue, I can check tomorrow if I can reproduce in our project.

Seldaek commented 4 years ago

I'd be curious yes if you can try and reproduce again.. because instanceof definitely does not need the class to be present: https://3v4l.org/GbeUS

rbaarsma commented 4 years ago

Dunno if it's the new version of some mistake I made earlier, but it works now without any problems.

Just for reference, this is working the monolog.yaml I have (in Symfony 4 app):

monolog:
    handlers:
        processor:
            type: mongo
            mongo:
                id: "doctrine_mongodb.odm.default_connection"
                database: "%env(MONGODB_DB)%"
                collection: ProcessorLog
            level: debug

So I'm closing as it seems to be no longer an issue -- cannot reproduce

cimo23 commented 3 years ago

bug occur when we use the configuration without ODM Id Service

monolog:
    handlers:
        main:
            type: mongo
            mongo:
               host: '127.0.0.1'
               port: 27017
               user: root
               pass: '123456'
               database: logsDb
               collection: logsCollection