diff --git a/slf4j-jdk-platform-logging/src/main/java/org/slf4j/jdk/platform/logging/SLF4JPlatformLogger.java b/slf4j-jdk-platform-logging/src/main/java/org/slf4j/jdk/platform/logging/SLF4JPlatformLogger.java index 194436d7e..5b29688fa 100644 --- a/slf4j-jdk-platform-logging/src/main/java/org/slf4j/jdk/platform/logging/SLF4JPlatformLogger.java +++ b/slf4j-jdk-platform-logging/src/main/java/org/slf4j/jdk/platform/logging/SLF4JPlatformLogger.java @@ -115,16 +115,11 @@ public void log(Level jplLevel, ResourceBundle bundle, String format, Object... * @param thrown * @param params */ - private void log(Level jplLevel, ResourceBundle bundle, String msg, Throwable thrown, Object... params) { - if (jplLevel == Level.OFF) - return; + private void log(final Level jplLevel, final ResourceBundle bundle, final String msg, final Throwable thrown, final Object... params) { - if (jplLevel == Level.ALL) { - performLog(org.slf4j.event.Level.TRACE, bundle, msg, thrown, params); - return; - } + final Level jplLevelReduced = fixExtremeLevels(jplLevel); - org.slf4j.event.Level slf4jLevel = jplLevelToSLF4JLevel(jplLevel); + org.slf4j.event.Level slf4jLevel = jplLevelToSLF4JLevel(jplLevelReduced); boolean isEnabled = slf4jLogger.isEnabledForLevel(slf4jLevel); if (isEnabled) { @@ -132,6 +127,27 @@ private void log(Level jplLevel, ResourceBundle bundle, String msg, Throwable th } } + /** + *

Level.OFF and Level.ALL levels are not supposed to be used when calling log printing methods. + *

+ * + *

We compensate for such incorrect usage by transforming Level.OFF as Level.ERROR and + * Level.ALL as Level.TRACE. + *

+ * + * @param jplLevel + * @return + */ + private Level fixExtremeLevels(Level jplLevel) { + if (jplLevel == Level.OFF) + return Level.ERROR; + + if (jplLevel == Level.ALL) + return Level.TRACE; + + return jplLevel; + } + private void performLog(org.slf4j.event.Level slf4jLevel, ResourceBundle bundle, String msg, Throwable thrown, Object... params) { String message = getResourceStringOrMessage(bundle, msg); LoggingEventBuilder leb = slf4jLogger.makeLoggingEventBuilder(slf4jLevel); diff --git a/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/test/SLF4JPlatformLoggingTest.java b/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/test/SLF4JPlatformLoggingTest.java index 8778fd2fa..9f8ce652a 100644 --- a/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/test/SLF4JPlatformLoggingTest.java +++ b/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/test/SLF4JPlatformLoggingTest.java @@ -118,5 +118,18 @@ public void throwTest() throws IOException { assertTrue(results.get(line++).contains(this.getClass().getName())); } + @Test + public void extremeLevels() throws IOException { + LoggerFinder finder = System.LoggerFinder.getLoggerFinder(); + assertEquals(EXPECTED_FINDER_CLASS, finder.getClass().getName()); + Logger systemLogger = finder.getLogger("extremeLevels", null); + systemLogger.log(Level.OFF, "hello"); + systemLogger.log(Level.ALL, "world"); + + List results = SPS.stringList; + assertEquals(1, results.size()); + assertEquals("ERROR extremeLevels - hello", results.get(0)); + + } }