I have configured Log4j2 to write the logs to my Mongo Atlas cluster (4.4.8).
The configuration seems ok (I use the connection string given by Atlas), and the logs (console) say that the connection to the MongoDB is ok, database retrieved correctly and collection retrived correctly.
But then, when it tries to write a log to the DB, it times out after 30000ms saying:
Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[]
I also can see several messages saying:
INFO org.mongodb.driver.cluster - Cluster description not yet available. Waiting for 30000 ms before timing out
What I don't understand is that, using the very same driver, same connection string, all the operations I perform on this same MongoDB managing the connection myself (I have a MongoDBService class where I build the Mongo Connection etc...normal stuff) work with no problem, so it leads me to thing that it is Log4j that handles the connection to MongoDB in a bad way...
Any help is appreciated!
Finally I found the problem in my configurations. Maybe it works for you too. I was used to have multiple appenders in root logger. So mongodb was trying to log something like: "hey, I'm going to log" after initializing the RollingFileAppender but before the mongodbAppender. You can see it in below:
Root:
level: info
AppenderRef:
- ref: ConsoleAppender
- ref: RollingFileAppender
- ref: MongoAppender
Just by changing the mongo appender's logger everything worked for me.
logger:
- name: com.sinansoft
level: info
additivity: false
AppenderRef:
- ref: MongoAppender
Root:
level: info
AppenderRef:
- ref: ConsoleAppender
- ref: RollingFileAppender
Let me know if you want more configuration details in this case.