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
This commit is contained in:
Steve Kowalik 2015-08-14 13:56:53 +10:00
parent 7f7b988b38
commit 5c5a9e18bd
6 changed files with 57 additions and 39 deletions

View File

@ -44,6 +44,7 @@ import github
import logging
import os
import jeepyb.log as l
import jeepyb.projects as p
import jeepyb.utils as u
@ -61,15 +62,13 @@ log = logging.getLogger("close_pull_requests")
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:

View File

@ -22,8 +22,9 @@ import json
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 @@ def main():
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')

View File

@ -64,6 +64,7 @@ import gerritlib.gerrit
import github
import jeepyb.gerritdb
import jeepyb.log as l
import jeepyb.utils as u
registry = u.ProjectsRegistry()
@ -528,28 +529,13 @@ def create_local_mirror(local_git_dir, project_git,
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)

View File

@ -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 @@ log = logging.getLogger('register_zanata_projects')
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")

View File

@ -31,6 +31,7 @@ import logging
import paramiko
import jeepyb.gerritdb
import jeepyb.log as l
BASE_DIR = '/home/gerrit2/review_site'
@ -152,20 +153,12 @@ def main():
# 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:

37
jeepyb/log.py Normal file
View File

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