通过 spring 配置的 Cassandra 查询日志记录

Cassandra query logging through spring configuration

有什么简单的方法可以通过 xml 配置在 cassandra 上打开查询日志记录吗?我正在使用命名空间:

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

但我找不到任何合适的解决方案。我试图通过 cqlsh 打开跟踪,但它对我的应用程序不起作用。

我也在尝试添加行:

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

但还是不行。

我的版本: spring-data-cassandra-1.4.0 卡桑德拉:2.1.5

请查看此 link 并检查您是否将查询记录器添加到集群定义中,如所述:

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

如果有帮助请告诉我。

添加一个 QueryLogger @Bean 并获取 Cluster @Autowired in:

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

(+显然根据需要配置QueryLogger.Builder).

不要忘记在 application.yml:

中将日志级别设置为 DEBUG/TRACE
logging.level.com.datastax.driver.core.QueryLogger.NORMAL: DEBUG
logging.level.com.datastax.driver.core.QueryLogger.SLOW: TRACE

瞧!

如果您正在使用 Spring Data for Apache Cassandra 2.0 或更高版本,那么您可以使用您的日志记录配置来激活 CQL 日志记录。设置org.springframework.data.cassandra.core.cql.CqlTemplate的日志级别为DEBUG,不用乱用QueryLogger:

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

当然,这可以在 application.properties 中永久完成。

如果您使用的是 Spring Data Cassandra 2.4+ QueryLogger 不再可用,它已被 RequestTracker 取代可以根据您的需要在 application.yml 中配置或覆盖。

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.

配置:

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.