DATACOUCH-14 - Enable SLF4J as default logging.

If the whole system is constructed through the AbstractCouchbaseConfiguration or
the custom <couchbase> xml config, the logger is specified automatically.

Please note that this also changes the java config a little bit, but it should
also make it easier to construct it.
This commit is contained in:
Michael Nitschinger
2014-01-23 16:24:58 +01:00
parent 46b33e9952
commit 7b6a3a9894
7 changed files with 123 additions and 24 deletions

View File

@@ -23,6 +23,7 @@ import org.springframework.context.annotation.ClassPathScanningCandidateComponen
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.couchbase.core.CouchbaseFactoryBean;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
import org.springframework.data.couchbase.core.convert.translation.JacksonTranslationService;
@@ -32,8 +33,9 @@ import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import java.util.HashSet;
import java.util.Set;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
/**
* Base class for Spring Data Couchbase configuration using JavaConfig.
@@ -43,13 +45,62 @@ import java.util.Set;
@Configuration
public abstract class AbstractCouchbaseConfiguration {
/**
* The list of hostnames (or IP addresses to bootstrap from).
*
* @return the list of bootstrap hosts.
*/
protected abstract List<String> bootstrapHosts();
/**
* The name of the bucket to connect to.
*
* @return the name of the bucket.
*/
protected abstract String getBucketName();
/**
* The password of the bucket (can be an empty string).
*
* @return the password of the bucket.
*/
protected abstract String getBucketPassword();
/**
* Return the {@link CouchbaseClient} instance to connect to.
*
* @throws Exception on Bean construction failure.
*/
@Bean(destroyMethod = "shutdown")
public abstract CouchbaseClient couchbaseClient() throws Exception;
public CouchbaseClient couchbaseClient() throws Exception {
setLoggerProperty(couchbaseLogger());
return new CouchbaseClient(
bootstrapUris(bootstrapHosts()),
getBucketName(),
getBucketPassword()
);
}
/**
* Specifies the logger to use (defaults to SLF4J).
*
* @return the logger property string.
*/
protected String couchbaseLogger() {
return CouchbaseFactoryBean.DEFAULT_LOGGER_PROPERTY;
}
/**
* Prepare the logging property before initializing couchbase.
*
* @param logger
*/
private void setLoggerProperty(String logger) {
Properties systemProperties = System.getProperties();
systemProperties.put("net.spy.log.LoggerImpl", logger);
System.setProperties(systemProperties);
}
/**
* Creates a {@link CouchbaseTemplate}.
@@ -130,4 +181,19 @@ public abstract class AbstractCouchbaseConfiguration {
return getClass().getPackage().getName();
}
/**
* Converts the given list of hostnames into parsable URIs.
*
* @param hosts the list of hosts to convert.
* @return the converted URIs.
*/
private List<URI> bootstrapUris(List<String> hosts) throws URISyntaxException {
List<URI> uris = new ArrayList<URI>();
for (String host : hosts) {
uris.add(new URI("http://" + host + ":8091/pools"));
}
return uris;
}
}

View File

@@ -30,6 +30,7 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Parser for "<couchbase:couchbase />" bean definitions.
@@ -68,6 +69,14 @@ public class CouchbaseParser extends AbstractSingleBeanDefinitionParser {
bean.addConstructorArgValue(StringUtils.hasText(password) ? password : CouchbaseFactoryBean.DEFAULT_PASSWORD);
bean.setDestroyMethodName(CouchbaseFactoryBean.DEFAULT_DESTROY_METHOD);
setLogger();
}
private void setLogger() {
Properties systemProperties = System.getProperties();
systemProperties.put("net.spy.log.LoggerImpl", CouchbaseFactoryBean.DEFAULT_LOGGER_PROPERTY);
System.setProperties(systemProperties);
}
/**

View File

@@ -64,6 +64,11 @@ public class CouchbaseFactoryBean implements FactoryBean<CouchbaseClient>, Initi
*/
public static final String DEFAULT_DESTROY_METHOD = "shutdown";
/**
* Use SLF4J as the default logger if not instructed otherwise.
*/
public static final String DEFAULT_LOGGER_PROPERTY = "net.spy.memcached.compat.log.SLF4JLogger";
/**
* Holds the enclosed {@link CouchbaseClient}.
*/