com.github.dockerjava.api.command.LogContainerCmd Java Examples

The following examples show how to use com.github.dockerjava.api.command.LogContainerCmd. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContainerLogs.java    From OpenLabeler with Apache License 2.0 7 votes vote down vote up
public List<String> getDockerLogs() {

        final List<String> logs = new ArrayList<>();

        LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerId);
        logContainerCmd.withStdOut(true).withStdErr(true);
        logContainerCmd.withSince( lastLogTime );  // UNIX timestamp (integer) to filter logs. Specifying a timestamp will only output log-entries since that timestamp.
        // logContainerCmd.withTail(4);  // get only the last 4 log entries

        logContainerCmd.withTimestamps(true);

        try {
            logContainerCmd.exec(new LogContainerResultCallback() {
                @Override
                public void onNext(Frame item) {
                    logs.add(item.toString());
                }
            }).awaitCompletion();
        } catch (InterruptedException e) {
            LOG.severe("Interrupted Exception!" + e.getMessage());
        }

        lastLogTime = (int) (System.currentTimeMillis() / 1000) + 5;  // assumes at least a 5 second wait between calls to getDockerLogs

        return logs;
    }
 
Example #2
Source File: ChaosContainer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public void tailContainerLog() {
    CompletableFuture.runAsync(() -> {
        while (null == containerId) {
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                return;
            }
        }

        LogContainerCmd logContainerCmd = this.dockerClient.logContainerCmd(containerId);
        logContainerCmd.withStdOut(true).withStdErr(true).withFollowStream(true);
        logContainerCmd.exec(new LogContainerResultCallback() {
            @Override
            public void onNext(Frame item) {
                log.info(new String(item.getPayload(), UTF_8));
            }
        });
    });
}
 
Example #3
Source File: ChaosContainer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public String getContainerLog() {
    StringBuilder sb = new StringBuilder();

    LogContainerCmd logContainerCmd = this.dockerClient.logContainerCmd(containerId);
    logContainerCmd.withStdOut(true).withStdErr(true);
    try {
        logContainerCmd.exec(new LogContainerResultCallback() {
            @Override
            public void onNext(Frame item) {
                sb.append(new String(item.getPayload(), UTF_8));
            }
        }).awaitCompletion();
    } catch (InterruptedException e) {

    }
    return sb.toString();
}
 
Example #4
Source File: DockerContainersUtil.java    From minimesos with Apache License 2.0 6 votes vote down vote up
/**
 * Synchronized method for returning logs of docker container
 *
 * @param containerId - ID of the container ot lookup logs
 * @return list of strings, where every string is log line
 */
public static List<String> getDockerLogs(String containerId) {

    final List<String> logs = new ArrayList<>();

    LogContainerCmd logContainerCmd = DockerClientFactory.build().logContainerCmd(containerId);
    logContainerCmd.withStdOut(true).withStdErr(true);
    try {
        logContainerCmd.exec(new LogContainerResultCallback() {
            @Override
            public void onNext(Frame item) {
                logs.add(item.toString());
            }
        }).awaitCompletion();
    } catch (InterruptedException e) {
        throw new MinimesosException("Failed to retrieve logs of container " + containerId, e);
    }

    return logs;
}
 
Example #5
Source File: LogUtils.java    From testcontainers-java with MIT License 6 votes vote down vote up
private static Closeable attachConsumer(
    DockerClient dockerClient,
    String containerId,
    Consumer<OutputFrame> consumer,
    boolean followStream,
    OutputFrame.OutputType... types
) {

    final LogContainerCmd cmd = dockerClient.logContainerCmd(containerId)
        .withFollowStream(followStream)
        .withSince(0);

    final FrameConsumerResultCallback callback = new FrameConsumerResultCallback();
    for (OutputFrame.OutputType type : types) {
        callback.addConsumer(type, consumer);
        if (type == STDOUT) cmd.withStdOut(true);
        if (type == STDERR) cmd.withStdErr(true);
    }

    return cmd.exec(callback);
}
 
