Skip to content

Commit 60187cf

Browse files
committed
Bugfix for issue #64; purge closed loggers from factory cache
1 parent 68c0c50 commit 60187cf

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
import java.lang.reflect.Constructor;
2121
import java.lang.reflect.InvocationTargetException;
22+
import java.util.Iterator;
2223
import java.util.Map;
24+
import java.util.Map.Entry;
2325
import java.util.Properties;
2426
import java.util.WeakHashMap;
2527

@@ -80,6 +82,18 @@ public synchronized FluentLogger getLogger(String tagPrefix, String host, int po
8082
return logger;
8183
}
8284

85+
/** Purges an invalid logger from the cache.
86+
*/
87+
protected synchronized void purgeLogger(FluentLogger logger) {
88+
Iterator<Entry<FluentLogger, String>> it = loggers.entrySet().iterator();
89+
while (it.hasNext()) {
90+
if (it.next().getKey() == logger) {
91+
it.remove();
92+
return;
93+
}
94+
}
95+
}
96+
8397
@SuppressWarnings("unchecked")
8498
private Sender createSenderInstance(final String className, final Object[] params) throws ClassNotFoundException,
8599
SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException,
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)