From 0fd2c4508af4f4665945163dc22c34c3b2834345 Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Fri, 26 Sep 2014 13:11:58 -0400 Subject: [PATCH] extract logging setup to separate file The logging setup for the bot includes very useful and specific information on the kinds of things we want to get rid of when we enable logging around elastic search code. Extract it so it can be used in CLI tools. Change-Id: I2eff77cf37d481c6518c95642e308af196b61d50 --- elastic_recheck/bot.py | 37 ++----------------------- elastic_recheck/log.py | 62 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 34 deletions(-) create mode 100644 elastic_recheck/log.py diff --git a/elastic_recheck/bot.py b/elastic_recheck/bot.py index 5470fc1b..ca856cbd 100755 --- a/elastic_recheck/bot.py +++ b/elastic_recheck/bot.py @@ -40,8 +40,6 @@ openstack-qa: import argparse import ConfigParser import daemon -import logging -import logging.config import os import textwrap import threading @@ -51,6 +49,8 @@ import yaml import irc.bot from launchpadlib import launchpad +from elastic_recheck import log as logging + LPCACHEDIR = os.path.expanduser('~/.launchpadlib/cache') @@ -269,7 +269,7 @@ def get_options(): def _main(args, config): - setup_logging(config) + logging.setup_logging(config) fp = config.get('ircbot', 'channel_config') if fp: @@ -328,36 +328,5 @@ def main(): _main(args, config) -def setup_logging(config): - """Turn down dependent library log levels so they aren't noise.""" - FORMAT = '%(asctime)s %(levelname)-8s [%(name)-15s] %(message)s' - DATEFMT = '%Y-%m-%d %H:%M:%S' - # set 3rd party library logging levels to sanity points - loglevels = { - "irc.client": logging.INFO, - "gerrit.GerritWatcher": logging.INFO, - "paramiko.transport": logging.INFO, - "pyelasticsearch": logging.INFO, - "requests.packages.urllib3.connectionpool": logging.WARN, - "urllib3.connectionpool": logging.WARN - } - - if config.has_option('ircbot', 'log_config'): - log_config = config.get('ircbot', 'log_config') - fp = os.path.expanduser(log_config) - if not os.path.exists(fp): - raise Exception("Unable to read logging config file at %s" % fp) - logging.config.fileConfig(fp) - else: - logging.basicConfig( - level=logging.DEBUG, - format=FORMAT, - datefmt=DATEFMT - ) - for module in loglevels: - log = logging.getLogger(module) - log.setLevel(loglevels[module]) - - if __name__ == "__main__": main() diff --git a/elastic_recheck/log.py b/elastic_recheck/log.py new file mode 100644 index 00000000..1044c239 --- /dev/null +++ b/elastic_recheck/log.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python + +# Copyright 2013 OpenStack Foundation +# +# 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. + +"""Common logging for all ER efforts""" + +import logging +import os + +CONFIGURED = False + + +def setup_logging(config=None): + """Turn down dependent library log levels so they aren't noise.""" + global CONFIGURED + FORMAT = '%(asctime)s %(levelname)-8s [%(name)-15s] %(message)s' + DATEFMT = '%Y-%m-%d %H:%M:%S' + # set 3rd party library logging levels to sanity points + loglevels = { + "irc.client": logging.INFO, + "gerrit.GerritWatcher": logging.INFO, + "paramiko.transport": logging.INFO, + "pyelasticsearch": logging.INFO, + "requests.packages.urllib3.connectionpool": logging.WARN, + "urllib3.connectionpool": logging.WARN + } + + if config is not None and config.has_option('ircbot', 'log_config'): + log_config = config.get('ircbot', 'log_config') + fp = os.path.expanduser(log_config) + if not os.path.exists(fp): + raise Exception("Unable to read logging config file at %s" % fp) + logging.config.fileConfig(fp) + else: + logging.basicConfig( + level=logging.DEBUG, + format=FORMAT, + datefmt=DATEFMT + ) + for module in loglevels: + log = logging.getLogger(module) + log.setLevel(loglevels[module]) + CONFIGURED = True + + +def getLogger(name): + global CONFIGURED + if not CONFIGURED: + setup_logging() + return logging.getLogger(name)