Skip to content

Commit

Permalink
Cleanup Log4J2Logger (netty#8245)
Browse files Browse the repository at this point in the history
Motivation:

Log4J2Logger had some code-duplication with AbstractInternalLogger

Modifications:

Reuse AbstractInternaLogger.EXCEPTION_MESSAGE in Log4J2Logger and so remove some code-duplication

Result:

Less duplicated code.
  • Loading branch information
normanmaurer authored Aug 31, 2018
1 parent e26666a commit 9d8846c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public abstract class AbstractInternalLogger implements InternalLogger, Serializ

private static final long serialVersionUID = -6382972526573193470L;

private static final String EXCEPTION_MESSAGE = "Unexpected exception:";
static final String EXCEPTION_MESSAGE = "Unexpected exception:";

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
import org.apache.logging.log4j.spi.ExtendedLogger;
import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;

import static io.netty.util.internal.logging.AbstractInternalLogger.EXCEPTION_MESSAGE;

class Log4J2Logger extends ExtendedLoggerWrapper implements InternalLogger {

private static final long serialVersionUID = 5485418394879791397L;

/** {@linkplain AbstractInternalLogger#EXCEPTION_MESSAGE} */
private static final String EXCEPTION_MESSAGE = "Unexpected exception:";

Log4J2Logger(Logger logger) {
super((ExtendedLogger) logger, logger.getName(), logger.getMessageFactory());
}
Expand Down Expand Up @@ -97,7 +96,7 @@ public void log(InternalLogLevel level, Throwable t) {
log(toLevel(level), EXCEPTION_MESSAGE, t);
}

protected Level toLevel(InternalLogLevel level) {
private static Level toLevel(InternalLogLevel level) {
switch (level) {
case INFO:
return Level.INFO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static org.junit.Assert.assertEquals;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

Expand All @@ -29,7 +28,6 @@
import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;
import org.hamcrest.CoreMatchers;
import org.junit.Assume;
import org.junit.Test;

import io.netty.util.internal.ReflectionUtil;

Expand All @@ -56,25 +54,6 @@ public void logMessage(String fqcn, Level level, Marker marker, Message message,
};
}

@Test
public void testEXCEPTION_MESSAGE() {
assertEquals(getFieldValue(AbstractInternalLogger.class, "EXCEPTION_MESSAGE"),
getFieldValue(Log4J2Logger.class, "EXCEPTION_MESSAGE"));
}

@SuppressWarnings("unchecked")
private static <T> T getFieldValue(Class<?> clazz, String fieldName) {
try {
Field field = clazz.getDeclaredField(fieldName);
if (!field.isAccessible()) {
Assume.assumeThat(ReflectionUtil.trySetAccessible(field, true), CoreMatchers.nullValue());
}
return (T) field.get(AbstractInternalLogger.class);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(e);
}
}

@Override
protected void setLevelEnable(InternalLogLevel level, boolean enable) throws Exception {
Level targetLevel = Level.valueOf(level.name());
Expand Down

0 comments on commit 9d8846c

Please sign in to comment.