Refactor logging arguments into a common module

The command line utilities provided only haphazardly set up logging
using command line arguments. Refactor it into a common module, and
make use of it in all scripts that use logging.

This changes expire-old-reviews.py to not hardcode the log file, but
that is fine, it has no scripts calling it.

Change-Id: Ibc85f8e3b47f9c7898ad4334511b44e91ecbd736
diff --git a/jeepyb/cmd/close_pull_requests.py b/jeepyb/cmd/close_pull_requests.py
index b49bac3..e380f2f 100644
--- a/jeepyb/cmd/close_pull_requests.py
+++ b/jeepyb/cmd/close_pull_requests.py
@@ -44,6 +44,7 @@
 import logging
 import os
 
+import jeepyb.log as l
 import jeepyb.projects as p
 import jeepyb.utils as u
 
@@ -61,15 +62,13 @@
 
 def main():
 
-    logging.basicConfig(level=logging.ERROR,
-                        format='%(asctime)-6s: %(name)s - %(levelname)s'
-                               ' - %(message)s')
-
     parser = argparse.ArgumentParser()
+    l.setup_logging_arguments(parser)
     parser.add_argument('--message-file', dest='message_file', default=None,
                         help='The close pull request message')
 
     args = parser.parse_args()
+    l.configure_logging(args)
 
     if args.message_file:
         try:
diff --git a/jeepyb/cmd/expire_old_reviews.py b/jeepyb/cmd/expire_old_reviews.py
index ef67e3a..9ec1064 100644
--- a/jeepyb/cmd/expire_old_reviews.py
+++ b/jeepyb/cmd/expire_old_reviews.py
@@ -22,8 +22,9 @@
 import logging
 import paramiko
 
+import jeepyb.log as l
+
 logger = logging.getLogger('expire_reviews')
-logger.setLevel(logging.INFO)
 
 
 def expire_patch_set(ssh, patch_id, patch_subject):
@@ -49,16 +50,14 @@
     parser.add_argument('ssh_key', help='The gerrit admin SSH key file')
     parser.add_argument('--age', dest='age', default='1w',
                         help='The minimum age of a review to expire')
+    l.setup_logging_arguments(parser)
     options = parser.parse_args()
+    l.configure_logging(options)
 
     GERRIT_USER = options.user
     GERRIT_SSH_KEY = options.ssh_key
     EXPIRY_AGE = options.age
 
-    logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s'
-                               ' - %(message)s',
-                        filename='/var/log/gerrit/expire_reviews.log')
-
     logger.info('Starting expire reviews')
     logger.info('Connecting to Gerrit')
 
diff --git a/jeepyb/cmd/manage_projects.py b/jeepyb/cmd/manage_projects.py
index b248e7d..3bca041 100644
--- a/jeepyb/cmd/manage_projects.py
+++ b/jeepyb/cmd/manage_projects.py
@@ -64,6 +64,7 @@
 import github
 
 import jeepyb.gerritdb
+import jeepyb.log as l
 import jeepyb.utils as u
 
 registry = u.ProjectsRegistry()
@@ -528,28 +529,13 @@
 
 def main():
     parser = argparse.ArgumentParser(description='Manage projects')
-    parser.add_argument('-v', dest='verbose', action='store_true',
-                        help='verbose output')
-    parser.add_argument('-d', dest='debug', action='store_true',
-                        help='debug output')
+    l.setup_logging_arguments(parser)
     parser.add_argument('--nocleanup', action='store_true',
                         help='do not remove temp directories')
     parser.add_argument('projects', metavar='project', nargs='*',
                         help='name of project(s) to process')
     args = parser.parse_args()
