spotify/docker-client — pull request #110 by JWGmeligMeyling (2014-12-19 17:24:00+00:00)
Title: Attach stdout and stderr as OutputStreams to LogStream

Looking for a way to get the `stderr` and `stdout` streams from a running (attach) or stopped (logs) container as `inputstreams`, I eventually came up with the following solution. I added a method `attach(outputstream, outputstream)` to `LogStream`, which then iterates over all incoming `LogMessages` and sends them to the correct stream. I'd like to hear your thoughts on this approach.

This can be used in the following ways:

### Binding to System.err and System.out
```java
dockerClient
.attachContainer(containerId,
AttachParameter.LOGS, AttachParameter.STDOUT,
AttachParameter.STDERR, AttachParameter.STREAM)
.attach(System.out, System.err);
```

### Binding to piped inputstreams
```java
final PipedInputStream stdout = new PipedInputStream();
final PipedInputStream stderr = new PipedInputStream();
final PipedOutputStream stdout_pipe = new PipedOutputStream(stdout);
final PipedOutputStream stderr_pipe = new PipedOutputStream(stderr);

executor.submit(new Callable() {

@Override
public Void call() throws Exception {
dockerClient
.attachContainer(containerId,
AttachParameter.LOGS, AttachParameter.STDOUT,
AttachParameter.STDERR, AttachParameter.STREAM)
.attach(stdout_pipe, stderr_pipe);
return null;
}

});

try (Scanner sc_stdout = new Scanner(stdout);
Scanner sc_stderr = new Scanner(stderr)) {
assertThat(sc_stdout.next(), equalTo("stdout"));
assertThat(sc_stderr.next(), equalTo("stderr"));
}
```