How to enable mongodb query logging in reactive java for quarkus with panache
Tags: java, mongodb, quarkus, panache
Published: 2024-07-07 19:51:55

Keeping it here so that anyone after me can find these configs easily.

At my job I've been writing a microservice with java with reactive style. I'm also using mongodb with panache. Recently I had a problem where one of my queries were working perfectly from mongo compass but with panache it wasn't working as expected. So I needed to see the query that panache was generating.

The quarkus's official document provides a [way](https://quarkus.io/guides/mongodb-panache#query-debugging) to log mongodb query in output. Unfortunately it only works for non reactive java and the guide doesn't properly mention this.

```
quarkus.log.category."io.quarkus.mongodb.panache.common.runtime".level=DEBUG
```

At first I thought for some reason maybe my project configuration was wrong or maybe configs were conflicting and that's why it wasn't showing the logs. I never thought what reactive coding was the problem. So I debugged the issue in other ways and found the proper solution for my problem.

After that I got somewhat bothered and kept asking myself why the query logging wasn't working and got frustrated. Then in an eureka moment I looked at the configuration key carefully and I can clearly see that the log was enabling a category `io.quarkus.mongodb.panache.common.runtime` so I went looking for that package. From my jetbrains IDE I found it and I can clearly see that there is a `common.runtime.CommonPanacheQueryImpl` and another `common.reactive.runtime.CommonReactivePanacheQueryImpl`.

![Folder structure of the package](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqk5kbw1sk21nns3npjc.png)

So I tried to filter by that category and it worked :) 
So the solution is to just enable `DEBUG` on this category. So add this line to your application.properties.
```
quarkus.log.category."io.quarkus.mongodb.panache.common.reactive.runtime".level=DEBUG
```

Now the logging is working

![Query logging working](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m55zoeuinohgpu274qcq.png)

### Bonus
While searching I've found another way to enable query logging, but it is logging from the mongodb driver. To enable it just add this to the application.properties
```
quarkus.log.category."org.mongodb.driver.protocol.command".level=DEBUG
```
The result looks like this, it also outputs the result from the db.

![MongoDB driver logging](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46o537y81psf7ydvnws2.png)

Just thought that I'd post these configs here as a reference to someone who is doing reactive java with panache. As I've invested one of my weekends figuring this out as well as other things. I'll probably make a longer post on my journey as a Senior Software Engineer to debug a panache capability issue and how a junior might navigate these and what were my step by step thought process during this.

I've learned all these because I kept asking myself "why wouldn't it work?" and it kept bothering me.


### References
1. [My own notes](https://github.com/minhaz1217/devops-notes/tree/master/72.%20enable%20mongodb%20query%20logging%20in%20panache%20for%20reactive%20java)
2. [Quarkus docs](https://quarkus.io/guides/mongodb-panache#query-debugging)
3. [A github discussion](https://github.com/quarkusio/quarkus/discussions/24290)