Freewind
Freewind

Reputation: 198408

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

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

I want to log all 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?

Upvotes: 5

Views: 3640

Answers (3)

Stefanos Kargas
Stefanos Kargas

Reputation: 11103

Using Wildfly deployment, I managed to enable Morphia logging by adding the following to the stadalone.xml:

<server xmlns="urn:jboss:domain:15.0">
  <profile>
    <subsystem xmlns="urn:jboss:domain:logging:8.0">
      <logger category="org.mongodb.morphia" use-parent-handlers="true">
        <level name="TRACE"/>
        <handlers>
          <handler name="FILE"/>
        </handlers>
      </logger>
    </subsystem>
  </profile>
</server>

Upvotes: 0

malloc4k
malloc4k

Reputation: 1937

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>

Upvotes: 2

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 341003

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.

Upvotes: 5

Related Questions