19
19
20
20
import java .lang .reflect .Constructor ;
21
21
import java .lang .reflect .InvocationTargetException ;
22
+ import java .util .Collections ;
22
23
import java .util .Map ;
23
24
import java .util .Properties ;
24
25
import java .util .WeakHashMap ;
@@ -33,7 +34,7 @@ public class FluentLoggerFactory {
33
34
private final Map <String , FluentLogger > loggers ;
34
35
35
36
public FluentLoggerFactory () {
36
- loggers = new WeakHashMap <String , FluentLogger >();
37
+ loggers = Collections . synchronizedMap ( new WeakHashMap <String , FluentLogger >() );
37
38
}
38
39
39
40
public FluentLogger getLogger (String tagPrefix ) {
@@ -48,7 +49,7 @@ public FluentLogger getLogger(String tagPrefix, String host, int port, int timeo
48
49
return getLogger (tagPrefix , host , port , timeout , bufferCapacity , new ExponentialDelayReconnector ());
49
50
}
50
51
51
- public synchronized FluentLogger getLogger (String tagPrefix , String host , int port , int timeout , int bufferCapacity ,
52
+ public FluentLogger getLogger (String tagPrefix , String host , int port , int timeout , int bufferCapacity ,
52
53
Reconnector reconnector ) {
53
54
String key = String .format ("%s_%s_%d_%d_%d" , new Object [] { tagPrefix , host , port , timeout , bufferCapacity });
54
55
if (loggers .containsKey (key )) {
@@ -73,6 +74,21 @@ public synchronized FluentLogger getLogger(String tagPrefix, String host, int po
73
74
}
74
75
}
75
76
77
+ public FluentLogger getLogger (String tagPrefix , String host , int port , int timeout , int bufferCapacity ,
78
+ Sender sender ) {
79
+ if (sender == null ) {
80
+ return getLogger (tagPrefix , host , port , timeout , bufferCapacity );
81
+ }
82
+ String key = String .format ("%s_%s_%d_%d_%d_%s" , new Object [] { tagPrefix , host , port , timeout , bufferCapacity , sender == null ? "null" : sender .getName () });
83
+ if (loggers .containsKey (key )) {
84
+ return loggers .get (key );
85
+ } else {
86
+ FluentLogger logger = new FluentLogger (tagPrefix , sender );
87
+ loggers .put (key , logger );
88
+ return logger ;
89
+ }
90
+ }
91
+
76
92
@ SuppressWarnings ("unchecked" )
77
93
private Sender createSenderInstance (final String className , final Object [] params ) throws ClassNotFoundException ,
78
94
SecurityException , NoSuchMethodException , IllegalArgumentException , InstantiationException ,
0 commit comments