Reputation: 441
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
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
Reputation: 18137
Yes, there are multiple approaches:
org.springframework.data.cassandra.core.cql.CqlTemplate
to DEBUG
.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