Make all InternalLoggerFactory implementations be singletons

Motivation:

It's better to make all InternalLoggerFactory implementations be singletons according to the discussions in #5047

Modifications:

Make all InternalLoggerFactory implementations be singletons and hide the construtors.

Result:

All InternalLoggerFactory implementations be singletons.
This commit is contained in:
Xiaoyan Lin 2016-04-07 01:12:11 -07:00 committed by Norman Maurer
parent 560e0946b3
commit b2651cc351
11 changed files with 49 additions and 9 deletions

View File

@ -28,7 +28,14 @@ import java.util.Map;
*/ */
public class CommonsLoggerFactory extends InternalLoggerFactory { public class CommonsLoggerFactory extends InternalLoggerFactory {
Map<String, InternalLogger> loggerMap = new HashMap<String, InternalLogger>(); public static final InternalLoggerFactory INSTANCE = new CommonsLoggerFactory();
/**
* @deprecated Use {@link #INSTANCE} instead.
*/
@Deprecated
public CommonsLoggerFactory() {
}
@Override @Override
public InternalLogger newInstance(String name) { public InternalLogger newInstance(String name) {

View File

@ -23,7 +23,7 @@ package io.netty.util.internal.logging;
* {@link JdkLoggerFactory} is used. You can change it to your preferred * {@link JdkLoggerFactory} is used. You can change it to your preferred
* logging framework before other Netty classes are loaded: * logging framework before other Netty classes are loaded:
* <pre> * <pre>
* {@link InternalLoggerFactory}.setDefaultFactory(new {@link Log4JLoggerFactory}()); * {@link InternalLoggerFactory}.setDefaultFactory({@link Log4JLoggerFactory}.INSTANCE);
* </pre> * </pre>
* Please note that the new default factory is effective only for the classes * Please note that the new default factory is effective only for the classes
* which were loaded after the default factory is changed. Therefore, * which were loaded after the default factory is changed. Therefore,
@ -42,10 +42,10 @@ public abstract class InternalLoggerFactory {
f.newInstance(name).debug("Using SLF4J as the default logging framework"); f.newInstance(name).debug("Using SLF4J as the default logging framework");
} catch (Throwable t1) { } catch (Throwable t1) {
try { try {
f = new Log4JLoggerFactory(); f = Log4JLoggerFactory.INSTANCE;
f.newInstance(name).debug("Using Log4J as the default logging framework"); f.newInstance(name).debug("Using Log4J as the default logging framework");
} catch (Throwable t2) { } catch (Throwable t2) {
f = new JdkLoggerFactory(); f = JdkLoggerFactory.INSTANCE;
f.newInstance(name).debug("Using java.util.logging as the default logging framework"); f.newInstance(name).debug("Using java.util.logging as the default logging framework");
} }
} }

View File

@ -25,6 +25,15 @@ import java.util.logging.Logger;
*/ */
public class JdkLoggerFactory extends InternalLoggerFactory { public class JdkLoggerFactory extends InternalLoggerFactory {
public static final InternalLoggerFactory INSTANCE = new JdkLoggerFactory();
/**
* @deprecated Use {@link #INSTANCE} instead.
*/
@Deprecated
public JdkLoggerFactory() {
}
@Override @Override
public InternalLogger newInstance(String name) { public InternalLogger newInstance(String name) {
return new JdkLogger(Logger.getLogger(name)); return new JdkLogger(Logger.getLogger(name));

View File

@ -19,6 +19,15 @@ import org.apache.logging.log4j.LogManager;
public final class Log4J2LoggerFactory extends InternalLoggerFactory { public final class Log4J2LoggerFactory extends InternalLoggerFactory {
public static final InternalLoggerFactory INSTANCE = new Log4J2LoggerFactory();
/**
* @deprecated Use {@link #INSTANCE} instead.
*/
@Deprecated
public Log4J2LoggerFactory() {
}
@Override @Override
public InternalLogger newInstance(String name) { public InternalLogger newInstance(String name) {
return new Log4J2Logger(LogManager.getLogger(name)); return new Log4J2Logger(LogManager.getLogger(name));

View File

@ -24,6 +24,15 @@ import org.apache.log4j.Logger;
*/ */
public class Log4JLoggerFactory extends InternalLoggerFactory { public class Log4JLoggerFactory extends InternalLoggerFactory {
public static final InternalLoggerFactory INSTANCE = new Log4JLoggerFactory();
/**
* @deprecated Use {@link #INSTANCE} instead.
*/
@Deprecated
public Log4JLoggerFactory() {
}
@Override @Override
public InternalLogger newInstance(String name) { public InternalLogger newInstance(String name) {
return new Log4JLogger(Logger.getLogger(name)); return new Log4JLogger(Logger.getLogger(name));

View File

@ -29,6 +29,12 @@ import java.io.UnsupportedEncodingException;
*/ */
public class Slf4JLoggerFactory extends InternalLoggerFactory { public class Slf4JLoggerFactory extends InternalLoggerFactory {
public static final InternalLoggerFactory INSTANCE = new Slf4JLoggerFactory();
/**
* @deprecated Use {@link #INSTANCE} instead.
*/
@Deprecated
public Slf4JLoggerFactory() { public Slf4JLoggerFactory() {
} }

View File

@ -23,7 +23,7 @@ public class CommonsLoggerFactoryTest {
@Test @Test
public void testCreation() { public void testCreation() {
InternalLogger logger = new CommonsLoggerFactory().newInstance("foo"); InternalLogger logger = CommonsLoggerFactory.INSTANCE.newInstance("foo");
assertTrue(logger instanceof CommonsLogger); assertTrue(logger instanceof CommonsLogger);
assertEquals("foo", logger.name()); assertEquals("foo", logger.name());
} }

View File

@ -23,7 +23,7 @@ public class JdkLoggerFactoryTest {
@Test @Test
public void testCreation() { public void testCreation() {
InternalLogger logger = new JdkLoggerFactory().newInstance("foo"); InternalLogger logger = JdkLoggerFactory.INSTANCE.newInstance("foo");
assertTrue(logger instanceof JdkLogger); assertTrue(logger instanceof JdkLogger);
assertEquals("foo", logger.name()); assertEquals("foo", logger.name());
} }

View File

@ -24,7 +24,7 @@ public class Log4J2LoggerFactoryTest {
@Test @Test
public void testCreation() { public void testCreation() {
InternalLogger logger = new Log4J2LoggerFactory().newInstance("foo"); InternalLogger logger = Log4J2LoggerFactory.INSTANCE.newInstance("foo");
assertTrue(logger instanceof Log4J2Logger); assertTrue(logger instanceof Log4J2Logger);
assertEquals("foo", logger.name()); assertEquals("foo", logger.name());
} }

View File

@ -23,7 +23,7 @@ public class Log4JLoggerFactoryTest {
@Test @Test
public void testCreation() { public void testCreation() {
InternalLogger logger = new Log4JLoggerFactory().newInstance("foo"); InternalLogger logger = Log4JLoggerFactory.INSTANCE.newInstance("foo");
assertTrue(logger instanceof Log4JLogger); assertTrue(logger instanceof Log4JLogger);
assertEquals("foo", logger.name()); assertEquals("foo", logger.name());
} }

View File

@ -23,7 +23,7 @@ public class Slf4JLoggerFactoryTest {
@Test @Test
public void testCreation() { public void testCreation() {
InternalLogger logger = new Slf4JLoggerFactory().newInstance("foo"); InternalLogger logger = Slf4JLoggerFactory.INSTANCE.newInstance("foo");
assertTrue(logger instanceof Slf4JLogger); assertTrue(logger instanceof Slf4JLogger);
assertEquals("foo", logger.name()); assertEquals("foo", logger.name());
} }