Skip to content

Commit

Permalink
fix incorrect interpretation of Level.OFF and Level.ALL in SLF4JPlatf…
Browse files Browse the repository at this point in the history
…ormLogger

Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Sep 21, 2024
1 parent 4fa92fc commit f7b34c2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,39 @@ 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) {
performLog(slf4jLevel, bundle, msg, thrown, params);
}
}

/**
* <p>Level.OFF and Level.ALL levels are not supposed to be used when calling log printing methods.
* </p>
*
* <p>We compensate for such incorrect usage by transforming Level.OFF as Level.ERROR and
* Level.ALL as Level.TRACE.
* </p>
*
* @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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> results = SPS.stringList;
assertEquals(1, results.size());
assertEquals("ERROR extremeLevels - hello", results.get(0));

}

}

0 comments on commit f7b34c2

Please sign in to comment.