【问题标题】:Datastax driver log levelDatastax 驱动程序日志级别
【发布时间】:2016-03-06 14:27:57
【问题描述】:

我想更改 Datastax 驱动程序记录器的日志级别,但经过多次尝试后我无法弄清楚...

这是我使用的类:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Metadata;

import com.datastax.driver.core.Session;
import com.datastax.driver.mapping.MappingManager;

public class CassandraSession {

/**
 * CassandraSession singleton
 */
private static CassandraSession INSTANCE = null;
/**
 * The Cassandra Cluster
 */
private static Cluster cluster;
/**
 * The Cassandra Session
 */
private static Session session;
/**
 * MappingManager is used to create Cassandra mappers
 */
private static MappingManager manager;
/**
 * LOGGER
 */
private static final Logger LOGGER = Logger.getLogger(CassandraSession.class);
/**
 * Keyspace Name
 */
private static final String KEYSPACE = "MY_KEYSPACE";

/**
 * CassandraSession
 */
private CassandraSession() {
    initialize();
}

/**
 * This method initializes the connection with the Cassandra Database
 */
private void initialize() {
    cluster = Cluster.builder().withClusterName("TestCluster").addContactPoints("127.0.0.1").withPort(9042).build();
    final Metadata metadata = cluster.getMetadata();
    LOGGER.info("Connected to cluster: " + metadata.getClusterName());
}

/**
 * Get the instance of the singleton CassandraSession
 *
 * @return
 */
public static synchronized CassandraSession getInstance() {
    if (INSTANCE == null) {
        INSTANCE = new CassandraSession();
    }
    return INSTANCE;
}

/**
 * Get the Cassandra Session
 *
 * @return
 */
public Session getSession() {
    if (session == null) {
        session = cluster.connect(KEYSPACE);
    }
    return session;
}

/**
 * Get the Cassandra MappingManager
 *
 * @return
 */
public MappingManager getManager() {
    if (manager == null) {
        manager = new MappingManager(session);
    }
    return manager;
}

}

我尝试将 log4j.properties 文件放在 src/main/resources 中,以编程方式更改日志级别,但没有任何变化。我仍然得到以下痕迹:

11:39:48.762 [http-bio-8080-exec-6] DEBUG c.d.driver.core.SystemProperties - com.datastax.driver.NEW_NODE_DELAY_SECONDS is undefined, using default value 1
11:39:48.768 [http-bio-8080-exec-6] DEBUG c.d.driver.core.SystemProperties - com.datastax.driver.NON_BLOCKING_EXECUTOR_SIZE is undefined, using default value 8
11:39:48.770 [http-bio-8080-exec-6] DEBUG c.d.driver.core.SystemProperties - com.datastax.driver.NOTIF_LOCK_TIMEOUT_SECONDS is undefined, using default value 60
11:39:48.812 [http-bio-8080-exec-6] DEBUG com.datastax.driver.core.Cluster - Starting new cluster with contact points [/127.0.0.1:9042]
11:39:48.827 [http-bio-8080-exec-6] DEBUG i.n.u.i.l.InternalLoggerFactory - Using SLF4J as the default logging framework
11:39:48.924 [http-bio-8080-exec-6] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
11:39:48.924 [http-bio-8080-exec-6] DEBUG i.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
11:39:48.924 [http-bio-8080-exec-6] DEBUG i.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
11:39:48.925 [http-bio-8080-exec-6] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: true
11:39:48.926 [http-bio-8080-exec-6] DEBUG i.n.util.internal.PlatformDependent - Platform: Windows
11:39:48.926 [http-bio-8080-exec-6] DEBUG i.n.util.internal.PlatformDependent - Java version: 8
11:39:48.926 [http-bio-8080-exec-6] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noUnsafe: false
11:39:48.926 [http-bio-8080-exec-6] DEBUG i.n.util.internal.PlatformDependent - sun.misc.Unsafe: available
11:39:48.927 [http-bio-8080-exec-6] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noJavassist: false
11:39:48.929 [http-bio-8080-exec-6] DEBUG i.n.util.internal.PlatformDependent - Javassist: unavailable

我可以从这些跟踪中看到 sl4j 被用作默认的日志记录框架。那么如何告诉 Datastax 的驱动程序使用我的记录器(其属性由我的服务器定义)。

使用的服务器:Apache TomEE Plume 1.7.2

Datastax 驱动程序版本:2.1.9

Cassandra 版本:2.2.1

谢谢。

【问题讨论】:

    标签: java logging cassandra datastax apache-tomee


    【解决方案1】:

    感谢 Olivier 的回答,但这种依赖关系已经在我的 pom.xml 中声明(我在提问之前看到了您链接的页面)。

    但没关系,我找到了解决方案。实际上问题是关于 slf4j 绑定之间的冲突,我在 TomEE 的日志中看到了。

    我的 pom.xml 中有这个绑定:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.12</version>
    </dependency>
    

    还有这个库中的另一个绑定:

    <dependency>
        <groupId>org.apache.cassandra</groupId>
        <artifactId>cassandra-all</artifactId>
        <version>2.2.1</version>
    </dependency>
    

    关于“logback-classic.jar”

    那么解决办法就是排除它:

    <dependency>
        <groupId>org.apache.cassandra</groupId>
        <artifactId>cassandra-all</artifactId>
        <version>2.2.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
            </exclusions>
    </dependency>
    

    谢谢。

    【讨论】:

      【解决方案2】:

      SLF4J 是一个外观,它将根据运行时类路径中存在的绑定来选择一个日志框架。既然你提到了 Log4j,你可能想要这样的东西:

      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.12</version>
      </dependency>
      

      有关登录驱动程序的更多信息,请参阅this page

      【讨论】:

        【解决方案3】:

        我刚刚使用基于 Datastax 的 Getting Started with Apache Cassandra and Java (Part I) 的简单命令行应用程序来实现这一点。经过大量搜索和实验,我发现我的解决方案是创建一个几乎为空的logback.xml 文件,其内容如下:

        <configuration />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-07-09
          • 2016-02-27
          • 2017-01-20
          • 2017-01-26
          • 1970-01-01
          • 2016-11-29
          • 2016-11-29
          相关资源
          最近更新 更多