Polish "Improve logging in DockerApi"

See gh-44412
This commit is contained in:
Moritz Halbritter 2025-02-25 13:14:38 +01:00
parent 9a940702bc
commit 3be5e6cc3e
5 changed files with 12 additions and 110 deletions

View File

@ -76,7 +76,7 @@ public class Builder {
* @param log a logger used to record output
*/
public Builder(BuildLog log) {
this(log, new DockerApi(null, BuildLogDockerLogDelegate.get(log)), null);
this(log, new DockerApi(null, BuildLogAdapter.get(log)), null);
}
/**
@ -87,7 +87,7 @@ public class Builder {
*/
public Builder(BuildLog log, DockerConfiguration dockerConfiguration) {
this(log, new DockerApi((dockerConfiguration != null) ? dockerConfiguration.getHost() : null,
BuildLogDockerLogDelegate.get(log)), dockerConfiguration);
BuildLogAdapter.get(log)), dockerConfiguration);
}
Builder(BuildLog log, DockerApi docker, DockerConfiguration dockerConfiguration) {
@ -264,14 +264,13 @@ public class Builder {
}
/**
* A {@link DockerLog} implementation that delegates logging to a provided
* {@link AbstractBuildLog}.
* A {@link DockerLog} implementation that adapts to an {@link AbstractBuildLog}.
*/
static final class BuildLogDockerLogDelegate implements DockerLog {
static final class BuildLogAdapter implements DockerLog {
private final AbstractBuildLog log;
private BuildLogDockerLogDelegate(AbstractBuildLog log) {
private BuildLogAdapter(AbstractBuildLog log) {
this.log = log;
}
@ -284,14 +283,14 @@ public class Builder {
* Creates{@link DockerLog} instance based on the provided {@link BuildLog}.
* <p>
* If the provided {@link BuildLog} instance is an {@link AbstractBuildLog}, the
* method returns a {@link BuildLogDockerLogDelegate}, otherwise it returns a
* default {@link DockerLog#toSystemOut()}.
* method returns a {@link BuildLogAdapter}, otherwise it returns a default
* {@link DockerLog#toSystemOut()}.
* @param log the {@link BuildLog} instance to delegate
* @return a {@link DockerLog} instance for logging
*/
static DockerLog get(BuildLog log) {
if (log instanceof AbstractBuildLog) {
return new BuildLogDockerLogDelegate(((AbstractBuildLog) log));
if (log instanceof AbstractBuildLog abstractBuildLog) {
return new BuildLogAdapter(abstractBuildLog);
}
return DockerLog.toSystemOut();
}

View File

@ -48,7 +48,7 @@ public interface DockerLog {
* @return {@link DockerLog} instance that logs to the given print stream
*/
static DockerLog to(PrintStream out) {
return new PrintStreamDockerLog(out);
return out::println;
}
}

View File

@ -1,42 +0,0 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.buildpack.platform.docker;
import java.io.PrintStream;
import org.springframework.util.Assert;
/**
* {@link DockerLog} implementation that prints output to a {@link PrintStream}.
*
* @author Dmytro Nosan
*/
class PrintStreamDockerLog implements DockerLog {
private final PrintStream stream;
PrintStreamDockerLog(PrintStream stream) {
Assert.notNull(stream, "'stream' must not be null");
this.stream = stream;
}
@Override
public void log(String message) {
this.stream.println(message);
}
}

View File

@ -25,7 +25,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.stubbing.Answer;
import org.springframework.boot.buildpack.platform.build.Builder.BuildLogDockerLogDelegate;
import org.springframework.boot.buildpack.platform.build.Builder.BuildLogAdapter;
import org.springframework.boot.buildpack.platform.docker.DockerApi;
import org.springframework.boot.buildpack.platform.docker.DockerApi.ContainerApi;
import org.springframework.boot.buildpack.platform.docker.DockerApi.ImageApi;
@ -84,7 +84,7 @@ class BuilderTests {
assertThat(builder).extracting("docker")
.extracting("system")
.extracting("log")
.isInstanceOf(BuildLogDockerLogDelegate.class);
.isInstanceOf(BuildLogAdapter.class);
}
@Test

View File

@ -1,55 +0,0 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.buildpack.platform.docker;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link PrintStreamDockerLog}.
*
* @author Dmytro Nosan
*/
class PrintStreamDockerLogTests {
@Test
void printsExpectedOutput() {
TestPrintStream stream = new TestPrintStream();
PrintStreamDockerLog logger = new PrintStreamDockerLog(stream);
logger.log("Some message");
logger.log("Some message1");
assertThat(stream.toString()).isEqualTo(String.format("Some message%nSome message1%n"));
}
static class TestPrintStream extends PrintStream {
TestPrintStream() {
super(new ByteArrayOutputStream());
}
@Override
public String toString() {
return this.out.toString();
}
}
}