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:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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}.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user