Example #6
Source File: LogContainerCmdExec.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Override
protected Void execute0(LogContainerCmd command, ResultCallback<Frame> resultCallback) {

    WebTarget webTarget = getBaseResource().path("/containers/{id}/logs").resolveTemplate("id",
            command.getContainerId());

    if (command.getTail() != null) {
        webTarget = webTarget.queryParam("tail", command.getTail());
    }

    if (command.getSince() != null) {
        webTarget = webTarget.queryParam("since", command.getSince());
    }

    webTarget = booleanQueryParam(webTarget, "timestamps", command.hasTimestampsEnabled());
    webTarget = booleanQueryParam(webTarget, "stdout", command.hasStdoutEnabled());
    webTarget = booleanQueryParam(webTarget, "stderr", command.hasStderrEnabled());
    webTarget = booleanQueryParam(webTarget, "follow", command.hasFollowStreamEnabled());

    LOGGER.trace("GET: {}", webTarget);

    webTarget.request().get(resultCallback);

    return null;
}
 
Example #7
Source File: LogMessageWaitStrategy.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
@SneakyThrows(IOException.class)
protected void waitUntilReady() {
    WaitingConsumer waitingConsumer = new WaitingConsumer();

    LogContainerCmd cmd = DockerClientFactory.instance().client().logContainerCmd(waitStrategyTarget.getContainerId())
        .withFollowStream(true)
        .withSince(0)
        .withStdOut(true)
        .withStdErr(true);

    try (FrameConsumerResultCallback callback = new FrameConsumerResultCallback()) {
        callback.addConsumer(STDOUT, waitingConsumer);
        callback.addConsumer(STDERR, waitingConsumer);

        cmd.exec(callback);

        Predicate<OutputFrame> waitPredicate = outputFrame ->
            // (?s) enables line terminator matching (equivalent to Pattern.DOTALL)
            outputFrame.getUtf8String().matches("(?s)" + regEx);

        try {
            waitingConsumer.waitUntil(waitPredicate, startupTimeout.getSeconds(), TimeUnit.SECONDS, times);
        } catch (TimeoutException e) {
            throw new ContainerLaunchException("Timed out waiting for log output matching '" + regEx + "'");
        }
    }
}
 
Example #8
Source File: DelegatingDockerClient.java    From docker-plugin with MIT License 4 votes vote down vote up
@Override
public LogContainerCmd logContainerCmd(String arg0) {
    return getDelegate().logContainerCmd(arg0);
}
 
Example #9
Source File: LogContainerCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public LogContainerCmd withSince(Integer since) {
    this.since = since;
    return this;
}
 
Example #10
Source File: LogContainerCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public LogContainerCmd withTail(Integer tail) {
    this.tail = tail;
    return this;
}
 
Example #11
Source File: LogContainerCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public LogContainerCmd withTailAll() {
    this.tail = -1;
    return this;
}
 
Example #12
Source File: LogContainerCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public LogContainerCmd withStdErr(Boolean stderr) {
    this.stderr = stderr;
    return this;
}
 
Example #13
Source File: LogContainerCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public LogContainerCmd withStdOut(Boolean stdout) {
    this.stdout = stdout;
    return this;
}
 
Example #14
Source File: LogContainerCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public LogContainerCmd withTimestamps(Boolean timestamps) {
    this.timestamps = timestamps;
    return this;
}
 
Example #15
Source File: LogContainerCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public LogContainerCmd withFollowStream(Boolean followStream) {
    this.followStream = followStream;
    return this;
}
 
Example #16
Source File: LogContainerCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public LogContainerCmd withContainerId(String containerId) {
    checkNotNull(containerId, "containerId was not specified");
    this.containerId = containerId;
    return this;
}
 
Example #17
Source File: LogContainerCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
public LogContainerCmdImpl(LogContainerCmd.Exec exec, String containerId) {
    super(exec);
    withContainerId(containerId);
}
 
Example #18
Source File: AbstractDockerCmdExecFactory.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public LogContainerCmd.Exec createLogContainerCmdExec() {
    return new LogContainerCmdExec(getBaseResource(), getDockerClientConfig());
}
 
Example #19
Source File: DockerClientImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public LogContainerCmd logContainerCmd(String containerId) {
    return new LogContainerCmdImpl(getDockerCmdExecFactory().createLogContainerCmdExec(), containerId);
}
 
Example #20
Source File: DockerClient.java    From docker-java with Apache License 2.0 votes vote down vote up
LogContainerCmd logContainerCmd(@Nonnull String containerId);