【问题标题】:Cassandra query logging through spring configurationCassandra 通过 spring 配置查询日志记录
【发布时间】:2017-05-04 09:04:22
【问题描述】:

有什么简单的方法可以通过 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

【问题讨论】:

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


    【解决方案1】:

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

    瞧!

    【讨论】:

    • 这实际上是更好的方法,因为如果您使用 CassandraClusterFactoryBean 初始化集群,它也可以工作。
    【解决方案2】:

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

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

    如果有帮助,请告诉我。

    【讨论】:

    • 任何 application.properties 解决方案?
    【解决方案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.

    【讨论】:

    • 我正在使用这个。但是,我的 application.conf 文件没有被检测到。有没有办法在我的 spring application.properties 文件中进行此配置?
    【解决方案4】:

    如果您使用 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 中永久完成。

    【讨论】:

    • 除了 Statement 上没有实现可读的 toString() ..
    • @Abhijit 我应该在哪个文件中设置日志级别?
    • @kayeshparvez 再次阅读我答案的最后一行,或查看其他答案。
    • @Abhijit 如果我把它放在 application.properties 那么 ide 无法解析该行。 ide是intellij
    猜你喜欢
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 2017-06-06
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多