Files
fuel-qa/core/helpers/log_helpers.py
Alexey Stepanov b7ca0ff8b6 Move QuietLogger and logwrap to core
1. Move QuietLogger and logwrap to core
2. Fix QuietLogger (if log level was upper, than in param,
  QuietLogger ebabled additional logging instead of pass)
3. Cover by unit-tests
4. Added missed license text
5. add *.log to gitignore

Change-Id: If842791000c76852fe1dec1e66edcc081c278b2c
Blueprint: fuel-qa-join-helpers
2016-09-02 11:20:54 +03:00

71 lines
2.2 KiB
Python

# Copyright 2016 Mirantis, Inc.
#
# 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.
from __future__ import unicode_literals
import functools
import logging
from core import logger
def logwrap(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
logger.debug(
"Calling: {} with args: {} {}".format(
func.__name__, args, kwargs
)
)
try:
result = func(*args, **kwargs)
logger.debug(
"Done: {} with result: {}".format(func.__name__, result))
except BaseException as e:
logger.exception(
'{func} raised: {exc!r}\n'.format(func=func.__name__, exc=e))
raise
return result
return wrapped
class QuietLogger(object):
"""Reduce logging level while context is executed."""
def __init__(self, upper_log_level=None):
"""Reduce logging level while context is executed.
:param upper_log_level: log level to ignore
:type upper_log_level: int
"""
self.log_level = upper_log_level
self.level = None
def __enter__(self):
console = logging.StreamHandler()
self.level = console.level
if self.log_level is None:
self.log_level = self.level
elif self.log_level < self.level:
logger.debug(
'QuietLogger requested lower level, than is already set. '
'Not changing level')
return
console.setLevel(self.log_level + 1)
def __exit__(self, exc_type, exc_value, exc_tb):
logging.StreamHandler().setLevel(self.level)
__all__ = ['logwrap', 'QuietLogger', 'logger']