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
This commit is contained in:
Monty Taylor 2015-09-22 16:03:30 -05:00
parent 2e350d034c
commit 03c1556a12
5 changed files with 39 additions and 8 deletions

View File

@ -51,6 +51,7 @@ import warnings
warnings.filterwarnings('ignore', 'Certificate has no `subjectAltName`') warnings.filterwarnings('ignore', 'Certificate has no `subjectAltName`')
from shade.exc import * # noqa from shade.exc import * # noqa
from shade import _log
from shade import meta from shade import meta
from shade import task_manager from shade import task_manager
from shade import _tasks from shade import _tasks
@ -106,7 +107,7 @@ def simple_logging(debug=False):
log_level = logging.DEBUG log_level = logging.DEBUG
else: else:
log_level = logging.INFO log_level = logging.INFO
log = logging.getLogger('shade') log = _log.setup_logging('shade')
log.addHandler(logging.StreamHandler()) log.addHandler(logging.StreamHandler())
log.setLevel(log_level) log.setLevel(log_level)
@ -220,7 +221,7 @@ class OpenStackCloud(object):
cache_arguments=None, cache_arguments=None,
manager=None, **kwargs): manager=None, **kwargs):
self.log = logging.getLogger('shade') self.log = _log.setup_logging('shade')
if not cloud_config: if not cloud_config:
config = os_client_config.OpenStackConfig() config = os_client_config.OpenStackConfig()
cloud_config = config.get_one_cloud(**kwargs) cloud_config = config.get_one_cloud(**kwargs)

28
shade/_log.py Normal file
View File

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

View File

@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging
import sys import sys
log = logging.getLogger(__name__) from shade import _log
log = _log.setup_logging(__name__)
class OpenStackCloudException(Exception): class OpenStackCloudException(Exception):

View File

@ -14,16 +14,16 @@
import bunch import bunch
import logging
import six import six
from shade import exc from shade import exc
from shade import _log
from shade import _utils from shade import _utils
NON_CALLABLES = (six.string_types, bool, dict, int, float, list, type(None)) 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): def find_nova_addresses(addresses, ext_tag=None, key_name=None, version=4):

View File

@ -17,13 +17,14 @@
# limitations under the License. # limitations under the License.
import abc import abc
import logging
import sys import sys
import threading import threading
import time import time
import six import six
from shade import _log
@six.add_metaclass(abc.ABCMeta) @six.add_metaclass(abc.ABCMeta)
class Task(object): class Task(object):
@ -78,7 +79,7 @@ class Task(object):
class TaskManager(object): class TaskManager(object):
log = logging.getLogger("shade.TaskManager") log = _log.setup_logging("shade.TaskManager")
def __init__(self, client, name): def __init__(self, client, name):
self.name = name self.name = name