Skip to content

Commit

Permalink
Make all InternalLoggerFactory implementations be singletons
Browse files Browse the repository at this point in the history
Motivation:

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

Modifications:

Make all InternalLoggerFactory implementations be singletons and hide the construtors.

Result:

All InternalLoggerFactory implementations be singletons.
  • Loading branch information
windie authored and normanmaurer committed Apr 8, 2016
1 parent 0bc93dd commit bc6adab
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@
*/
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
public InternalLogger newInstance(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* {@link JdkLoggerFactory} is used. You can change it to your preferred
* logging framework before other Netty classes are loaded:
* <pre>
* {@link InternalLoggerFactory}.setDefaultFactory(new {@link Log4JLoggerFactory}());
* {@link InternalLoggerFactory}.setDefaultFactory({@link Log4JLoggerFactory}.INSTANCE);
* </pre>
* Please note that the new default factory is effective only for the classes
* which were loaded after the default factory is changed. Therefore,
Expand Down Expand Up @@ -55,10 +55,10 @@ private static InternalLoggerFactory newDefaultFactory(String name) {
f.newInstance(name).debug("Using SLF4J as the default logging framework");
} catch (Throwable t1) {
try {
f = new Log4JLoggerFactory();
f = Log4JLoggerFactory.INSTANCE;
f.newInstance(name).debug("Using Log4J as the default logging framework");
} catch (Throwable t2) {
f = new JdkLoggerFactory();
f = JdkLoggerFactory.INSTANCE;
f.newInstance(name).debug("Using java.util.logging as the default logging framework");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
*/
public class JdkLoggerFactory extends InternalLoggerFactory {

public static final InternalLoggerFactory INSTANCE = new JdkLoggerFactory();

/**
* @deprecated Use {@link #INSTANCE} instead.
*/
@Deprecated
public JdkLoggerFactory() {
}

@Override
public InternalLogger newInstance(String name) {
return new JdkLogger(Logger.getLogger(name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@

public final class Log4J2LoggerFactory extends InternalLoggerFactory {

public static final InternalLoggerFactory INSTANCE = new Log4J2LoggerFactory();

/**
* @deprecated Use {@link #INSTANCE} instead.
*/
@Deprecated
public Log4J2LoggerFactory() {
}

@Override
public InternalLogger newInstance(String name) {
return new Log4J2Logger(LogManager.getLogger(name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
*/
public class Log4JLoggerFactory extends InternalLoggerFactory {

public static final InternalLoggerFactory INSTANCE = new Log4JLoggerFactory();

/**
* @deprecated Use {@link #INSTANCE} instead.
*/
@Deprecated
public Log4JLoggerFactory() {
}

@Override
public InternalLogger newInstance(String name) {
return new Log4JLogger(Logger.getLogger(name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
*/
public class Slf4JLoggerFactory extends InternalLoggerFactory {

public static final InternalLoggerFactory INSTANCE = new Slf4JLoggerFactory();

/**
* @deprecated Use {@link #INSTANCE} instead.
*/
@Deprecated
public Slf4JLoggerFactory() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class CommonsLoggerFactoryTest {

@Test
public void testCreation() {
InternalLogger logger = new CommonsLoggerFactory().newInstance("foo");
InternalLogger logger = CommonsLoggerFactory.INSTANCE.newInstance("foo");
assertTrue(logger instanceof CommonsLogger);
assertEquals("foo", logger.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class JdkLoggerFactoryTest {

@Test
public void testCreation() {
InternalLogger logger = new JdkLoggerFactory().newInstance("foo");
InternalLogger logger = JdkLoggerFactory.INSTANCE.newInstance("foo");
assertTrue(logger instanceof JdkLogger);
assertEquals("foo", logger.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Log4J2LoggerFactoryTest {

@Test
public void testCreation() {
InternalLogger logger = new Log4J2LoggerFactory().newInstance("foo");
InternalLogger logger = Log4J2LoggerFactory.INSTANCE.newInstance("foo");
assertTrue(logger instanceof Log4J2Logger);
assertEquals("foo", logger.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Log4JLoggerFactoryTest {

@Test
public void testCreation() {
InternalLogger logger = new Log4JLoggerFactory().newInstance("foo");
InternalLogger logger = Log4JLoggerFactory.INSTANCE.newInstance("foo");
assertTrue(logger instanceof Log4JLogger);
assertEquals("foo", logger.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Slf4JLoggerFactoryTest {

@Test
public void testCreation() {
InternalLogger logger = new Slf4JLoggerFactory().newInstance("foo");
InternalLogger logger = Slf4JLoggerFactory.INSTANCE.newInstance("foo");
assertTrue(logger instanceof Slf4JLogger);
assertEquals("foo", logger.name());
}
Expand Down

0 comments on commit bc6adab

Please sign in to comment.