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

时间:2016-12-19 10:24:14

标签: java spring cassandra cassandra-2.1 spring-data-cassandra

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

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

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

我还试图添加一行:

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

但也行不通。

我的版本: 弹簧数据卡桑德拉-1.4.0 cassandra:2.1.5

4 个答案:

答案 0 :(得分:3)

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

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

如果有帮助,请告诉我。

答案 1 :(得分:3)

添加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

瞧!

答案 2 :(得分:3)

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

<块引用>

Java 驱动程序提供了一个 RequestTracker 接口。您可以通过配置 datastax-java-driver.advanced.request-tracker 命名空间中的属性来指定自己的实现或使用提供的 RequestLogger 实现。 RequestLogger 跟踪您的应用程序执行的每个查询,并具有启用成功、失败和慢速查询日志记录的选项。使用慢查询记录器来识别不在您定义的性能范围内的查询。

配置:

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.

答案 3 :(得分:0)

如果您将Spring Data用于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中永久完成。