Skip to content

Commit

Permalink
[Jib CLI] log exception with ConsoleLogger (#3162)
Browse files Browse the repository at this point in the history
* Log exception with ConsoleLogger
* Update CHANGELOG
  • Loading branch information
chanseokoh committed Mar 23, 2021
1 parent 3cbf30f commit 64afcd8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 24 deletions.
2 changes: 2 additions & 0 deletions jib-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ All notable changes to this project will be documented in this file.

### Fixed

- 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))

## 0.4.0

### Added
Expand Down
21 changes: 9 additions & 12 deletions jib-cli/src/main/java/com/google/cloud/tools/jib/cli/Build.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ public Integer call() {
commonCliOptions.validate();
Path buildFile = getBuildFile();
SingleThreadedExecutor executor = new SingleThreadedExecutor();
ConsoleLogger logger =
CliLogger.newLogger(
commonCliOptions.getVerbosity(),
commonCliOptions.getHttpTrace(),
commonCliOptions.getConsoleOutput(),
spec.commandLine().getOut(),
spec.commandLine().getErr(),
executor);
try {
ConsoleLogger logger =
CliLogger.newLogger(
commonCliOptions.getVerbosity(),
commonCliOptions.getHttpTrace(),
commonCliOptions.getConsoleOutput(),
spec.commandLine().getOut(),
spec.commandLine().getErr(),
executor);
JibCli.configureHttpLogging(commonCliOptions.getHttpTrace().toJulLevel());

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

containerBuilder.containerize(containerizer);
} catch (Exception ex) {
if (commonCliOptions.isStacktrace()) {
ex.printStackTrace();
}
System.err.println(ex.getClass().getName() + ": " + ex.getMessage());
JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
return 1;
} finally {
executor.shutDownAndAwaitTermination(Duration.ofSeconds(3));
Expand Down
21 changes: 9 additions & 12 deletions jib-cli/src/main/java/com/google/cloud/tools/jib/cli/Jar.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ public class Jar implements Callable<Integer> {
public Integer call() {
commonCliOptions.validate();
SingleThreadedExecutor executor = new SingleThreadedExecutor();
ConsoleLogger logger =
CliLogger.newLogger(
commonCliOptions.getVerbosity(),
commonCliOptions.getHttpTrace(),
commonCliOptions.getConsoleOutput(),
spec.commandLine().getOut(),
spec.commandLine().getErr(),
executor);
try {
ConsoleLogger logger =
CliLogger.newLogger(
commonCliOptions.getVerbosity(),
commonCliOptions.getHttpTrace(),
commonCliOptions.getConsoleOutput(),
spec.commandLine().getOut(),
spec.commandLine().getErr(),
executor);
JibCli.configureHttpLogging(commonCliOptions.getHttpTrace().toJulLevel());

if (!Files.exists(jarFile)) {
Expand Down Expand Up @@ -204,10 +204,7 @@ public Integer call() {

containerBuilder.containerize(containerizer);
} catch (Exception ex) {
if (commonCliOptions.isStacktrace()) {
ex.printStackTrace();
}
System.err.println(ex.getClass().getName() + ": " + ex.getMessage());
JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
return 1;
} finally {
executor.shutDownAndAwaitTermination(Duration.ofSeconds(3));
Expand Down
21 changes: 21 additions & 0 deletions jib-cli/src/main/java/com/google/cloud/tools/jib/cli/JibCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
import com.google.cloud.tools.jib.api.LogEvent;
import com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -46,6 +50,23 @@ static Logger configureHttpLogging(Level level) {
return logger;
}

static void logTerminatingException(
ConsoleLogger consoleLogger, Exception exception, boolean logStackTrace) {
if (logStackTrace) {
StringWriter writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
consoleLogger.log(LogEvent.Level.ERROR, writer.toString());
}

consoleLogger.log(
LogEvent.Level.ERROR,
"\u001B[31;1m"
+ exception.getClass().getName()
+ ": "
+ exception.getMessage()
+ "\u001B[0m");
}

/**
* The magic starts here.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@
package com.google.cloud.tools.jib.cli;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import com.google.cloud.tools.jib.api.LogEvent;
import com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
Expand All @@ -37,4 +45,27 @@ public void testConfigureHttpLogging() {
assertThat(handler).isInstanceOf(ConsoleHandler.class);
assertThat(handler.getLevel()).isEqualTo(Level.ALL);
}

@Test
public void testLogTerminatingException() {
ConsoleLogger logger = mock(ConsoleLogger.class);
JibCli.logTerminatingException(logger, new IOException("test error message"), false);

verify(logger)
.log(LogEvent.Level.ERROR, "\u001B[31;1mjava.io.IOException: test error message\u001B[0m");
verifyNoMoreInteractions(logger);
}

@Test
public void testLogTerminatingException_stackTrace() {
ConsoleLogger logger = mock(ConsoleLogger.class);
JibCli.logTerminatingException(logger, new IOException("test error message"), true);

String stackTraceLine =
"at com.google.cloud.tools.jib.cli.JibCliTest.testLogTerminatingException_stackTrace";
verify(logger).log(eq(LogEvent.Level.ERROR), contains(stackTraceLine));
verify(logger)
.log(LogEvent.Level.ERROR, "\u001B[31;1mjava.io.IOException: test error message\u001B[0m");
verifyNoMoreInteractions(logger);
}
}

0 comments on commit 64afcd8

Please sign in to comment.