Skip to content

Commit ffc1341

Browse files
authored
Merge pull request #69 from pxcurtis/bugfix/issue64
Bugfix for issue #64; purge closed loggers from factory cache
2 parents 9cd8714 + e0ed8b7 commit ffc1341

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/main/java/org/fluentd/logger/FluentLogger.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public void close() {
112112
sender.close();
113113
sender = null;
114114
}
115+
factory.purgeLogger(this);
115116
}
116117

117118
public String getName() {

src/main/java/org/fluentd/logger/FluentLoggerFactory.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919

2020
import java.lang.reflect.Constructor;
2121
import java.lang.reflect.InvocationTargetException;
22+
import java.util.ArrayList;
23+
import java.util.Iterator;
2224
import java.util.Map;
25+
import java.util.Map.Entry;
2326
import java.util.Properties;
2427
import java.util.WeakHashMap;
2528

@@ -80,6 +83,18 @@ public synchronized FluentLogger getLogger(String tagPrefix, String host, int po
8083
return logger;
8184
}
8285

86+
/** Purges an invalid logger from the cache.
87+
*/
88+
protected synchronized void purgeLogger(FluentLogger logger) {
89+
Iterator<Entry<FluentLogger, String>> it = loggers.entrySet().iterator();
90+
while (it.hasNext()) {
91+
if (it.next().getKey() == logger) {
92+
it.remove();
93+
return;
94+
}
95+
}
96+
}
97+
8398
@SuppressWarnings("unchecked")
8499
private Sender createSenderInstance(final String className, final Object[] params) throws ClassNotFoundException,
85100
SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException,
@@ -98,7 +113,7 @@ Map<FluentLogger, String> getLoggers() {
98113
}
99114

100115
public synchronized void closeAll() {
101-
for (FluentLogger logger : loggers.keySet()) {
116+
for (FluentLogger logger : new ArrayList<FluentLogger>(loggers.keySet())) {
102117
logger.close();
103118
}
104119
loggers.clear();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.fluentd.logger;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Collections;
6+
7+
import org.junit.Test;
8+
9+
public class TestBugfixes {
10+
/** Prior to the issue fix, this test fails with an NPE on the last line.
11+
*/
12+
@Test
13+
public void validLoggerReturned_whenOpenThenCloseThenOpenWithSameParameters() {
14+
// use test sender so we don't need to have an actual fluentd running...
15+
System.setProperty(Config.FLUENT_SENDER_CLASS, "org.fluentd.logger.sender.NullSender");
16+
FluentLogger logger = FluentLogger.getLogger("test");
17+
18+
// this works
19+
logger.log("tag", Collections.<String, Object>emptyMap());
20+
21+
// now close it; sender is closed and set to null
22+
logger.close();
23+
assertEquals(null, logger.sender);
24+
25+
// get another logger with the exact same parameters; we'd expect this to work, yes?
26+
FluentLogger logger2 = FluentLogger.getLogger("test");
27+
28+
// let's see if it does
29+
logger2.log("tag", Collections.<String, Object>emptyMap());
30+
}
31+
}

0 commit comments

Comments
 (0)