Cassandra query logging through spring configuration

8.4k views Asked by At

is there any easy way to turn on query logging on cassandra through xml configuration? I'm using namespace:

xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"

but I can't find any suitable solution. I was trying to turn on trace through cqlsh, but it dosen't work for my app.

I was trying also to add line:

<logger name="com.datastax.driver.core.QueryLogger.NORMAL" level="TRACE" />

But also doesn't work.

My versions: spring-data-cassandra-1.4.0 cassandra: 2.1.5

4

There are 4 answers

1
Matija Gobec On BEST ANSWER

Please check out this link and check if you added the query logger to your cluster definition like stated:

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

Let me know if it helped.

5
Abhijit Sarkar On

If you're using 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, no need to mess with QueryLogger:

-Dlogging.level.org.springframework.data.cassandra.core.cql.CqlTemplate=DEBUG

This can, of course, be permanently done in the application.properties.

1
Marius Jaraminas On

If you are using Spring Data Cassandra 2.4+ QueryLogger is not available anymore, it was replaced with RequestTracker which can be configured in application.yml or overridden depending on your needs.

The Java driver provides a RequestTracker interface. You can specify an implementation of your own or use the provided RequestLogger implementation by configuring the properties in the datastax-java-driver.advanced.request-tracker namespace. The RequestLogger tracks every query your application executes and has options to enable logging for successful, failed, and slow queries. Use the slow query logger to identify queries that are not within your defined performance.

Configuration:

datastax-java-driver.advanced.request-tracker {
  class = RequestLogger

  logs {
    # Whether to log successful requests.
    success.enabled = true

    slow {
      # The threshold to classify a successful request as "slow". If this is unset, all
      # successful requests will be considered as normal.
      threshold = 1 second

      # Whether to log slow requests.
      enabled = true
    }

    # Whether to log failed requests.
    error.enabled = true

    # The maximum length of the query string in the log message. If it is longer than that, it
    # will be truncated.
    max-query-length = 500

    # Whether to log bound values in addition to the query string.
    show-values = true

    # The maximum length for bound values in the log message. If the formatted representation of
    # a value is longer than that, it will be truncated.
    max-value-length = 50

    # The maximum number of bound values to log. If a request has more values, the list of
    # values will be truncated.
    max-values = 50

    # Whether to log stack traces for failed queries. If this is disabled, the log will just
    # include the exception's string representation (generally the class name and message).
    show-stack-traces = true
}

More details.

1
Hartmut On

Add a QueryLogger @Bean and get the Cluster @Autowired in:

@Bean
public QueryLogger queryLogger(Cluster cluster) {
    QueryLogger queryLogger = QueryLogger.builder()
            .build();
    cluster.register(queryLogger);
    return queryLogger;
}

(+ obviously configure QueryLogger.Builder as required).

Don't forget to set log levels to DEBUG/TRACE in your application.yml:

logging.level.com.datastax.driver.core.QueryLogger.NORMAL: DEBUG
logging.level.com.datastax.driver.core.QueryLogger.SLOW: TRACE

VoilĂ !