Skip to content
Permalink
Browse files

[JENKINS-30777] also allow slaves to decorate logger

  • Loading branch information...
kohsuke committed Oct 2, 2015
1 parent 36eea3d commit 4c4367113fff8fd9388ac3659504177aedd32f33
@@ -26,18 +26,14 @@

import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.FilePath;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.Build;
import hudson.model.Job;
import hudson.model.Node;
import hudson.model.Computer;
import hudson.model.Run;
import hudson.scm.SCM;
import hudson.tasks.BuildWrapper;
import hudson.util.ArgumentListBuilder;
import jenkins.model.Jenkins;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.OutputStream;

@@ -56,7 +52,7 @@
* Called on the start of each build, giving extensions a chance to intercept
* the data that is written to the log.
*
* @deprecated as of 1.630. Use {@link #decorateLogger(Run, OutputStream)}
* @deprecated as of 1.632. Use {@link #decorateLogger(Run, OutputStream)}
*/
public OutputStream decorateLogger(AbstractBuild build, OutputStream logger) throws IOException, InterruptedException {
if (Util.isOverridden(ConsoleLogFilter.class, getClass(), "decorateLogger", Run.class, OutputStream.class)) {
@@ -91,6 +87,18 @@ public OutputStream decorateLogger(Run build, OutputStream logger) throws IOExce
}
}

/**
* Called to decorate logger for master/slave communication.
*
* @param computer
* Slave computer for which the logger is getting decorated. Useful to do
* contextual decoration.
* @since 1.632
*/
public OutputStream decorateLogger(@Nonnull Computer computer, OutputStream logger) throws IOException, InterruptedException {
return logger; // by default no-op
}

/**
* All the registered {@link ConsoleLogFilter}s.
*/
@@ -28,6 +28,7 @@
import hudson.AbortException;
import hudson.FilePath;
import hudson.Util;
import hudson.console.ConsoleLogFilter;
import hudson.model.Computer;
import hudson.model.Executor;
import hudson.model.ExecutorListener;
@@ -137,10 +138,24 @@
public SlaveComputer(Slave slave) {
super(slave);
this.log = new ReopenableRotatingFileOutputStream(getLogFile(),10);
this.taskListener = new StreamTaskListener(log);
this.taskListener = new StreamTaskListener(decorate(this.log));
assert slave.getNumExecutors()!=0 : "Computer created with 0 executors";
}

/**
* Uses {@link ConsoleLogFilter} to decorate logger.
*/
private OutputStream decorate(OutputStream os) {
for (ConsoleLogFilter f : ConsoleLogFilter.all()) {
try {
os = f.decorateLogger(this,os);
} catch (IOException|InterruptedException e) {
LOGGER.log(Level.WARNING, "Failed to filter log with "+f, e);
}
}
return os;
}

/**
* {@inheritDoc}
*/
@@ -843,4 +858,6 @@ public static VirtualChannel getChannelToMaster() {
return new ArrayList<LogRecord>(SLAVE_LOG_HANDLER.getView());
}
}

private static final Logger LOGGER = Logger.getLogger(SlaveComputer.class.getName());
}
@@ -0,0 +1,51 @@
package hudson.console;

import hudson.model.Computer;
import hudson.model.Run;
import hudson.slaves.SlaveComputer;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;

import java.io.IOException;
import java.io.OutputStream;

/**
* @author Kohsuke Kawaguchi
*/
public class ConsoleLogFilterTest extends Assert {
@Rule
public JenkinsRule r = new JenkinsRule();

/**
* Checks
*/
@Issue("JENKINS-30777")
@Test public void decorateSlaveLog() throws Exception {
SlaveComputer c = r.createSlave().getComputer();
c.connect(false).get();
assertTrue(c.getLog().contains("[["+c.getName()+"]] "));
}

@TestExtension
public static class Impl extends ConsoleLogFilter {
@Override
public OutputStream decorateLogger(Run build, OutputStream logger) throws IOException, InterruptedException {
return logger;
}

@Override
public OutputStream decorateLogger(final Computer c, final OutputStream out) throws IOException, InterruptedException {
return new LineTransformationOutputStream() {
@Override
protected void eol(byte[] b, int len) throws IOException {
out.write(("[["+c.getName()+"]] ").getBytes());
out.write(b, 0, len);
}
};
}
}
}

0 comments on commit 4c43671

Please sign in to comment.
You can’t perform that action at this time.