venkat g
venkat g

Reputation: 441

Log queries executed by Spring Data Cassandra

We are using AbstractCassandraConfiguration to configure cassandra entities, is there a way to log queries executed by the application using application.properties?

Thanks.

Upvotes: 1

Views: 2858

Answers (2)

saikiran
saikiran

Reputation: 19

There are ways to achieve this

1) Setting the cassandra logging level to DEBUG

logging.level.org.springframework.data.cassandra.core.cql.CqlTemplate=DEBUG

By enabling the debug logs you can see the queries , but you may see the java objects instead of actual queries . For that you can use logback , logback has conversionRule to convert the log line and log them as we want . So we will use conversionRule to convert the java object to queries

First thing is to setup few loggers and appenders by creating logback.xml

<configuration>
    <conversionRule conversionWord="cql" converterClass="com.converter.CqlQueryExtractor" />

    <appender name="CONSOLE_CQL" class="ch.qos.logback.core.ConsoleAppender" >
        <encoder>
            <pattern>
                 %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %cql%n
            </pattern>
        </encoder>
    </appender>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender" >
        <encoder>
            <pattern>
                %X{Nonce} - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>


    <logger name="org.springframework.data.cassandra" additivity="false" level="DEBUG" >
        <appender-ref ref="CONSOLE_CQL" />
    </logger>

    <root level="INFO" >
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>

Now create converter the we referred in xml file

public class CqlQueryExtractor extends ClassicConverter {


    @Override
    public String convert(ILoggingEvent iLoggingEvent) {
        Object[] args = iLoggingEvent.getArgumentArray();
        for(Object arg : args){
            if(arg instanceof SimpleStatement){
                return ((SimpleStatement)arg).getQuery();
            }
        }
        return iLoggingEvent.toString();

    }
}

2) Using QueryLogger

The QueryLogger provides clients with the ability to log queries executed by the driver, and especially, it allows client to track slow queries, i.e. queries that take longer to complete than a configured threshold in milliseconds.

Cluster cluster = ...
QueryLogger queryLogger = QueryLogger.builder(cluster)
    .withConstantThreshold(...)
    .withMaxQueryStringLength(...)
.build();
cluster.register(queryLogger);

Follow the official Docs for more docs

Upvotes: 0

mp911de
mp911de

Reputation: 18137

Yes, there are multiple approaches:

  1. If you use Spring Data for Apache Cassandra version 2.0 or higher, then you can use your logging configuration to activate CQL logging. Set the log level of org.springframework.data.cassandra.core.cql.CqlTemplate to DEBUG.
  2. In any other case (or instead of 1.), use QueryLogger that is directly attached to your Cluster object. See What is a good way to discover all queries made by a Cassandra java app? for further details.

Upvotes: 2

Related Questions