git-cl: Make request to upload logs more visible.

Print the message asking the user to upload gerrit logs for git-cl upload
failures after the exception.

Also, initialize the error logger only for git-cl upload, so that the
headers are not overwritten unless git-cl upload is executed again.

Bug: 881860
Change-Id: I8e63432eecca5253e56096e3ad0218eec4058e12
Reviewed-on: https://chromium-review.googlesource.com/c/1277426
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: Andrii Shyshkalov <tandrii@chromium.org>
diff --git a/git_cl.py b/git_cl.py
index e85b241..93e2198 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -3170,6 +3170,15 @@
 
     try:
       # TODO(crbug.com/881860): Remove.
+      # Clear the log after each git-cl upload run by setting mode='w'.
+      handler = logging.FileHandler(gerrit_util.GERRIT_ERR_LOG_FILE, mode='w')
+      handler.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
+
+      GERRIT_ERR_LOGGER.addHandler(handler)
+      GERRIT_ERR_LOGGER.setLevel(logging.INFO)
+      # Don't propagate to root logger, so that logs are not printed.
+      GERRIT_ERR_LOGGER.propagate = 0
+
       # Get interesting headers from git push, to be displayed to the user if
       # subsequent Gerrit RPC calls fail.
       env = os.environ.copy()
@@ -6258,16 +6267,6 @@
         format='[%(levelname).1s%(asctime)s %(process)d %(thread)d '
                '%(filename)s] %(message)s')
 
-    # TODO(crbug.com/881860): Remove.
-    # Clear the log after each git-cl run by setting mode='w'.
-    handler = logging.FileHandler(gerrit_util.GERRIT_ERR_LOG_FILE, mode='w')
-    handler.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
-
-    GERRIT_ERR_LOGGER.addHandler(handler)
-    GERRIT_ERR_LOGGER.setLevel(logging.INFO)
-    # Don't propagate to root logger, so that logs are not printed.
-    GERRIT_ERR_LOGGER.propagate = 0
-
     return options, args