开发者

How to log all the queries in a log file in a java application with mongodb?

开发者 https://www.devze.com 2023-03-22 19:03 出处:网络
I have a java application which use morphia to work with mongodb. I want to log al开发者_运维技巧l the queries sent to mongodb in a file, which I can analyze them to improve the performance. But I do

I have a java application which use morphia to work with mongodb.

I want to log al开发者_运维技巧l the queries sent to mongodb in a file, which I can analyze them to improve the performance. But I don't find a way to log them.

I know mongodb can log the queries in system.profile collection, but I still want a log file.

How to do that?


Do you use SLF4J or Logback/Log4J? Add Morphias' SLF4JExtension to your CLASSPATH.

Then simply enable com.google.code.morphia logger and log it wherever you want.


I'm using Morphia (version 1.3.2) on top of Java MongoDB driver (version 3.9.1), and I was able to enable the actual query logging (using Log4j2) by setting org.mongodb.morphia logger level to trace. That is, in my log4j2.xml:

<Loggers>
    ...
    <Logger name="org.mongodb.morphia" level="trace" />
    ...
</Loggers>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号