diff --git a/core/src/main/java/hudson/slaves/SlaveComputer.java b/core/src/main/java/hudson/slaves/SlaveComputer.java index 4c59ec8aae..bf307dade4 100644 --- a/core/src/main/java/hudson/slaves/SlaveComputer.java +++ b/core/src/main/java/hudson/slaves/SlaveComputer.java @@ -92,10 +92,13 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.concurrent.Future; +import java.util.logging.Formatter; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; +import java.util.stream.Stream; import static hudson.slaves.SlaveComputer.LogHolder.SLAVE_LOG_HANDLER; import org.jenkinsci.remoting.util.LoggingChannelListener; @@ -1015,7 +1018,18 @@ public class SlaveComputer extends Computer { } public Void call() { - SLAVE_LOG_HANDLER = new RingBufferLogHandler(ringBufferSize); + SLAVE_LOG_HANDLER = new RingBufferLogHandler(ringBufferSize) { + Formatter dummy = new SimpleFormatter(); + @Override + public synchronized void publish(LogRecord record) { + // see LogRecord.writeObject for dangers of serializing non-String/null parameters + if (record.getMessage() != null && record.getParameters() != null && Stream.of(record.getParameters()).anyMatch(p -> p != null && !(p instanceof String))) { + record.setMessage(dummy.formatMessage(record)); + record.setParameters(null); + } + super.publish(record); + } + }; // avoid double installation of the handler. Inbound agents can reconnect to the master multiple times // and each connection gets a different RemoteClassLoader, so we need to evict them by class name, diff --git a/test/src/test/java/hudson/logging/LogRecorderManagerTest.java b/test/src/test/java/hudson/logging/LogRecorderManagerTest.java index 8d203c2b72..20015adf2e 100644 --- a/test/src/test/java/hudson/logging/LogRecorderManagerTest.java +++ b/test/src/test/java/hudson/logging/LogRecorderManagerTest.java @@ -34,6 +34,7 @@ import java.util.List; import java.util.logging.Logger; import java.util.logging.Level; import java.util.logging.LogRecord; +import java.util.logging.SimpleFormatter; import static org.junit.Assert.*; import org.junit.Rule; import org.junit.Test; @@ -93,8 +94,10 @@ public class LogRecorderManagerTest { assertFalse(ch.call(new Log(Level.FINER, "ns3", "not displayed"))); assertTrue(ch.call(new Log(Level.INFO, "ns4", "msg #4"))); assertFalse(ch.call(new Log(Level.FINE, "ns4", "not displayed"))); + assertTrue(ch.call(new Log(Level.INFO, "other", "msg #5 {0,number,0.0} {1,number,0.0} ''OK?''", new Object[] {1.0, 2.0}))); List recs = c.getLogRecords(); - assertEquals(show(recs), 4, recs.size()); + assertEquals(show(recs), 5, recs.size()); + assertEquals("msg #5 1.0 2.0 'OK?'", new SimpleFormatter().formatMessage(recs.get(0))); recs = r1.getSlaveLogRecords().get(c); assertNotNull(recs); assertEquals(show(recs), 2, recs.size()); @@ -113,14 +116,23 @@ public class LogRecorderManagerTest { private final Level level; private final String logger; private final String message; + private final Object[] params; Log(Level level, String logger, String message) { + this(level, logger, message, null); + } + Log(Level level, String logger, String message, Object[] params) { this.level = level; this.logger = logger; this.message = message; + this.params = params; } @Override public Boolean call() throws Error { Logger log = Logger.getLogger(logger); - log.log(level, message); + if (params != null) { + log.log(level, message, params); + } else { + log.log(level, message); + } return log.isLoggable(level); } }