/** * Show stderr log. Default false. * * @param stderr Whether to show stderr log. * @return LogsParam */ public static LogsParam stderr(final boolean stderr) { return create("stderr", String.valueOf(stderr)); }
@Override public LogStream serviceLogs(String serviceId, LogsParam... params) throws DockerException, InterruptedException { assertApiVersionIsAbove("1.25"); WebTarget resource = noTimeoutResource() .path("services").path(serviceId) .path("logs"); for (final LogsParam param : params) { resource = resource.queryParam(param.name(), param.value()); } return getServiceLogStream(GET, resource, serviceId); }
/** * GEts the logs for a specific container * @param containerId * @param from the number of lines to skip (to avoid killing the clients with giant logs */ private HashMap<String, Object> getContainerLogs(String containerId, int from) throws DockerException, InterruptedException { final String logs; try (LogStream stream = client.logs(containerId, DockerClient.LogsParam.stdout(), DockerClient.LogsParam.stderr())) { logs = stream.readFully(); } String[] split = logs.split("\n"); List<String> lines = Stream.of(split) .skip(from) .collect(Collectors.toList()); var result = new HashMap<String, Object>(); result.put("lines", lines); result.put("total", split.length); return result; } }
private TaskExecutionResult collectTaskExecutionResult(DockerTask task, String containerId) throws DockerException, InterruptedException { final ContainerExit exit = docker.waitContainer(containerId); log.info("Container " + containerId + " finished with exit code " + exit.statusCode()); TaskExecutionResult result = new TaskExecutionResult(task); // TODO : Do some descent exception treatment if (exit.statusCode() != 0) { result.markAsFinished(exit.statusCode()); } else { result.markAsFinishedWithSuccess(); } log.info("Collecting container logs for " + containerId); String logs = docker.logs(containerId, LogsParam.stdout(), LogsParam.stderr()).readFully(); // Remove the last new line if needed if (logs.endsWith("\n")) { logs = logs.substring(0, logs.length() - 1); } result.setOutput(logs); return result; }
/** * Collect logs from container and write to the log directory */ private void collectLogs() { if (!loggingParams.isEnabled()) { log.debug("Logging is disabled"); return; } File serverLogDir = loggingParams.getServerLogDir().toFile(); if (!serverLogDir.exists() && serverLogDir.mkdirs()) { log.info("Created new corfu log directory at {}.", serverLogDir); } log.debug("Collect logs for: {}", params.getName()); try (LogStream stream = docker.logs(params.getName(), LogsParam.stdout(), LogsParam.stderr())) { String logs = stream.readFully(); if (StringUtils.isEmpty(logs)) { log.warn("Empty logs from container: {}", params.getName()); } Path filePathObj = loggingParams.getServerLogDir().resolve(params.getName() + ".log"); Files.write(filePathObj, logs.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (InterruptedException | DockerException | IOException e) { log.error("Can't collect logs from container: {}", params.getName(), e); } }
/** * Container log. */ public String getLog() { try (LogStream stream = dockerClient.logs(container.id(), LogsParam.stdout(), LogsParam.stderr());) { String fullLog = stream.readFully(); if (log.isTraceEnabled()) { log.trace("{} full log: {}", containerShortId, StringUtils.replace(fullLog, "\n", "|")); } return fullLog; } catch (DockerException | InterruptedException e) { throw new IllegalStateException(e); } }
@Override public Void call() throws Exception { log.trace("{} attaching to logs", containerShortId); LogStream logs = dockerClient.logs(containerId, LogsParam.stdout(), LogsParam.stderr(), LogsParam.follow()); try { logs.attach(logSplitter.getStdoutOutput(), logSplitter.getStderrOutput()); } finally { IOUtils.closeQuietly(logs, logSplitter); log.trace("{} dettached from logs", containerShortId); } return null; } });
/** * Show stdout log. * * @return LogsParam */ public static LogsParam stdout() { return stdout(true); }
@Override public LogStream serviceLogs(String serviceId, LogsParam... params) throws DockerException, InterruptedException { assertApiVersionIsAbove("1.25"); WebTarget resource = noTimeoutResource() .path("services").path(serviceId) .path("logs"); for (final LogsParam param : params) { resource = resource.queryParam(param.name(), param.value()); } return getServiceLogStream(GET, resource, serviceId); }
/** * Print timestamp for every log line. * * @return LogsParam */ public static LogsParam timestamps() { return timestamps(true); }
/** * Create a custom parameter. * * @param name custom name * @param value custom value * @return LogsParam */ public static LogsParam create(final String name, final String value) { return new LogsParam(name, value); } }
/** * Show stderr log. Default false. * * @param stderr Whether to show stderr log. * @return LogsParam */ public static LogsParam stderr(final boolean stderr) { return create("stderr", String.valueOf(stderr)); }
/** * Return stream. * * @return LogsParam */ public static LogsParam follow() { return follow(true); }
/** * Print timestamp for every log line. Default false. * * @param timestamps Whether to print timestamp for every log line. * @return LogsParam */ public static LogsParam timestamps(final boolean timestamps) { return create("timestamps", String.valueOf(timestamps)); }
/** * Filter logs and only output entries since given Unix timestamp. Only available in Docker API * >= 1.19. * * @param timestamp Only output entries since timestamp. * @return LogsParam */ public static LogsParam since(final Integer timestamp) { return create("since", String.valueOf(timestamp)); }
/** * Show stdout log. Default false. * * @param stdout Whether to show stdout log. * @return LogsParam */ public static LogsParam stdout(final boolean stdout) { return create("stdout", String.valueOf(stdout)); }
protected void waitForLog(String messageToMatch) throws DockerException, InterruptedException, UnsupportedEncodingException { LogStream logs = dockerClient.logs(container.id(), follow(), stdout()); String log; do { LogMessage logMessage = logs.next(); ByteBuffer buffer = logMessage.content(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); log = new String(bytes); } while (!log.contains(messageToMatch)); } }
@Override public LogStream logs(final String containerId, final LogsParam... params) throws DockerException, InterruptedException { WebTarget resource = noTimeoutResource() .path("containers").path(containerId) .path("logs"); for (final LogsParam param : params) { resource = resource.queryParam(param.name(), param.value()); } return getLogStream(GET, resource, containerId); }