-
-    if args.debug:
-        logging.basicConfig(level=logging.DEBUG,
-                            format='%(asctime)-6s: %(name)s - %(levelname)s'
-                                   ' - %(message)s')
-    elif args.verbose:
-        logging.basicConfig(level=logging.INFO,
-                            format='%(asctime)-6s: %(name)s - %(levelname)s'
-                                   ' - %(message)s')
-    else:
-        logging.basicConfig(level=logging.ERROR,
-                            format='%(asctime)-6s: %(name)s - %(levelname)s'
-                                   ' - %(message)s')
+    l.configure_logging(args)
 
     default_has_github = registry.get_defaults('has-github', True)
 
diff --git a/jeepyb/cmd/register_zanata_projects.py b/jeepyb/cmd/register_zanata_projects.py
index 355662a..6d64625 100644
--- a/jeepyb/cmd/register_zanata_projects.py
+++ b/jeepyb/cmd/register_zanata_projects.py
@@ -14,9 +14,11 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import argparse
 import logging
 import os
 
+import jeepyb.log as l
 import jeepyb.projects as p
 import jeepyb.translations as t
 import jeepyb.utils as u
@@ -30,9 +32,11 @@
 
 
 def main():
-    logging.basicConfig(level=logging.ERROR,
-                        format='%(asctime)-6s: %(name)s - %(levelname)s'
-                               ' - %(message)s')
+    parser = argparse.ArgumentParser(description='Register projects in Zanata')
+    l.setup_logging_arguments(parser)
+    args = parser.parse_args()
+    l.configure_logging(args)
+
     registry = u.ProjectsRegistry(PROJECTS_YAML)
     rest_service = t.ZanataRestService(ZANATA_URL, ZANATA_USER, ZANATA_KEY)
     log.info("Registering projects in Zanata")
diff --git a/jeepyb/cmd/welcome_message.py b/jeepyb/cmd/welcome_message.py
index 4c78f52..c8ed843 100644
--- a/jeepyb/cmd/welcome_message.py
+++ b/jeepyb/cmd/welcome_message.py
@@ -31,6 +31,7 @@
 import paramiko
 
 import jeepyb.gerritdb
+import jeepyb.log as l
 
 BASE_DIR = '/home/gerrit2/review_site'
 
@@ -152,20 +153,12 @@
     # Don't actually post the message
     parser.add_argument('--dryrun', dest='dryrun', action='store_true')
     parser.add_argument('--no-dryrun', dest='dryrun', action='store_false')
-    parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
-                        help='verbose output')
     parser.set_defaults(dryrun=False)
+    l.setup_logging_arguments(parser)
 
     args = parser.parse_args()
 
-    if args.verbose:
-        logging.basicConfig(level=logging.DEBUG,
-                            format='%(asctime)-6s: %(name)s - %(levelname)s'
-                                   ' - %(message)s')
-    else:
-        logging.basicConfig(level=logging.ERROR,
-                            format='%(asctime)-6s: %(name)s - %(levelname)s'
-                                   ' - %(message)s')
+    l.configure_logging(args)
 
     # they're a first-timer, post the message on 1st patchset
     if is_newbie(args.uploader) and args.patchset == '1' and not args.dryrun:
diff --git a/jeepyb/log.py b/jeepyb/log.py
new file mode 100644
index 0000000..dbd6c65
--- /dev/null
+++ b/jeepyb/log.py
@@ -0,0 +1,37 @@
+# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import logging
+
+
+def setup_logging_arguments(parser):
+    """Sets up logging arguments, adds -d, -l and -v to the given parser."""
+    parser.add_argument('-v', dest='verbose', action='store_true',
+                        help='verbose output')
+    parser.add_argument('-d', dest='debug', action='store_true',
+                        help='debug output')
+    parser.add_argument('-l', dest='logfile', help='log file to use')
+
+
+def configure_logging(args):
+    if args.debug:
+        level = logging.DEBUG
+    elif args.verbose:
+        level = logging.INFO
+    else:
+        level = logging.ERROR
+    logging.basicConfig(level=level, filename=args.logfile,
+                        format='%(asctime)-6s: %(name)s - %(levelname)s'
+                               ' - %(message)s')