Skip to content

Commit 8bd1b13

Browse files
Juan Pablo Diaz-VazJuan Pablo Diaz-Vaz
authored andcommitted
SUMOK-17 Added option to use Log4j to Config properties
1 parent 3e5f58b commit 8bd1b13

4 files changed

Lines changed: 44 additions & 25 deletions

File tree

src/main/java/com/mcplusa/sumologic/KinesisConnectorForSumologicConfiguration.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,25 @@
1414
public class KinesisConnectorForSumologicConfiguration extends KinesisConnectorConfiguration {
1515
// Properties added for Sumologic
1616
public static final String PROP_SUMOLOGIC_URL = "sumologicUrl";
17+
public static final String PROP_SUMOLOGIC_USE_LOG4J = "useLog4j";
1718
public final String SUMOLOGIC_URL;
19+
public final boolean SUMOLOGIC_USE_LOG4J;
20+
21+
public final boolean DEFAULT_SUMOLOGIC_USE_LOG4J = false;
1822

1923
/**
2024
* Configure the connector application with any set of properties that are unique to the application. Any
2125
* unspecified property will be set to a default value.
2226
*/
2327
public KinesisConnectorForSumologicConfiguration(Properties properties, AWSCredentialsProvider credentialsProvider) {
2428
super(properties, credentialsProvider);
25-
2629
SUMOLOGIC_URL = properties.getProperty(PROP_SUMOLOGIC_URL, null);
30+
SUMOLOGIC_USE_LOG4J = getBooleanProperty(PROP_SUMOLOGIC_USE_LOG4J,
31+
DEFAULT_SUMOLOGIC_USE_LOG4J, properties);
2732
}
33+
34+
private boolean getBooleanProperty(String property, boolean defaultValue, Properties properties) {
35+
String propertyValue = properties.getProperty(property, Boolean.toString(defaultValue));
36+
return Boolean.parseBoolean(propertyValue);
37+
}
2838
}

src/main/java/com/mcplusa/sumologic/SumologicSender.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121

2222
public class SumologicSender {
2323
private static final Log LOG = LogFactory.getLog(SumologicSender.class);
24-
25-
private static final boolean USE_LOG4J = true;
26-
24+
2725
private String url = null;
2826
private HttpClient httpClient = null;
2927

@@ -32,31 +30,33 @@ public class SumologicSender {
3230
private static final int RETRIES = 3;
3331
private static final int SLEEP_TIME = 1000;
3432

35-
public SumologicSender(String url) {
33+
private boolean useLog4j = false;
34+
35+
public SumologicSender(String url, boolean useLog4j) {
3636
this.url = url;
37+
this.useLog4j = useLog4j;
3738

38-
HttpParams params = new BasicHttpParams();
39-
HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
40-
HttpConnectionParams.setSoTimeout(params, socketTimeout);
41-
httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(), params);
39+
if (!useLog4j) {
40+
HttpParams params = new BasicHttpParams();
41+
HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
42+
HttpConnectionParams.setSoTimeout(params, socketTimeout);
43+
httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(), params);
44+
}
4245
}
4346

4447
public boolean sendToSumologicUsingLog4j(String data) {
4548
Logger sumologicLog = Logger.getLogger("sumologic");
4649
sumologicLog.trace(data);
4750
return true;
4851
}
49-
50-
public boolean sendToSumologic(String data) throws IOException{
51-
if (USE_LOG4J)
52-
return sendToSumologicUsingLog4j(data);
53-
52+
53+
public boolean sendToSumologicUsingHTTPRequest(String data) throws IOException {
5454
int retries = RETRIES;
55-
int sleep_time = SLEEP_TIME;
56-
int statusCode;
57-
58-
do {
59-
HttpPost post = null;
55+
int sleep_time = SLEEP_TIME;
56+
int statusCode;
57+
58+
do {
59+
HttpPost post = null;
6060
post = new HttpPost(url);
6161
post.setEntity(new StringEntity(data, HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8));
6262
HttpResponse response = httpClient.execute(post);
@@ -74,7 +74,7 @@ public boolean sendToSumologic(String data) throws IOException{
7474
Thread.sleep(sleep_time);
7575
} catch (InterruptedException ignore) {}
7676
}
77-
} while (statusCode == 429 && retries > 0);
77+
} while (statusCode == 429 && retries > 0);
7878

7979
// Check if the request was successful;
8080
if (statusCode != 200) {
@@ -85,4 +85,13 @@ public boolean sendToSumologic(String data) throws IOException{
8585
return true;
8686
}
8787
}
88+
89+
public boolean sendToSumologic(String data) throws IOException{
90+
if (this.useLog4j) {
91+
return sendToSumologicUsingLog4j(data);
92+
}
93+
else {
94+
return sendToSumologicUsingHTTPRequest(data);
95+
}
96+
}
8897
}

src/main/java/com/mcplusa/sumologic/implementations/SumologicEmitter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public class SumologicEmitter implements IEmitter<String> {
3131

3232
public SumologicEmitter(KinesisConnectorConfiguration configuration) {
3333
this.config = (KinesisConnectorForSumologicConfiguration) configuration;
34-
sender = new SumologicSender(this.config.SUMOLOGIC_URL);
34+
sender = new SumologicSender(config.SUMOLOGIC_URL, config.SUMOLOGIC_USE_LOG4J);
3535
}
3636

37-
public SumologicEmitter(String url) {
38-
sender = new SumologicSender(url);
37+
public SumologicEmitter(String url, boolean useLog4j) {
38+
sender = new SumologicSender(url, useLog4j);
3939
}
4040

4141
@Override

src/test/java/com/mcplusa/sumologic/implementations/SumologicEmitterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void theEmitterShouldReturnTheListParameterWhenFailing () {
3939
messages.add("This is message #3");
4040
messages.add("This is message #4");
4141

42-
SumologicEmitter emitter = new SumologicEmitter(url);
42+
SumologicEmitter emitter = new SumologicEmitter(url, false);
4343
List <String> notEmittedMessages = emitter.sendBatchConcatenating(messages);
4444

4545
Assert.assertEquals(messages, notEmittedMessages);
@@ -55,7 +55,7 @@ public void theEmitterShouldReturnAnEmptyListOnSuccess () {
5555
messages.add("This is message #3");
5656
messages.add("This is message #4");
5757

58-
SumologicEmitter emitter = new SumologicEmitter(url);
58+
SumologicEmitter emitter = new SumologicEmitter(url, false);
5959
List <String> notEmittedMessages = emitter.sendBatchConcatenating(messages);
6060

6161
Assert.assertEquals(0, notEmittedMessages.size());

0 commit comments

Comments
 (0)