Skip to content

Commit 64afcd8

Browse files
authored
[Jib CLI] log exception with ConsoleLogger (#3162)
* Log exception with ConsoleLogger * Update CHANGELOG
1 parent 3cbf30f commit 64afcd8

File tree

5 files changed

+72
-24
lines changed

5 files changed

+72
-24
lines changed

jib-cli/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ All notable changes to this project will be documented in this file.
99

1010
### Fixed
1111

12+
- Fixed an issue where critical error messages (for example, unauthorized access from a registry) were erased by progress reporting and not shown. ([#3148](https://github.com/GoogleContainerTools/jib/issues/3148))
13+
1214
## 0.4.0
1315

1416
### Added

jib-cli/src/main/java/com/google/cloud/tools/jib/cli/Build.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ public Integer call() {
9898
commonCliOptions.validate();
9999
Path buildFile = getBuildFile();
100100
SingleThreadedExecutor executor = new SingleThreadedExecutor();
101+
ConsoleLogger logger =
102+
CliLogger.newLogger(
103+
commonCliOptions.getVerbosity(),
104+
commonCliOptions.getHttpTrace(),
105+
commonCliOptions.getConsoleOutput(),
106+
spec.commandLine().getOut(),
107+
spec.commandLine().getErr(),
108+
executor);
101109
try {
102-
ConsoleLogger logger =
103-
CliLogger.newLogger(
104-
commonCliOptions.getVerbosity(),
105-
commonCliOptions.getHttpTrace(),
106-
commonCliOptions.getConsoleOutput(),
107-
spec.commandLine().getOut(),
108-
spec.commandLine().getErr(),
109-
executor);
110110
JibCli.configureHttpLogging(commonCliOptions.getHttpTrace().toJulLevel());
111111

112112
if (!Files.isReadable(buildFile)) {
@@ -133,10 +133,7 @@ public Integer call() {
133133

134134
containerBuilder.containerize(containerizer);
135135
} catch (Exception ex) {
136-
if (commonCliOptions.isStacktrace()) {
137-
ex.printStackTrace();
138-
}
139-
System.err.println(ex.getClass().getName() + ": " + ex.getMessage());
136+
JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
140137
return 1;
141138
} finally {
142139
executor.shutDownAndAwaitTermination(Duration.ofSeconds(3));

jib-cli/src/main/java/com/google/cloud/tools/jib/cli/Jar.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,15 @@ public class Jar implements Callable<Integer> {
165165
public Integer call() {
166166
commonCliOptions.validate();
167167
SingleThreadedExecutor executor = new SingleThreadedExecutor();
168+
ConsoleLogger logger =
169+
CliLogger.newLogger(
170+
commonCliOptions.getVerbosity(),
171+
commonCliOptions.getHttpTrace(),
172+
commonCliOptions.getConsoleOutput(),
173+
spec.commandLine().getOut(),
174+
spec.commandLine().getErr(),
175+
executor);
168176
try {
169-
ConsoleLogger logger =
170-
CliLogger.newLogger(
171-
commonCliOptions.getVerbosity(),
172-
commonCliOptions.getHttpTrace(),
173-
commonCliOptions.getConsoleOutput(),
174-
spec.commandLine().getOut(),
175-
spec.commandLine().getErr(),
176-
executor);
177177
JibCli.configureHttpLogging(commonCliOptions.getHttpTrace().toJulLevel());
178178

179179
if (!Files.exists(jarFile)) {
@@ -204,10 +204,7 @@ public Integer call() {
204204

205205
containerBuilder.containerize(containerizer);
206206
} catch (Exception ex) {
207-
if (commonCliOptions.isStacktrace()) {
208-
ex.printStackTrace();
209-
}
210-
System.err.println(ex.getClass().getName() + ": " + ex.getMessage());
207+
JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
211208
return 1;
212209
} finally {
213210
executor.shutDownAndAwaitTermination(Duration.ofSeconds(3));

jib-cli/src/main/java/com/google/cloud/tools/jib/cli/JibCli.java

+21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
import com.google.api.client.http.HttpTransport;
2020
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
21+
import com.google.cloud.tools.jib.api.LogEvent;
22+
import com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger;
23+
import java.io.PrintWriter;
24+
import java.io.StringWriter;
2125
import java.util.logging.ConsoleHandler;
2226
import java.util.logging.Level;
2327
import java.util.logging.Logger;
@@ -46,6 +50,23 @@ static Logger configureHttpLogging(Level level) {
4650
return logger;
4751
}
4852

53+
static void logTerminatingException(
54+
ConsoleLogger consoleLogger, Exception exception, boolean logStackTrace) {
55+
if (logStackTrace) {
56+
StringWriter writer = new StringWriter();
57+
exception.printStackTrace(new PrintWriter(writer));
58+
consoleLogger.log(LogEvent.Level.ERROR, writer.toString());
59+
}
60+
61+
consoleLogger.log(
62+
LogEvent.Level.ERROR,
63+
"\u001B[31;1m"
64+
+ exception.getClass().getName()
65+
+ ": "
66+
+ exception.getMessage()
67+
+ "\u001B[0m");
68+
}
69+
4970
/**
5071
* The magic starts here.
5172
*

jib-cli/src/test/java/com/google/cloud/tools/jib/cli/JibCliTest.java

+31
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@
1717
package com.google.cloud.tools.jib.cli;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.mockito.ArgumentMatchers.contains;
21+
import static org.mockito.ArgumentMatchers.eq;
22+
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.verify;
24+
import static org.mockito.Mockito.verifyNoMoreInteractions;
2025

26+
import com.google.cloud.tools.jib.api.LogEvent;
27+
import com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger;
28+
import java.io.IOException;
2129
import java.util.logging.ConsoleHandler;
2230
import java.util.logging.Handler;
2331
import java.util.logging.Level;
@@ -37,4 +45,27 @@ public void testConfigureHttpLogging() {
3745
assertThat(handler).isInstanceOf(ConsoleHandler.class);
3846
assertThat(handler.getLevel()).isEqualTo(Level.ALL);
3947
}
48+
49+
@Test
50+
public void testLogTerminatingException() {
51+
ConsoleLogger logger = mock(ConsoleLogger.class);
52+
JibCli.logTerminatingException(logger, new IOException("test error message"), false);
53+
54+
verify(logger)
55+
.log(LogEvent.Level.ERROR, "\u001B[31;1mjava.io.IOException: test error message\u001B[0m");
56+
verifyNoMoreInteractions(logger);
57+
}
58+
59+
@Test
60+
public void testLogTerminatingException_stackTrace() {
61+
ConsoleLogger logger = mock(ConsoleLogger.class);
62+
JibCli.logTerminatingException(logger, new IOException("test error message"), true);
63+
64+
String stackTraceLine =
65+
"at com.google.cloud.tools.jib.cli.JibCliTest.testLogTerminatingException_stackTrace";
66+
verify(logger).log(eq(LogEvent.Level.ERROR), contains(stackTraceLine));
67+
verify(logger)
68+
.log(LogEvent.Level.ERROR, "\u001B[31;1mjava.io.IOException: test error message\u001B[0m");
69+
verifyNoMoreInteractions(logger);
70+
}
4071
}

0 commit comments

Comments
 (0)