Misc logging cleanup
Project: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/commit/71fa4c14 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/tree/71fa4c14 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/diff/71fa4c14 Branch: refs/heads/master Commit: 71fa4c145fdf6a06e895033dd16b888ea711019c Parents: 673d4df Author: Thomas Jackson <[email protected]> Authored: Mon Jan 5 19:02:49 2015 -0800 Committer: Thomas Jackson <[email protected]> Committed: Mon Jan 5 19:02:49 2015 -0800 ---------------------------------------------------------------------- tsqa/__init__.py | 2 ++ tsqa/environment.py | 12 ++++++--- tsqa/test_cases.py | 5 ++-- tsqa/utils.py | 70 ++++-------------------------------------------- 4 files changed, 18 insertions(+), 71 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/71fa4c14/tsqa/__init__.py ---------------------------------------------------------------------- diff --git a/tsqa/__init__.py b/tsqa/__init__.py index e69de29..92492c8 100644 --- a/tsqa/__init__.py +++ b/tsqa/__init__.py @@ -0,0 +1,2 @@ +# initialize logger +import tsqa.log http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/71fa4c14/tsqa/environment.py ---------------------------------------------------------------------- diff --git a/tsqa/environment.py b/tsqa/environment.py index 03c1396..7574930 100644 --- a/tsqa/environment.py +++ b/tsqa/environment.py @@ -4,11 +4,11 @@ import os import copy import shutil import tsqa.utils -import logging import sys import tsqa.configs import tsqa.utils +import logging class EnvironmentFactory(object): @@ -29,7 +29,7 @@ class EnvironmentFactory(object): # TODO: ensure this directory exists? (and is git?) self.source_dir = source_dir - self.log = tsqa.utils.get_logger() + self.log = logging.getLogger(__name__) self.env_cache_dir = env_cache_dir # base directory for environment caching if default_configure is not None: @@ -187,7 +187,7 @@ class Layout: def __init__(self, prefix): self.prefix = prefix - self.log = tsqa.utils.get_logger() + self.log = logging.getLogger(__name__) def __getattr__(self, name): # Raise an error for suffixes we don't know about @@ -252,7 +252,7 @@ class Environment: """ Initialize a new Environment. """ - self.log = tsqa.utils.get_logger() + self.log = logging.getLogger(__name__) self.cop = None self.hostports = [] if layout: @@ -329,6 +329,8 @@ class Environment: self.layout = Layout(None) def start(self): + if self.running(): # if its already running, don't start another one + raise Exception('traffic cop already started') self.log.debug("Starting traffic cop") assert(os.path.isfile(os.path.join(self.layout.sysconfdir, 'records.config'))) self.__exec_cop() @@ -343,6 +345,8 @@ class Environment: self.cop.terminate() # TODO: remove?? or wait... def running(self): + if self.cop is None: + return False self.cop.poll() return self.cop is not None and self.cop.returncode is not None # its running if it hasn't died http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/71fa4c14/tsqa/test_cases.py ---------------------------------------------------------------------- diff --git a/tsqa/test_cases.py b/tsqa/test_cases.py index cb1ef55..87885b0 100644 --- a/tsqa/test_cases.py +++ b/tsqa/test_cases.py @@ -1,6 +1,7 @@ ''' Some base test cases that do environment handling for you ''' +import logging import tsqa.endpoint import tsqa.environment @@ -25,7 +26,7 @@ class EnvironmentCase(unittest.TestCase): super(EnvironmentCase, cls).setUpClass() # get a logger - cls.log = tsqa.utils.get_logger() + cls.log = logging.getLogger(__name__) # get an environment cls.environment = cls.getEnv() @@ -93,7 +94,7 @@ class DynamicHTTPEndpointCase(unittest.TestCase): @classmethod def setUpClass(cls, port=0): # get a logger - cls.log = tsqa.utils.get_logger() + cls.log = logging.getLogger(__name__) cls.http_endpoint = tsqa.endpoint.DynamicHTTPEndpoint(port=port) cls.http_endpoint.start() http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/71fa4c14/tsqa/utils.py ---------------------------------------------------------------------- diff --git a/tsqa/utils.py b/tsqa/utils.py index d522f66..4f63a2a 100644 --- a/tsqa/utils.py +++ b/tsqa/utils.py @@ -4,68 +4,11 @@ import json import sys import subprocess import socket -import logging import time -tsqa_logger = None -tsqa_log_level = logging.INFO -tsqa_log_levels = { - 'CRITICAL': logging.CRITICAL, - 'ERROR': logging.ERROR, - 'WARN': logging.WARNING, - 'WARNING': logging.WARNING, - 'INFO': logging.INFO, - 'DEBUG': logging.DEBUG, - 'NOTSET': logging.NOTSET -} - -def set_log_level(log_level): - ''' - Set the global log level (override with env var TSQA_LOG_LEVEL). Must be called - before first get_logger() - ''' - - global tsqa_log_level - tsqa_log_level = log_level - -def get_log_level(): - ''' - Get the global log level (override with env var TSQA_LOG_LEVEL). - ''' - - if os.environ.has_key('TSQA_LOG_LEVEL'): - log_level = os.environ['TSQA_LOG_LEVEL'].upper() - - if tsqa_log_levels.has_key(log_level): - return tsqa_log_levels[log_level] - - return tsqa_log_level - -def set_logger(logger): - ''' - Set/replace the global logger - ''' - - global tsqa_logger - tsqa_logger = logger - -def get_logger(): - ''' - Get the global logger - ''' - - global tsqa_logger - - if tsqa_logger: - return tsqa_logger - - tsqa_logger = logging.getLogger() - tsqa_logger.setLevel(get_log_level()) - handler = logging.StreamHandler() - handler.setFormatter(logging.Formatter("%(levelname)s %(asctime)-15s - %(message)s")) - tsqa_logger.addHandler(handler) +import logging - return tsqa_logger +log = logging.getLogger(__name__) def poll_interfaces(hostports, **kwargs): ''' Block until we can successfully connect to all ports or timeout @@ -89,8 +32,7 @@ def poll_interfaces(hostports, **kwargs): hostname = hostport[0] port = hostport[1] - if get_logger().isEnabledFor(logging.DEBUG): - get_logger().debug("Checking interface '%s:%d'", hostname, port) + log.debug("Checking interface '%s:%d'", hostname, port) # This supports IPv6 @@ -99,8 +41,7 @@ def poll_interfaces(hostports, **kwargs): s.close() hostports.remove(hostport) - if get_logger().isEnabledFor(logging.DEBUG): - get_logger().debug("Interface '%s:%d' is up", hostname, port) + log.debug("Interface '%s:%d' is up", hostname, port) except: pass @@ -113,8 +54,7 @@ def poll_interfaces(hostports, **kwargs): raise Exception("Timeout waiting for interfaces: {0}".format( reduce(lambda x, y: str(x) + ',' + str(y), hostports))) - if get_logger().isEnabledFor(logging.DEBUG): - get_logger().debug("All interfaces are up") + log.debug("All interfaces are up") # TODO: test def import_unittest():
