diff --git a/server/src/main/java/org/elasticsearch/common/logging/JULBridge.java b/server/src/main/java/org/elasticsearch/common/logging/JULBridge.java index a6b63b91cf888..bf1d70d77684d 100644 --- a/server/src/main/java/org/elasticsearch/common/logging/JULBridge.java +++ b/server/src/main/java/org/elasticsearch/common/logging/JULBridge.java @@ -13,6 +13,8 @@ import org.elasticsearch.logging.LogManager; import org.elasticsearch.logging.Logger; +import java.text.MessageFormat; +import java.util.Locale; import java.util.Map; import java.util.NavigableMap; import java.util.logging.Handler; @@ -22,7 +24,6 @@ * A Java Util Logging handler that writes log messages to the Elasticsearch logging framework. */ class JULBridge extends Handler { - private static final Map levelMap = Map.of( java.util.logging.Level.OFF, Level.OFF, @@ -53,17 +54,22 @@ public static void install() { rootJulLogger.addHandler(new JULBridge()); } - private JULBridge() {} + private JULBridge() { + } @Override public void publish(LogRecord record) { Logger logger = LogManager.getLogger(record.getLoggerName()); Level level = translateJulLevel(record.getLevel()); Throwable thrown = record.getThrown(); - if (thrown == null) { - logger.log(level, record.getMessage()); + if (record.getMessage() == null) { + logger.log(level, () -> "", thrown); } else { - logger.log(level, record::getMessage, thrown); + // exception handling as in https://github.com/qos-ch/slf4j/blob/master/jul-to-slf4j/src/main/java/org/slf4j/bridge/SLF4JBridgeHandler.java#L280 + // ? + logger.log(level, + () -> new MessageFormat(record.getMessage(), Locale.ROOT).format(record.getParameters()), + thrown); } } @@ -79,8 +85,10 @@ private Level translateJulLevel(java.util.logging.Level julLevel) { } @Override - public void flush() {} + public void flush() { + } @Override - public void close() {} + public void close() { + } } diff --git a/server/src/test/java/org/elasticsearch/common/logging/JULBridgeTests.java b/server/src/test/java/org/elasticsearch/common/logging/JULBridgeTests.java index 84d6545aa385c..81305f986b91b 100644 --- a/server/src/test/java/org/elasticsearch/common/logging/JULBridgeTests.java +++ b/server/src/test/java/org/elasticsearch/common/logging/JULBridgeTests.java @@ -16,11 +16,13 @@ import org.elasticsearch.test.MockLogAppender; import org.elasticsearch.test.MockLogAppender.LoggingExpectation; import org.elasticsearch.test.MockLogAppender.SeenEventExpectation; +import org.elasticsearch.test.MockLogAppender.UnseenEventExpectation; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import java.util.logging.ConsoleHandler; +import java.util.logging.LogRecord; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; @@ -111,6 +113,23 @@ public void testCustomLevels() { assertMessage("above finest", julLevel(java.util.logging.Level.FINEST.intValue() + 1), Level.TRACE); } + public void testEmptyMessage(){ + JULBridge.install(); + + assertLogged(() -> { + LogRecord record = new LogRecord(java.util.logging.Level.INFO, null); + record.setLoggerName(logger.getName()); + logger.log(record);//the Logger.log method does not set logger name + }, + new SeenEventExpectation("msg", "", Level.INFO, "")); + } + public void testWithParameters() { + JULBridge.install(); + + assertLogged(() -> logger.log(java.util.logging.Level.INFO, "{0},{1},{2},{3},{4},{5}", new Object[]{"a", "b", "c", 123, 'x'}), + new SeenEventExpectation("a,b,c,123,x", "", Level.INFO, "a,b,c,123,x")); + } + public void testThrowable() { JULBridge.install(); java.util.logging.Logger logger = java.util.logging.Logger.getLogger("");