From 6159f5cd3cc88315eff4761743cdc57ea43309ed Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 28 Oct 2013 11:37:46 -0500 Subject: [PATCH] Support Logger#log(Level,String,Object) Signed-off-by: Johannes Schindelin --- classpath/java/util/logging/Logger.java | 24 ++++++++++++++++++++++++ test/Logging.java | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/classpath/java/util/logging/Logger.java b/classpath/java/util/logging/Logger.java index ae60507a4e..1236821061 100644 --- a/classpath/java/util/logging/Logger.java +++ b/classpath/java/util/logging/Logger.java @@ -80,6 +80,30 @@ public class Logger { log(level, Method.getCaller(), message, exception); } + public void log(Level level, String message, Object param) { + log(level, Method.getCaller(), replaceParameters(message, param), null); + } + + private static String replaceParameters(String message, Object... params) { + StringBuilder builder = new StringBuilder(); + int offset = 0; + for (int i = 0; i < params.length; ++i) { + int curly = message.indexOf("{}", offset); + if (curly < 0) { + break; + } + if (curly > offset) { + builder.append(message, offset, curly); + } + offset = curly + 2; + builder.append(params[i]); + } + if (message.length() > offset) { + builder.append(message, offset, message.length()); + } + return builder.toString(); + } + public void logp(Level level, String sourceClass, String sourceMethod, String msg) { if (!isLoggable(level)) { return; diff --git a/test/Logging.java b/test/Logging.java index ec88986f89..c7e5b4d6ae 100644 --- a/test/Logging.java +++ b/test/Logging.java @@ -66,7 +66,7 @@ public class Logging { public void run() { log.info("Started run"); a(); - log.info("Ended run"); + log.log(Level.INFO, "Ended {}!", "run"); } private void a() {