1

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

Thanks.

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
venkat g
  • 421
  • 1
  • 6
  • 20

2 Answers2

2

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.
mp911de
  • 17,546
  • 2
  • 55
  • 95
  • 3
    In case 1, the logger logs `DEBUG CqlTemplate - Executing CQL Statement [com.datastax.oss.driver.internal.core.cql.DefaultSimpleStatement@50187558]` which is not really relevant since the toString on DefaultSimpleStatement is not implemented as expected.. – Fabien MIFSUD May 22 '20 at 15:11
  • Fixed after version 3.0.5 – Adriano Feb 28 '23 at 16:29
0

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

saikiran
  • 9
  • 4