Print hook stdout/stderr in case of a non-zero exit code

Prior to this change the hook output was only printed when the logging
level was at least DEBUG. However, when a hook exits with an error we
need to read the hook output in order to get more details about the
error.

Release-Notes: Print hook stdout/stderr in case of a non-zero exit code
Change-Id: I24b9f69c9faea561d7fce0b3818801dd64852729
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookTask.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookTask.java
index b7a0784..c5837c4 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookTask.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookTask.java
@@ -30,6 +30,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Callable;
+import java.util.logging.Level;
 import org.eclipse.jgit.lib.Repository;
 
 class HookTask {
@@ -124,22 +125,27 @@
       int exitValue = result.getExitValue();
       if (exitValue != 0) {
         logger.atSevere().log("hook[%s] exited with error status: %d", name, exitValue);
+        printHookOutput(name, result, Level.SEVERE);
       }
 
       if (logger.atFine().isEnabled()) {
-        try (BufferedReader br = new BufferedReader(new StringReader(result.getOutput()))) {
-          br.lines()
-              .filter(s -> !s.isEmpty())
-              .forEach(line -> logger.atFine().log("hook[%s] output: %s", name, line));
-        } catch (IOException iox) {
-          logger.atSevere().withCause(iox).log("Error writing hook [%s] output", name);
-        }
+        printHookOutput(name, result, Level.FINE);
       }
     }
 
     return result;
   }
 
+  private void printHookOutput(String name, HookResult result, Level logLevel) {
+    try (BufferedReader br = new BufferedReader(new StringReader(result.getOutput()))) {
+      br.lines()
+          .filter(s -> !s.isEmpty())
+          .forEach(line -> logger.at(logLevel).log("hook[%s] output: %s", name, line));
+    } catch (IOException iox) {
+      logger.atSevere().withCause(iox).log("Error writing hook [%s] output", name);
+    }
+  }
+
   @Override
   public String toString() {
     return "hook " + hook.getFileName();