From 03c1556a12aabfc21de60a9fac97aea7871485a3 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Tue, 22 Sep 2015 16:03:30 -0500 Subject: [PATCH] Add a NullHandler to all of our loggers As a library, it's important to make sure our logging is set up to not be confusing, but also to not to step on the toes of app developers. https://docs.python.org/3.1/library/logging.html#configuring-logging-for-a-library Change-Id: I312871e057ca0f64b9c5514bc94567cbdb34b4c6 --- shade/__init__.py | 5 +++-- shade/_log.py | 28 ++++++++++++++++++++++++++++ shade/exc.py | 5 +++-- shade/meta.py | 4 ++-- shade/task_manager.py | 5 +++-- 5 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 shade/_log.py diff --git a/shade/__init__.py b/shade/__init__.py index 69aecc266..12d9999d4 100644 --- a/shade/__init__.py +++ b/shade/__init__.py @@ -51,6 +51,7 @@ import warnings warnings.filterwarnings('ignore', 'Certificate has no `subjectAltName`') from shade.exc import * # noqa +from shade import _log from shade import meta from shade import task_manager from shade import _tasks @@ -106,7 +107,7 @@ def simple_logging(debug=False): log_level = logging.DEBUG else: log_level = logging.INFO - log = logging.getLogger('shade') + log = _log.setup_logging('shade') log.addHandler(logging.StreamHandler()) log.setLevel(log_level) @@ -220,7 +221,7 @@ class OpenStackCloud(object): cache_arguments=None, manager=None, **kwargs): - self.log = logging.getLogger('shade') + self.log = _log.setup_logging('shade') if not cloud_config: config = os_client_config.OpenStackConfig() cloud_config = config.get_one_cloud(**kwargs) diff --git a/shade/_log.py b/shade/_log.py new file mode 100644 index 000000000..ff2f2eac7 --- /dev/null +++ b/shade/_log.py @@ -0,0 +1,28 @@ +# Copyright (c) 2015 IBM Corp. +# +# 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 + + +class NullHandler(logging.Handler): + def emit(self, record): + pass + + +def setup_logging(name): + log = logging.getLogger(name) + if len(log.handlers) == 0: + h = NullHandler() + log.addHandler(h) + return log diff --git a/shade/exc.py b/shade/exc.py index 32d9e90ed..6d20992aa 100644 --- a/shade/exc.py +++ b/shade/exc.py @@ -12,10 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import logging import sys -log = logging.getLogger(__name__) +from shade import _log + +log = _log.setup_logging(__name__) class OpenStackCloudException(Exception): diff --git a/shade/meta.py b/shade/meta.py index 5585e9bf3..576a8c26a 100644 --- a/shade/meta.py +++ b/shade/meta.py @@ -14,16 +14,16 @@ import bunch -import logging import six from shade import exc +from shade import _log from shade import _utils NON_CALLABLES = (six.string_types, bool, dict, int, float, list, type(None)) -log = logging.getLogger(__name__) +log = _log.setup_logging(__name__) def find_nova_addresses(addresses, ext_tag=None, key_name=None, version=4): diff --git a/shade/task_manager.py b/shade/task_manager.py index d4aaf73e2..f2b9e5704 100644 --- a/shade/task_manager.py +++ b/shade/task_manager.py @@ -17,13 +17,14 @@ # limitations under the License. import abc -import logging import sys import threading import time import six +from shade import _log + @six.add_metaclass(abc.ABCMeta) class Task(object): @@ -78,7 +79,7 @@ class Task(object): class TaskManager(object): - log = logging.getLogger("shade.TaskManager") + log = _log.setup_logging("shade.TaskManager") def __init__(self, client, name): self.name = name