1919
2020import java .lang .reflect .Constructor ;
2121import java .lang .reflect .InvocationTargetException ;
22+ import java .util .Collections ;
2223import java .util .Map ;
2324import java .util .Properties ;
2425import java .util .WeakHashMap ;
@@ -33,7 +34,7 @@ public class FluentLoggerFactory {
3334 private final Map <String , FluentLogger > loggers ;
3435
3536 public FluentLoggerFactory () {
36- loggers = new WeakHashMap <String , FluentLogger >();
37+ loggers = Collections . synchronizedMap ( new WeakHashMap <String , FluentLogger >() );
3738 }
3839
3940 public FluentLogger getLogger (String tagPrefix ) {
@@ -48,7 +49,7 @@ public FluentLogger getLogger(String tagPrefix, String host, int port, int timeo
4849 return getLogger (tagPrefix , host , port , timeout , bufferCapacity , new ExponentialDelayReconnector ());
4950 }
5051
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 ,
5253 Reconnector reconnector ) {
5354 String key = String .format ("%s_%s_%d_%d_%d" , new Object [] { tagPrefix , host , port , timeout , bufferCapacity });
5455 if (loggers .containsKey (key )) {
@@ -73,6 +74,21 @@ public synchronized FluentLogger getLogger(String tagPrefix, String host, int po
7374 }
7475 }
7576
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+
7692 @ SuppressWarnings ("unchecked" )
7793 private Sender createSenderInstance (final String className , final Object [] params ) throws ClassNotFoundException ,
7894 SecurityException , NoSuchMethodException , IllegalArgumentException , InstantiationException ,
0 commit comments