Skip to content
Open
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 @@ -25,6 +25,9 @@
* @param since
* - UNIX timestamp (integer) to filter logs. Specifying a timestamp will only output log-entries since that timestamp. Default:
* 0 (unfiltered)
* @param until
* - UNIX timestamp (integer) to filter logs. Specifying a timestamp will only output log-entries before that timestamp. Default:
* 0 (unfiltered)
*/
public interface LogContainerCmd extends AsyncDockerCmd<LogContainerCmd, Frame> {

Expand All @@ -49,6 +52,9 @@ public interface LogContainerCmd extends AsyncDockerCmd<LogContainerCmd, Frame>
@CheckForNull
Integer getSince();

@CheckForNull
Integer getUntil();

LogContainerCmd withContainerId(@Nonnull String containerId);

/**
Expand All @@ -69,6 +75,8 @@ public interface LogContainerCmd extends AsyncDockerCmd<LogContainerCmd, Frame>

LogContainerCmd withSince(Integer since);

LogContainerCmd withUntil(Integer until);

/**
* @throws com.github.dockerjava.api.NotFoundException
* No such container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
* @param since
* - UNIX timestamp (integer) to filter logs. Specifying a timestamp will only output log-entries since that timestamp. Default:
* 0 (unfiltered)
* @param until
* - UNIX timestamp (integer) to filter logs. Specifying a timestamp will only output log-entries before that timestamp. Default:
* 0 (unfiltered)
*/
public class LogContainerCmdImpl extends AbstrAsyncDockerCmd<LogContainerCmd, Frame> implements LogContainerCmd {

private String containerId;

private Boolean followStream, timestamps, stdout, stderr;

private Integer tail, since;
private Integer tail, since, until;

public LogContainerCmdImpl(LogContainerCmd.Exec exec, String containerId) {
super(exec);
Expand Down Expand Up @@ -73,6 +76,11 @@ public Integer getSince() {
return since;
}

@Override
public Integer getUntil() {
return until;
}

@Override
public LogContainerCmd withContainerId(String containerId) {
checkNotNull(containerId, "containerId was not specified");
Expand Down Expand Up @@ -122,6 +130,12 @@ public LogContainerCmd withSince(Integer since) {
return this;
}

@Override
public LogContainerCmd withUntil(Integer until) {
this.until = until;
return this;
}

@Override
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ protected Void execute0(LogContainerCmd command, ResultCallback<Frame> resultCal
webTarget = webTarget.queryParam("since", command.getSince());
}

if (command.getUntil() != null) {
webTarget = webTarget.queryParam("until", command.getUntil());
}

webTarget = booleanQueryParam(webTarget, "timestamps", command.hasTimestampsEnabled());
webTarget = booleanQueryParam(webTarget, "stdout", command.hasStdoutEnabled());
webTarget = booleanQueryParam(webTarget, "stderr", command.hasStderrEnabled());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.github.dockerjava.cmd;

import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.StreamType;
import com.github.dockerjava.utils.LogContainerTestCallback;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -197,4 +201,52 @@ public void asyncLogContainerWithSince() throws Exception {

assertThat(loggingCallback.toString(), containsString(snippet));
}

@Test
public void asyncLogContainerWithUntil() throws Exception {

CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox")
.withCmd("/bin/sh", "-c", "while true; do echo hello; sleep 1; done")
.exec();

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

dockerRule.getClient().startContainerCmd(container.getId()).exec();
Thread.sleep(5000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Awaitility instead


int timestamp = (int) (System.currentTimeMillis() / 1000);

Thread.sleep(2000);
dockerRule.getClient().stopContainerCmd(container.getId()).exec();

LogContainerTestCallback loggingCallback = new LogContainerTestCallback();

dockerRule.getClient().logContainerCmd(container.getId())
.withStdErr(true)
.withStdOut(true)
.withUntil(timestamp)
.exec(loggingCallback);

loggingCallback.awaitCompletion();

String logs_until = loggingCallback.toString();
assertThat(logs_until, containsString("hello"));
assertEquals(5, StringUtils.countMatches(logs_until,"hello"));

loggingCallback = new LogContainerTestCallback();

dockerRule.getClient().logContainerCmd(container.getId())
.withStdErr(true)
.withStdOut(true)
.exec(loggingCallback);

loggingCallback.awaitCompletion();

String logs = loggingCallback.toString();
assertThat(logs, containsString("hello"));

assertTrue(logs.length() > logs_until.length());
}

}