Setting up logging
The Couchbase Java SDK has no hard dependency on a specific logger implementation. It tries to find one on the classpath and uses it if supported. If no logger implementation is found, the standard JDK logger is used.
The following loggers are supported (and tried in this order):
- SLF4J
- Log4J
- JDK Logger (java.util.logging)
Configuring SLF4J
To enable SLF4J, put it on the classpath, as well as one of the support logger implementations (like logback). If you want to use logback and include logback-classic, it will be pulled in automatically:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
</dependency>
By default, the log level for logback is set to DEBUG, but with the addition of a logback configuration this can be configured (for example, as a logback.xml in the resources folder):
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Consult the SLF4J documentation for advanced configuration.
Configuring Log4J
Log4J is also automatically found when put on the classpath:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
If no configuration is applied, the following message appears:
log4j:WARN No appenders could be found for logger (com.couchbase.client.core.logging.CouchbaseLoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
This log4j.xml sets it to INFO level:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="CA" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
</layout>
</appender>
<root>
<level value="info" />
<appender-ref ref="CA" />
</root>
</log4j:configuration>
Alternatively, you can provide a log4j.properties file with similar settings:
log4j.rootLogger=INFO, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Consult the Log4J documentation for more information and advanced configuration options.
Configuring the JDK Logger
If no logging library is found on the classpath, the JDK logger (also known as JUL from java.util.logging) is used as a fallback.
By default it logs INFO level and above. If you want to set it to DEBUG (or the JUL equivalent: Fine) you can do it like this programmatically before initializing the CouchbaseCluster object:
Logger logger = Logger.getLogger("com.couchbase.client");
logger.setLevel(Level.FINE);
for(Handler h : logger.getParent().getHandlers()) {
if(h instanceof ConsoleHandler){
h.setLevel(Level.FINE);
}
}
You should not use JUL in production because SLF4J and Log4J provide better configuration options and performance.
Manual Overriding
If different loggers are on the classpath and you want to force a different one (for example Log4J instead of SLF4J) you can set the default one directly:
CouchbaseLoggerFactory.setDefaultFactory(new Log4JLoggerFactory());