git cl: ensure logging is properly initialized.

R=ehmaldonado

Bug: 877717
Change-Id: If8cce9453ca72b3b300902f5a03e7a8aa93d17b5
Reviewed-on: https://chromium-review.googlesource.com/c/1296856
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Andrii Shyshkalov <tandrii@chromium.org>
diff --git a/git_cl.py b/git_cl.py
index 5bf0ff0..7fe4de7 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -5878,6 +5878,22 @@
         help='Use 2 times for more debugging info')
 
   def parse_args(self, args=None, _values=None):
+    try:
+      return self._parse_args(args)
+    finally:
+      # Regardless of success or failure of args parsing, we want to report
+      # metrics, but only after logging has been initialized (if parsing
+      # succeeded).
+      global settings
+      settings = Settings()
+
+      if not metrics.DISABLE_METRICS_COLLECTION:
+        # GetViewVCUrl ultimately calls logging method.
+        project_url = settings.GetViewVCUrl().strip('/+')
+        if project_url in metrics_utils.KNOWN_PROJECT_URLS:
+          metrics.collector.add('project_urls', [project_url])
+
+  def _parse_args(self, args=None):
     # Create an optparse.Values object that will store only the actual passed
     # options, without the defaults.
     actual_options = optparse.Values()
@@ -5906,15 +5922,6 @@
           (sys.version.split(' ', 1)[0],), file=sys.stderr)
     return 2
 
-  # Reload settings.
-  global settings
-  settings = Settings()
-
-  if not metrics.DISABLE_METRICS_COLLECTION:
-    project_url = settings.GetViewVCUrl().strip('/+')
-    if project_url in metrics_utils.KNOWN_PROJECT_URLS:
-      metrics.collector.add('project_urls', [project_url])
-
   colorize_CMDstatus_doc()
   dispatcher = subcommand.CommandDispatcher(__name__)
   try: