Barnabé Faliu
Barnabé Faliu

Reputation: 157

Datastax driver log level

I'd like to change the log level of Datastax driver's logger but after many tries I can't figure it out...

Here's the class I use :

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;
}

}

I tried to put a log4j.properties file in src/main/resources, change the log level programmatically, nothing is changing. I'am still getting the following traces :

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

What I can see from those traces is that sl4j is used as default logging framwork. Then how can I tell Datastax's driver to user my logger (whose properties are defined by my server).

Server used : Apache TomEE Plume 1.7.2

Datastax driver version : 2.1.9

Cassandra version : 2.2.1

Thanks.

Upvotes: 3

Views: 2401

Answers (3)

metadaddy
metadaddy

Reputation: 4419

I just hit this with a simple command-line app based on Datastax's Getting Started with Apache Cassandra and Java (Part I). After much searching and experimentation, I discovered that the solution for me was to create an almost-empty logback.xml file, with the following content:

<configuration />

Upvotes: 0

Barnab&#233; Faliu
Barnab&#233; Faliu

Reputation: 157

Thank you for your answer Olivier but this dependency already was declared in my pom.xml (I saw the page you linked before asking the question).

But it's OK I found the solution. Actually the problem was about a conflict between slf4j bindings, I saw it in TomEE's logs.

I had this binding in my pom.xml :

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

And another binding in this library :

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

concerning "logback-classic.jar"

Then the solution is to exclude it :

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

Thanks.

Upvotes: 2

Olivier Michallat
Olivier Michallat

Reputation: 2312

SLF4J is a facade, it will pick a logging framework depending on which binding is present in the classpath at runtime. Since you mention Log4j, you probably want something like:

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

See this page for more information about logging in the driver.

Upvotes: 1

Related Questions