Skip to content

Commit

Permalink
TFJ-718 make logger configurable via twitter4j.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuke committed Nov 23, 2012
1 parent ddb167f commit 02b3dab
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
Expand Up @@ -121,4 +121,6 @@ public interface Configuration extends HttpClientConfiguration
long getContributingTo();

String getDispatcherImpl();

String getLoggerFactory();
}
26 changes: 21 additions & 5 deletions twitter4j-core/src/main/java/twitter4j/conf/ConfigurationBase.java
Expand Up @@ -60,12 +60,12 @@ class ConfigurationBase implements Configuration, java.io.Serializable {
private String oAuthAuthenticationURL;

private String restBaseURL;
private String searchBaseURL;
private String streamBaseURL;
private String userStreamBaseURL;
private String siteStreamBaseURL;

private String dispatcherImpl;
private String loggerFactory;

private int asyncNumThreads;

Expand Down Expand Up @@ -176,6 +176,7 @@ protected ConfigurationBase() {
setSiteStreamBaseURL(DEFAULT_SITE_STREAM_BASE_URL);

setDispatcherImpl("twitter4j.internal.async.DispatcherImpl");
setLoggerFactory(null);

setUserStreamRepliesAllEnabled(false);
setStallWarningsEnabled(true);
Expand Down Expand Up @@ -602,6 +603,15 @@ protected final void setDispatcherImpl(String dispatcherImpl) {
this.dispatcherImpl = dispatcherImpl;
}

@Override
public String getLoggerFactory() {
return loggerFactory;
}

protected final void setLoggerFactory(String loggerImpl) {
this.loggerFactory = loggerImpl;
}

protected final void setIncludeRTsEnbled(boolean enabled) {
this.includeRTsEnabled = enabled;
}
Expand Down Expand Up @@ -706,6 +716,7 @@ public boolean equals(Object o) {
if (IS_DALVIK != that.IS_DALVIK) return false;
if (IS_GAE != that.IS_GAE) return false;
if (asyncNumThreads != that.asyncNumThreads) return false;
if (contributingTo != that.contributingTo) return false;
if (debug != that.debug) return false;
if (defaultMaxPerRoute != that.defaultMaxPerRoute) return false;
if (gzipEnabled != that.gzipEnabled) return false;
Expand All @@ -716,6 +727,7 @@ public boolean equals(Object o) {
if (httpRetryIntervalSeconds != that.httpRetryIntervalSeconds) return false;
if (httpStreamingReadTimeout != that.httpStreamingReadTimeout) return false;
if (includeEntitiesEnabled != that.includeEntitiesEnabled) return false;
if (includeMyRetweetEnabled != that.includeMyRetweetEnabled) return false;
if (includeRTsEnabled != that.includeRTsEnabled) return false;
if (jsonStoreEnabled != that.jsonStoreEnabled) return false;
if (maxTotalConnections != that.maxTotalConnections) return false;
Expand All @@ -735,6 +747,8 @@ public boolean equals(Object o) {
return false;
if (httpProxyUser != null ? !httpProxyUser.equals(that.httpProxyUser) : that.httpProxyUser != null)
return false;
if (loggerFactory != null ? !loggerFactory.equals(that.loggerFactory) : that.loggerFactory != null)
return false;
if (mediaProvider != null ? !mediaProvider.equals(that.mediaProvider) : that.mediaProvider != null)
return false;
if (mediaProviderAPIKey != null ? !mediaProviderAPIKey.equals(that.mediaProviderAPIKey) : that.mediaProviderAPIKey != null)
Expand All @@ -761,8 +775,6 @@ public boolean equals(Object o) {
if (requestHeaders != null ? !requestHeaders.equals(that.requestHeaders) : that.requestHeaders != null)
return false;
if (restBaseURL != null ? !restBaseURL.equals(that.restBaseURL) : that.restBaseURL != null) return false;
if (searchBaseURL != null ? !searchBaseURL.equals(that.searchBaseURL) : that.searchBaseURL != null)
return false;
if (siteStreamBaseURL != null ? !siteStreamBaseURL.equals(that.siteStreamBaseURL) : that.siteStreamBaseURL != null)
return false;
if (streamBaseURL != null ? !streamBaseURL.equals(that.streamBaseURL) : that.streamBaseURL != null)
Expand Down Expand Up @@ -804,14 +816,16 @@ public int hashCode() {
result = 31 * result + (oAuthAccessTokenURL != null ? oAuthAccessTokenURL.hashCode() : 0);
result = 31 * result + (oAuthAuthenticationURL != null ? oAuthAuthenticationURL.hashCode() : 0);
result = 31 * result + (restBaseURL != null ? restBaseURL.hashCode() : 0);
result = 31 * result + (searchBaseURL != null ? searchBaseURL.hashCode() : 0);
result = 31 * result + (streamBaseURL != null ? streamBaseURL.hashCode() : 0);
result = 31 * result + (userStreamBaseURL != null ? userStreamBaseURL.hashCode() : 0);
result = 31 * result + (siteStreamBaseURL != null ? siteStreamBaseURL.hashCode() : 0);
result = 31 * result + (dispatcherImpl != null ? dispatcherImpl.hashCode() : 0);
result = 31 * result + (loggerFactory != null ? loggerFactory.hashCode() : 0);
result = 31 * result + asyncNumThreads;
result = 31 * result + (int) (contributingTo ^ (contributingTo >>> 32));
result = 31 * result + (includeRTsEnabled ? 1 : 0);
result = 31 * result + (includeEntitiesEnabled ? 1 : 0);
result = 31 * result + (includeMyRetweetEnabled ? 1 : 0);
result = 31 * result + (jsonStoreEnabled ? 1 : 0);
result = 31 * result + (mbeanEnabled ? 1 : 0);
result = 31 * result + (userStreamRepliesAllEnabled ? 1 : 0);
Expand Down Expand Up @@ -857,14 +871,16 @@ public String toString() {
", oAuthAccessTokenURL='" + oAuthAccessTokenURL + '\'' +
", oAuthAuthenticationURL='" + oAuthAuthenticationURL + '\'' +
", restBaseURL='" + restBaseURL + '\'' +
", searchBaseURL='" + searchBaseURL + '\'' +
", streamBaseURL='" + streamBaseURL + '\'' +
", userStreamBaseURL='" + userStreamBaseURL + '\'' +
", siteStreamBaseURL='" + siteStreamBaseURL + '\'' +
", dispatcherImpl='" + dispatcherImpl + '\'' +
", loggerFactory='" + loggerFactory + '\'' +
", asyncNumThreads=" + asyncNumThreads +
", contributingTo=" + contributingTo +
", includeRTsEnabled=" + includeRTsEnabled +
", includeEntitiesEnabled=" + includeEntitiesEnabled +
", includeMyRetweetEnabled=" + includeMyRetweetEnabled +
", jsonStoreEnabled=" + jsonStoreEnabled +
", mbeanEnabled=" + mbeanEnabled +
", userStreamRepliesAllEnabled=" + userStreamRepliesAllEnabled +
Expand Down
Expand Up @@ -289,6 +289,12 @@ public ConfigurationBuilder setMediaProviderParameters(Properties props) {
return this;
}

public ConfigurationBuilder setLoggerImpl(String loggerImpl) {
checkNotBuilt();
configurationBean.setLoggerFactory(loggerImpl);
return this;
}

public Configuration build() {
checkNotBuilt();
configurationBean.cacheInstance();
Expand Down
Expand Up @@ -75,6 +75,7 @@ public final class PropertyConfiguration extends ConfigurationBase implements ja
public static final String INCLUDE_RTS = "includeRTs";
public static final String INCLUDE_ENTITIES = "includeEntities";
public static final String INCLUDE_MY_RETWEET = "includeMyRetweet";
public static final String LOGGER_FACTORY = "loggerFactory";
public static final String JSON_STORE_ENABLED = "jsonStoreEnabled";
public static final String MBEAN_ENABLED = "mbeanEnabled";
public static final String STREAM_USER_REPLIES_ALL = "stream.user.repliesAll";
Expand Down Expand Up @@ -345,6 +346,9 @@ private void setFieldsWithPrefix(Properties props, String prefix) {
if (notNull(props, prefix, INCLUDE_MY_RETWEET)) {
setIncludeMyRetweetEnabled(getBoolean(props, prefix, INCLUDE_MY_RETWEET));
}
if (notNull(props, prefix, LOGGER_FACTORY)) {
setLoggerFactory(getString(props, prefix, LOGGER_FACTORY));
}
if (notNull(props, prefix, JSON_STORE_ENABLED)) {
setJSONStoreEnabled(getBoolean(props, prefix, JSON_STORE_ENABLED));
}
Expand Down
Expand Up @@ -32,11 +32,19 @@ public abstract class Logger {

static {
LoggerFactory loggerFactory = null;
//-Dtwitter4j.debug=true -Dtwitter4j.loggerFactory=twitter4j.internal.logging.StdOutLoggerFactory
// -Dtwitter4j.debug=true -Dtwitter4j.loggerFactory=twitter4j.internal.logging.StdOutLoggerFactory
String loggerFactoryImpl = System.getProperty(LOGGER_FACTORY_IMPLEMENTATION);
if (loggerFactoryImpl != null) {
loggerFactory = getLoggerFactoryIfAvailable(loggerFactoryImpl, loggerFactoryImpl);
}

Configuration conf = ConfigurationContext.getInstance();
// configuration in twitter4j.properties
// loggerFactory=twitter4j.internal.logging.StdOutLoggerFactory
loggerFactoryImpl = conf.getLoggerFactory();
if (loggerFactoryImpl != null) {
loggerFactory = getLoggerFactoryIfAvailable(loggerFactoryImpl, loggerFactoryImpl);
}
// use SLF4J if it's found in the classpath
if (null == loggerFactory) {
loggerFactory = getLoggerFactoryIfAvailable("org.slf4j.impl.StaticLoggerBinder", "twitter4j.internal.logging.SLF4JLoggerFactory");
Expand All @@ -59,7 +67,6 @@ public abstract class Logger {
}
LOGGER_FACTORY = loggerFactory;

Configuration conf = ConfigurationContext.getInstance();
try {
Method method = conf.getClass().getMethod("dumpConfiguration", new Class[]{});
method.setAccessible(true);
Expand Down

0 comments on commit 02b3dab

Please sign in to comment.