Skip to content

Commit 9adb71e

Browse files
authored
Disables Apache HTTP logging for build to Docker daemon. (GoogleContainerTools#342)
1 parent 56f5ff3 commit 9adb71e

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.tools.jib.gradle;
1818

19+
import com.google.api.client.http.HttpTransport;
1920
import com.google.cloud.tools.jib.builder.BuildConfiguration;
2021
import com.google.cloud.tools.jib.docker.DockerClient;
2122
import com.google.cloud.tools.jib.frontend.BuildStepsExecutionException;
@@ -25,18 +26,28 @@
2526
import com.google.cloud.tools.jib.http.Authorization;
2627
import com.google.cloud.tools.jib.image.ImageReference;
2728
import com.google.cloud.tools.jib.image.InvalidImageReferenceException;
29+
import com.google.cloud.tools.jib.registry.RegistryClient;
2830
import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials;
2931
import com.google.common.base.Preconditions;
32+
import java.util.logging.Level;
33+
import java.util.logging.Logger;
3034
import javax.annotation.Nullable;
3135
import org.gradle.api.DefaultTask;
3236
import org.gradle.api.GradleException;
3337
import org.gradle.api.tasks.Nested;
3438
import org.gradle.api.tasks.TaskAction;
3539
import org.gradle.api.tasks.options.Option;
40+
import org.gradle.internal.logging.events.LogEvent;
41+
import org.gradle.internal.logging.events.OutputEventListener;
42+
import org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext;
43+
import org.slf4j.LoggerFactory;
3644

3745
/** Builds a container image and exports to the default Docker daemon. */
3846
public class BuildDockerTask extends DefaultTask {
3947

48+
/** {@code User-Agent} header suffix to send to the registry. */
49+
private static final String USER_AGENT_SUFFIX = "jib-gradle-plugin";
50+
4051
private static final HelpfulSuggestions HELPFUL_SUGGESTIONS =
4152
HelpfulSuggestionsProvider.get("Build to Docker daemon failed");
4253

@@ -86,6 +97,26 @@ public void buildDocker() throws InvalidImageReferenceException {
8697
.setJvmFlags(jibExtension.getJvmFlags())
8798
.build();
8899

100+
// TODO: Consolidate with BuildImageTask
101+
// TODO: Instead of disabling logging, have authentication credentials be provided
102+
// Disables annoying Apache HTTP client logging.
103+
// Note that this is a hack and depends on internal Gradle classes
104+
OutputEventListenerBackedLoggerContext context =
105+
(OutputEventListenerBackedLoggerContext) LoggerFactory.getILoggerFactory();
106+
OutputEventListener defaultOutputEventListener = context.getOutputEventListener();
107+
context.setOutputEventListener(
108+
event -> {
109+
LogEvent logEvent = (LogEvent) event;
110+
if (!logEvent.getCategory().contains("org.apache")) {
111+
defaultOutputEventListener.onOutput(event);
112+
}
113+
});
114+
115+
// Disables Google HTTP client logging.
116+
Logger.getLogger(HttpTransport.class.getName()).setLevel(Level.OFF);
117+
118+
RegistryClient.setUserAgentSuffix(USER_AGENT_SUFFIX);
119+
89120
// Uses a directory in the Gradle build cache as the Jib cache.
90121
try {
91122
BuildStepsRunner.forBuildToDockerDaemon(

‎jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.tools.jib.frontend.CacheDirectoryCreationException;
2424
import com.google.cloud.tools.jib.frontend.HelpfulSuggestions;
2525
import com.google.cloud.tools.jib.image.ImageReference;
26+
import com.google.cloud.tools.jib.registry.RegistryClient;
2627
import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials;
2728
import com.google.common.base.Preconditions;
2829
import org.apache.maven.plugin.MojoExecutionException;
@@ -33,6 +34,9 @@
3334
@Mojo(name = "buildDocker", requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM)
3435
public class BuildDockerMojo extends JibPluginConfiguration {
3536

37+
/** {@code User-Agent} header suffix to send to the registry. */
38+
private static final String USER_AGENT_SUFFIX = "jib-maven-plugin";
39+
3640
private static final HelpfulSuggestions HELPFUL_SUGGESTIONS =
3741
HelpfulSuggestionsProvider.get("Build to Docker daemon failed");
3842

@@ -68,6 +72,15 @@ public void execute() throws MojoExecutionException {
6872
.setEnvironment(getEnvironment())
6973
.build();
7074

75+
// TODO: Consolidate with BuildImageMojo
76+
// TODO: Instead of disabling logging, have authentication credentials be provided
77+
// Disables annoying Apache HTTP client logging.
78+
System.setProperty(
79+
"org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
80+
System.setProperty("org.apache.commons.logging.simplelog.defaultlog", "error");
81+
82+
RegistryClient.setUserAgentSuffix(USER_AGENT_SUFFIX);
83+
7184
try {
7285
BuildStepsRunner.forBuildToDockerDaemon(
7386
buildConfiguration,

0 commit comments

Comments
 (0)