Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggestions to code review #4

Open
wants to merge 1 commit into
base: logging/jul2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<java.util.logging.Level, Level> levelMap = Map.of(
java.util.logging.Level.OFF,
Level.OFF,
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("");
Expand Down