X Tutup
Skip to content
Merged
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 @@ -18,6 +18,8 @@
* - true or false, if true, print timestamps for every log line. Defaults to false.
* @param tail
* - `all` or `<number>`, Output specified number of lines at the end of logs
* @param since
* - UNIX timestamp (integer) to filter logs. Specifying a timestamp will only output log-entries since that timestamp. Default: 0 (unfiltered)
*/
public interface LogContainerCmd extends AsyncDockerCmd<LogContainerCmd, Frame> {

Expand All @@ -33,6 +35,8 @@ public interface LogContainerCmd extends AsyncDockerCmd<LogContainerCmd, Frame>

public boolean hasStderrEnabled();

public int getSince();

public LogContainerCmd withContainerId(String containerId);

/**
Expand Down Expand Up @@ -63,6 +67,8 @@ public interface LogContainerCmd extends AsyncDockerCmd<LogContainerCmd, Frame>

public LogContainerCmd withTail(int tail);

public LogContainerCmd withSince(int since);

public static interface Exec extends DockerCmdAsyncExec<LogContainerCmd, Frame> {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* - true or false, if true, print timestamps for every log line. Defaults to false.
* @param tail
* - `all` or `<number>`, Output specified number of lines at the end of logs
* @param since
* - UNIX timestamp (integer) to filter logs. Specifying a timestamp will only output log-entries since that timestamp. Default: 0 (unfiltered)
*/
public class LogContainerCmdImpl extends AbstrAsyncDockerCmd<LogContainerCmd, Frame> implements LogContainerCmd {

Expand All @@ -27,6 +29,8 @@ public class LogContainerCmdImpl extends AbstrAsyncDockerCmd<LogContainerCmd, Fr

private boolean followStream, timestamps, stdout, stderr;

private int since = 0;

public LogContainerCmdImpl(LogContainerCmd.Exec exec, String containerId) {
super(exec);
withContainerId(containerId);
Expand Down Expand Up @@ -62,6 +66,9 @@ public boolean hasStderrEnabled() {
return stderr;
}

@Override
public int getSince() {return since; }

@Override
public LogContainerCmd withContainerId(String containerId) {
checkNotNull(containerId, "containerId was not specified");
Expand Down Expand Up @@ -125,10 +132,18 @@ public LogContainerCmd withTail(int tail) {
return this;
}

@Override
public LogContainerCmd withSince(int since) {
this.since = since;
return this;
}

@Override
public String toString() {
return new StringBuilder("logs ").append(followStream ? "--follow=true" : "")
.append(timestamps ? "--timestamps=true" : "").append(containerId).toString();
.append(timestamps ? "--timestamps=true" : "")
.append(since > 0 ? "--since=" + since : "")
.append(containerId).toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected AbstractCallbackNotifier<Frame> callbackNotifier(LogContainerCmd comma
.queryParam("stdout", command.hasStdoutEnabled() ? "1" : "0")
.queryParam("stderr", command.hasStderrEnabled() ? "1" : "0")
.queryParam("follow", command.hasFollowStreamEnabled() ? "1" : "0")
.queryParam("since", command.getSince())
.queryParam("tail", command.getTail() < 0 ? "all" : "" + command.getTail());

LOGGER.trace("GET: {}", webTarget);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ public void asyncLogContainer() throws Exception {

LogContainerTestCallback loggingCallback = new LogContainerTestCallback();

//this essentially test the since=0 case
dockerClient.logContainerCmd(container.getId()).withStdErr().withStdOut().exec(loggingCallback);

loggingCallback.awaitCompletion();

assertTrue(loggingCallback.toString().contains(snippet));
}



@Test
public void asyncLogNonExistingContainer() throws Exception {

Expand Down Expand Up @@ -135,4 +138,30 @@ public void asyncMultipleLogContainer() throws Exception {
assertTrue(loggingCallback.toString().contains(snippet));
}

@Test
public void asyncLogContainerWithSince() throws Exception {
String snippet = "hello world";

CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("/bin/echo", snippet)
.exec();

LOG.info("Created container: {}", container.toString());
assertThat(container.getId(), not(isEmptyString()));

dockerClient.startContainerCmd(container.getId()).exec();

int exitCode = dockerClient.waitContainerCmd(container.getId()).exec();

assertThat(exitCode, equalTo(0));

LogContainerTestCallback loggingCallback = new LogContainerTestCallback();

int oneMinuteIntoFuture = (int)(System.currentTimeMillis()/1000) + 60;
dockerClient.logContainerCmd(container.getId()).withStdErr().withStdOut().withSince(oneMinuteIntoFuture).exec(loggingCallback);

loggingCallback.awaitCompletion();

assertFalse(loggingCallback.toString().contains(snippet));
}

}
X Tutup