Remko Popma
2014-09-10 07:15:43 UTC
Nice!
Shall we move the remaining level constants to DefaultLevelConverter?
Also, do we need to define custom log4j levels FINE and FINEST? I thought it might be good to remove them since these JUL levels already map to built-in log4j levels DEBUG and TRACE.
Sent from my iPhone
Shall we move the remaining level constants to DefaultLevelConverter?
Also, do we need to define custom log4j levels FINE and FINEST? I thought it might be good to remove them since these JUL levels already map to built-in log4j levels DEBUG and TRACE.
Sent from my iPhone
Update LevelTranslator to use LevelConverter interface.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/07e6faa3
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/07e6faa3
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/07e6faa3
Branch: refs/heads/master
Commit: 07e6faa32656571f18358e834e36b0a76d9917e6
Parents: dc8be98
Authored: Wed Sep 10 01:39:57 2014 -0500
Committed: Wed Sep 10 01:39:57 2014 -0500
----------------------------------------------------------------------
.../logging/log4j/jul/LevelTranslator.java | 98 ++++++--------------
.../logging/log4j/jul/LevelTranslatorTest.java | 31 ++++---
2 files changed, 45 insertions(+), 84 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07e6faa3/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java
----------------------------------------------------------------------
diff --git a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java
index bc83457..8df429a 100644
--- a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java
+++ b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java
@@ -17,10 +17,11 @@
package org.apache.logging.log4j.jul;
-import java.util.IdentityHashMap;
-import java.util.Map;
-
import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.LoaderUtil;
+import org.apache.logging.log4j.util.PropertiesUtil;
/**
* Utility class to convert between JDK Levels and Log4j 2 Levels.
@@ -29,37 +30,31 @@ import org.apache.logging.log4j.Level;
*/
public final class LevelTranslator {
- private static final int JDK_SEVERE = java.util.logging.Level.SEVERE.intValue(); // ERROR
- private static final int JDK_WARNING = java.util.logging.Level.WARNING.intValue(); // WARN
- private static final int JDK_INFO = java.util.logging.Level.INFO.intValue(); // INFO
- private static final int JDK_CONFIG = java.util.logging.Level.CONFIG.intValue(); // INFO
- private static final int JDK_FINE = java.util.logging.Level.FINE.intValue(); // DEBUG
- private static final int JDK_FINER = java.util.logging.Level.FINER.intValue(); // DEBUG
- private static final int JDK_FINEST = java.util.logging.Level.FINEST.intValue(); // TRACE
+ public static final String LEVEL_CONVERTER_PROPERTY = "log4j.jul.levelConverter";
+
+ public static final Level FINEST = Level.forName("FINEST", Level.TRACE.intLevel() + 100);
+ public static final Level FINER = Level.forName("FINER", Level.TRACE.intLevel());
+ public static final Level FINE = Level.forName("FINE", Level.DEBUG.intLevel());
+ public static final Level CONFIG = Level.forName("CONFIG", Level.INFO.intLevel() + 50);
- // standard level mappings
- private static final Map<java.util.logging.Level, Level> JDK_TO_LOG4J =
- new IdentityHashMap<java.util.logging.Level, Level>(10);
- private static final Map<Level, java.util.logging.Level> LOG4J_TO_JDK =
- new IdentityHashMap<Level, java.util.logging.Level>(10);
+ private static final Logger LOGGER = StatusLogger.getLogger();
+ private static final LevelConverter LEVEL_CONVERTER;
static {
- JDK_TO_LOG4J.put(java.util.logging.Level.OFF, Level.OFF);
- JDK_TO_LOG4J.put(java.util.logging.Level.FINEST, Level.TRACE);
- JDK_TO_LOG4J.put(java.util.logging.Level.FINER, Level.DEBUG);
- JDK_TO_LOG4J.put(java.util.logging.Level.FINE, Level.DEBUG);
- JDK_TO_LOG4J.put(java.util.logging.Level.CONFIG, Level.INFO);
- JDK_TO_LOG4J.put(java.util.logging.Level.INFO, Level.INFO);
- JDK_TO_LOG4J.put(java.util.logging.Level.WARNING, Level.WARN);
- JDK_TO_LOG4J.put(java.util.logging.Level.SEVERE, Level.ERROR);
- JDK_TO_LOG4J.put(java.util.logging.Level.ALL, Level.ALL);
- LOG4J_TO_JDK.put(Level.OFF, java.util.logging.Level.OFF);
- LOG4J_TO_JDK.put(Level.TRACE, java.util.logging.Level.FINEST);
- LOG4J_TO_JDK.put(Level.DEBUG, java.util.logging.Level.FINE);
- LOG4J_TO_JDK.put(Level.INFO, java.util.logging.Level.INFO);
- LOG4J_TO_JDK.put(Level.WARN, java.util.logging.Level.WARNING);
- LOG4J_TO_JDK.put(Level.ERROR, java.util.logging.Level.SEVERE);
- LOG4J_TO_JDK.put(Level.ALL, java.util.logging.Level.ALL);
+ final String levelConverterClassName =
+ PropertiesUtil.getProperties().getStringProperty(LEVEL_CONVERTER_PROPERTY);
+ if (levelConverterClassName != null) {
+ LevelConverter levelConverter;
+ try {
+ levelConverter = LoaderUtil.newCheckedInstanceOf(levelConverterClassName, LevelConverter.class);
+ } catch (final Exception e) {
+ LOGGER.error("Could not create custom LevelConverter [{}].", levelConverterClassName, e);
+ levelConverter = new DefaultLevelConverter();
+ }
+ LEVEL_CONVERTER = levelConverter;
+ } else {
+ LEVEL_CONVERTER = new DefaultLevelConverter();
+ }
}
/**
@@ -69,40 +64,7 @@ public final class LevelTranslator {
*/
public static Level toLevel(final java.util.logging.Level level) {
- final Level standardLevel = JDK_TO_LOG4J.get(level);
- if (standardLevel != null) {
- return standardLevel;
- }
- final int value = level.intValue();
- if (value == Integer.MAX_VALUE) {
- return Level.OFF;
- }
- if (value == Integer.MIN_VALUE) {
- return Level.ALL;
- }
- if (value <= JDK_FINEST) { // up to 300
- return Level.TRACE;
- }
- if (value <= JDK_FINER) { // 301 to 400
- return Level.DEBUG;
- }
- if (value <= JDK_FINE) { // 401 to 500
- return Level.DEBUG;
- }
- if (value <= JDK_CONFIG) { // 501 to 700
- return Level.INFO;
- }
- if (value <= JDK_INFO) { // 701 to 800
- return Level.INFO;
- }
- if (value <= JDK_WARNING) { // 801 to 900
- return Level.WARN;
- }
- if (value <= JDK_SEVERE) { // 901 to 1000
- return Level.ERROR;
- }
- // 1001+
- return Level.FATAL;
+ return LEVEL_CONVERTER.toLevel(level);
}
/**
@@ -112,11 +74,7 @@ public final class LevelTranslator {
*/
public static java.util.logging.Level toJavaLevel(final Level level) {
- final java.util.logging.Level standardLevel = LOG4J_TO_JDK.get(level);
- if (standardLevel != null) {
- return standardLevel;
- }
- return java.util.logging.Level.parse(level.name());
+ return LEVEL_CONVERTER.toJavaLevel(level);
}
private LevelTranslator() {
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07e6faa3/log4j-jul/src/test/java/org/apache/logging/log4j/jul/LevelTranslatorTest.java
----------------------------------------------------------------------
diff --git a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/LevelTranslatorTest.java b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/LevelTranslatorTest.java
index 744e296..3ab5e92 100644
--- a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/LevelTranslatorTest.java
+++ b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/LevelTranslatorTest.java
@@ -30,37 +30,40 @@ import static org.junit.Assert.*;
@RunWith(Parameterized.class)
public class LevelTranslatorTest {
- private final java.util.logging.Level level;
- private final Level expectedLevel;
+ private final java.util.logging.Level javaLevel;
+ private final Level log4jLevel;
- public LevelTranslatorTest(final java.util.logging.Level level, final Level expectedLevel) {
- this.level = level;
- this.expectedLevel = expectedLevel;
+ public LevelTranslatorTest(final java.util.logging.Level javaLevel, final Level log4jLevel) {
+ this.javaLevel = javaLevel;
+ this.log4jLevel = log4jLevel;
}
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
new Object[][]{
- {CustomJdkLevel.TEST, Level.INFO},
- {CustomJdkLevel.DEFCON_2, Level.ERROR},
- {CustomJdkLevel.DEFCON_1, Level.FATAL},
{java.util.logging.Level.OFF, Level.OFF},
{java.util.logging.Level.ALL, Level.ALL},
{java.util.logging.Level.SEVERE, Level.ERROR},
{java.util.logging.Level.WARNING, Level.WARN},
{java.util.logging.Level.INFO, Level.INFO},
- {java.util.logging.Level.CONFIG, Level.INFO},
- {java.util.logging.Level.FINE, Level.DEBUG},
- {java.util.logging.Level.FINER, Level.DEBUG},
- {java.util.logging.Level.FINEST, Level.TRACE}
+ {java.util.logging.Level.CONFIG, LevelTranslator.CONFIG},
+ {java.util.logging.Level.FINE, LevelTranslator.FINE},
+ {java.util.logging.Level.FINER, LevelTranslator.FINER},
+ {java.util.logging.Level.FINEST, LevelTranslator.FINEST}
}
);
}
@Test
public void testToLevel() throws Exception {
- final Level actualLevel = LevelTranslator.toLevel(level);
- assertEquals(expectedLevel, actualLevel);
+ final Level actualLevel = LevelTranslator.toLevel(javaLevel);
+ assertEquals(log4jLevel, actualLevel);
+ }
+
+ public void testToJavaLevel() throws Exception {
+ final java.util.logging.Level actualLevel = LevelTranslator.toJavaLevel(log4jLevel);
+ assertEquals(javaLevel, actualLevel);
}
}
\ No newline at end of file
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/07e6faa3
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/07e6faa3
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/07e6faa3
Branch: refs/heads/master
Commit: 07e6faa32656571f18358e834e36b0a76d9917e6
Parents: dc8be98
Authored: Wed Sep 10 01:39:57 2014 -0500
Committed: Wed Sep 10 01:39:57 2014 -0500
----------------------------------------------------------------------
.../logging/log4j/jul/LevelTranslator.java | 98 ++++++--------------
.../logging/log4j/jul/LevelTranslatorTest.java | 31 ++++---
2 files changed, 45 insertions(+), 84 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07e6faa3/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java
----------------------------------------------------------------------
diff --git a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java
index bc83457..8df429a 100644
--- a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java
+++ b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java
@@ -17,10 +17,11 @@
package org.apache.logging.log4j.jul;
-import java.util.IdentityHashMap;
-import java.util.Map;
-
import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.LoaderUtil;
+import org.apache.logging.log4j.util.PropertiesUtil;
/**
* Utility class to convert between JDK Levels and Log4j 2 Levels.
@@ -29,37 +30,31 @@ import org.apache.logging.log4j.Level;
*/
public final class LevelTranslator {
- private static final int JDK_SEVERE = java.util.logging.Level.SEVERE.intValue(); // ERROR
- private static final int JDK_WARNING = java.util.logging.Level.WARNING.intValue(); // WARN
- private static final int JDK_INFO = java.util.logging.Level.INFO.intValue(); // INFO
- private static final int JDK_CONFIG = java.util.logging.Level.CONFIG.intValue(); // INFO
- private static final int JDK_FINE = java.util.logging.Level.FINE.intValue(); // DEBUG
- private static final int JDK_FINER = java.util.logging.Level.FINER.intValue(); // DEBUG
- private static final int JDK_FINEST = java.util.logging.Level.FINEST.intValue(); // TRACE
+ public static final String LEVEL_CONVERTER_PROPERTY = "log4j.jul.levelConverter";
+
+ public static final Level FINEST = Level.forName("FINEST", Level.TRACE.intLevel() + 100);
+ public static final Level FINER = Level.forName("FINER", Level.TRACE.intLevel());
+ public static final Level FINE = Level.forName("FINE", Level.DEBUG.intLevel());
+ public static final Level CONFIG = Level.forName("CONFIG", Level.INFO.intLevel() + 50);
- // standard level mappings
- private static final Map<java.util.logging.Level, Level> JDK_TO_LOG4J =
- new IdentityHashMap<java.util.logging.Level, Level>(10);
- private static final Map<Level, java.util.logging.Level> LOG4J_TO_JDK =
- new IdentityHashMap<Level, java.util.logging.Level>(10);
+ private static final Logger LOGGER = StatusLogger.getLogger();
+ private static final LevelConverter LEVEL_CONVERTER;
static {
- JDK_TO_LOG4J.put(java.util.logging.Level.OFF, Level.OFF);
- JDK_TO_LOG4J.put(java.util.logging.Level.FINEST, Level.TRACE);
- JDK_TO_LOG4J.put(java.util.logging.Level.FINER, Level.DEBUG);
- JDK_TO_LOG4J.put(java.util.logging.Level.FINE, Level.DEBUG);
- JDK_TO_LOG4J.put(java.util.logging.Level.CONFIG, Level.INFO);
- JDK_TO_LOG4J.put(java.util.logging.Level.INFO, Level.INFO);
- JDK_TO_LOG4J.put(java.util.logging.Level.WARNING, Level.WARN);
- JDK_TO_LOG4J.put(java.util.logging.Level.SEVERE, Level.ERROR);
- JDK_TO_LOG4J.put(java.util.logging.Level.ALL, Level.ALL);
- LOG4J_TO_JDK.put(Level.OFF, java.util.logging.Level.OFF);
- LOG4J_TO_JDK.put(Level.TRACE, java.util.logging.Level.FINEST);
- LOG4J_TO_JDK.put(Level.DEBUG, java.util.logging.Level.FINE);
- LOG4J_TO_JDK.put(Level.INFO, java.util.logging.Level.INFO);
- LOG4J_TO_JDK.put(Level.WARN, java.util.logging.Level.WARNING);
- LOG4J_TO_JDK.put(Level.ERROR, java.util.logging.Level.SEVERE);
- LOG4J_TO_JDK.put(Level.ALL, java.util.logging.Level.ALL);
+ final String levelConverterClassName =
+ PropertiesUtil.getProperties().getStringProperty(LEVEL_CONVERTER_PROPERTY);
+ if (levelConverterClassName != null) {
+ LevelConverter levelConverter;
+ try {
+ levelConverter = LoaderUtil.newCheckedInstanceOf(levelConverterClassName, LevelConverter.class);
+ } catch (final Exception e) {
+ LOGGER.error("Could not create custom LevelConverter [{}].", levelConverterClassName, e);
+ levelConverter = new DefaultLevelConverter();
+ }
+ LEVEL_CONVERTER = levelConverter;
+ } else {
+ LEVEL_CONVERTER = new DefaultLevelConverter();
+ }
}
/**
@@ -69,40 +64,7 @@ public final class LevelTranslator {
*/
public static Level toLevel(final java.util.logging.Level level) {
- final Level standardLevel = JDK_TO_LOG4J.get(level);
- if (standardLevel != null) {
- return standardLevel;
- }
- final int value = level.intValue();
- if (value == Integer.MAX_VALUE) {
- return Level.OFF;
- }
- if (value == Integer.MIN_VALUE) {
- return Level.ALL;
- }
- if (value <= JDK_FINEST) { // up to 300
- return Level.TRACE;
- }
- if (value <= JDK_FINER) { // 301 to 400
- return Level.DEBUG;
- }
- if (value <= JDK_FINE) { // 401 to 500
- return Level.DEBUG;
- }
- if (value <= JDK_CONFIG) { // 501 to 700
- return Level.INFO;
- }
- if (value <= JDK_INFO) { // 701 to 800
- return Level.INFO;
- }
- if (value <= JDK_WARNING) { // 801 to 900
- return Level.WARN;
- }
- if (value <= JDK_SEVERE) { // 901 to 1000
- return Level.ERROR;
- }
- // 1001+
- return Level.FATAL;
+ return LEVEL_CONVERTER.toLevel(level);
}
/**
@@ -112,11 +74,7 @@ public final class LevelTranslator {
*/
public static java.util.logging.Level toJavaLevel(final Level level) {
- final java.util.logging.Level standardLevel = LOG4J_TO_JDK.get(level);
- if (standardLevel != null) {
- return standardLevel;
- }
- return java.util.logging.Level.parse(level.name());
+ return LEVEL_CONVERTER.toJavaLevel(level);
}
private LevelTranslator() {
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07e6faa3/log4j-jul/src/test/java/org/apache/logging/log4j/jul/LevelTranslatorTest.java
----------------------------------------------------------------------
diff --git a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/LevelTranslatorTest.java b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/LevelTranslatorTest.java
index 744e296..3ab5e92 100644
--- a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/LevelTranslatorTest.java
+++ b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/LevelTranslatorTest.java
@@ -30,37 +30,40 @@ import static org.junit.Assert.*;
@RunWith(Parameterized.class)
public class LevelTranslatorTest {
- private final java.util.logging.Level level;
- private final Level expectedLevel;
+ private final java.util.logging.Level javaLevel;
+ private final Level log4jLevel;
- public LevelTranslatorTest(final java.util.logging.Level level, final Level expectedLevel) {
- this.level = level;
- this.expectedLevel = expectedLevel;
+ public LevelTranslatorTest(final java.util.logging.Level javaLevel, final Level log4jLevel) {
+ this.javaLevel = javaLevel;
+ this.log4jLevel = log4jLevel;
}
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
new Object[][]{
- {CustomJdkLevel.TEST, Level.INFO},
- {CustomJdkLevel.DEFCON_2, Level.ERROR},
- {CustomJdkLevel.DEFCON_1, Level.FATAL},
{java.util.logging.Level.OFF, Level.OFF},
{java.util.logging.Level.ALL, Level.ALL},
{java.util.logging.Level.SEVERE, Level.ERROR},
{java.util.logging.Level.WARNING, Level.WARN},
{java.util.logging.Level.INFO, Level.INFO},
- {java.util.logging.Level.CONFIG, Level.INFO},
- {java.util.logging.Level.FINE, Level.DEBUG},
- {java.util.logging.Level.FINER, Level.DEBUG},
- {java.util.logging.Level.FINEST, Level.TRACE}
+ {java.util.logging.Level.CONFIG, LevelTranslator.CONFIG},
+ {java.util.logging.Level.FINE, LevelTranslator.FINE},
+ {java.util.logging.Level.FINER, LevelTranslator.FINER},
+ {java.util.logging.Level.FINEST, LevelTranslator.FINEST}
}
);
}
@Test
public void testToLevel() throws Exception {
- final Level actualLevel = LevelTranslator.toLevel(level);
- assertEquals(expectedLevel, actualLevel);
+ final Level actualLevel = LevelTranslator.toLevel(javaLevel);
+ assertEquals(log4jLevel, actualLevel);
+ }
+
+ public void testToJavaLevel() throws Exception {
+ final java.util.logging.Level actualLevel = LevelTranslator.toJavaLevel(log4jLevel);
+ assertEquals(javaLevel, actualLevel);
}
}
\ No newline at end of file