Clean up logger usage

* Use module-specific logger in integration helper.
  Previously the root logger (returned by getLogger without parameter)
  was used for module-specifc log messages.
  It is better to use module-specific logger for such messages
  and we can easily identify where log messages come from.
* In case of requiring access to the root logger,
  get the root logger explicitly.
* Use 'LOG' for logger name. Almost all modules in horizon use
  'LOG' for logger name, but some do not.
  It is better to follow the convention.

* Use LOG.warning instead of LOG.warn.
  LOG.warning is recommended way in Python3.

Change-Id: Ib9de7ee08a235de9820b95910d8f54724a1f2b91
This commit is contained in:
Akihiro Motoki 2017-03-22 22:18:11 +00:00
parent 5d5dcf3e9e
commit f22669058a
3 changed files with 23 additions and 17 deletions

View File

@ -23,7 +23,7 @@ from oslo_serialization import jsonutils
from horizon import exceptions from horizon import exceptions
log = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
class AjaxError(Exception): class AjaxError(Exception):
@ -136,11 +136,11 @@ def ajax(authenticated=True, data_required=False,
http_status = getattr(e, attr) http_status = getattr(e, attr)
break break
else: else:
log.exception('HTTP exception with no status/code') LOG.exception('HTTP exception with no status/code')
return JSONResponse(str(e), 500) return JSONResponse(str(e), 500)
return JSONResponse(str(e), http_status) return JSONResponse(str(e), http_status)
except Exception as e: except Exception as e:
log.exception('error invoking apiclient') LOG.exception('error invoking apiclient')
return JSONResponse(str(e), 500) return JSONResponse(str(e), 500)
return _wrapped return _wrapped

View File

@ -36,8 +36,13 @@ from openstack_dashboard.test.integration_tests.regions import messages
from openstack_dashboard.test.integration_tests.video_recorder import \ from openstack_dashboard.test.integration_tests.video_recorder import \
VideoRecorder VideoRecorder
LOGGER = logging.getLogger() # Set logging level to DEBUG for all logger here
LOGGER.setLevel(logging.DEBUG) # so that lower level messages are output even before starting tests.
ROOT_LOGGER = logging.getLogger()
ROOT_LOGGER.setLevel(logging.DEBUG)
LOG = logging.getLogger(__name__)
IS_SELENIUM_HEADLESS = os.environ.get('SELENIUM_HEADLESS', False) IS_SELENIUM_HEADLESS = os.environ.get('SELENIUM_HEADLESS', False)
ROOT_PATH = os.path.dirname(os.path.abspath(config.__file__)) ROOT_PATH = os.path.dirname(os.path.abspath(config.__file__))
@ -46,7 +51,7 @@ if not subprocess.call('which xdpyinfo > /dev/null 2>&1', shell=True):
shell=True).split()[1].split('x') shell=True).split()[1].split('x')
else: else:
SCREEN_SIZE = (None, None) SCREEN_SIZE = (None, None)
LOGGER.info("X11 isn't installed. Should use xvfb to run tests.") LOG.info("X11 isn't installed. Should use xvfb to run tests.")
def gen_random_resource_name(resource="", timestamp=True): def gen_random_resource_name(resource="", timestamp=True):
@ -198,14 +203,15 @@ class BaseTestCase(testtools.TestCase):
"""Configure log to capture test logs include selenium logs in order """Configure log to capture test logs include selenium logs in order
to attach them if test will be broken. to attach them if test will be broken.
""" """
LOGGER.handlers[:] = [] # clear other handlers to set target handler # clear other handlers to set target handler
ROOT_LOGGER.handlers[:] = []
self._log_buffer = StringIO() self._log_buffer = StringIO()
stream_handler = logging.StreamHandler(stream=self._log_buffer) stream_handler = logging.StreamHandler(stream=self._log_buffer)
stream_handler.setLevel(logging.DEBUG) stream_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter( formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s') '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
stream_handler.setFormatter(formatter) stream_handler.setFormatter(formatter)
LOGGER.addHandler(stream_handler) ROOT_LOGGER.addHandler(stream_handler)
@property @property
def _test_report_dir(self): def _test_report_dir(self):
@ -230,8 +236,8 @@ class BaseTestCase(testtools.TestCase):
def _attach_video(self, exc_info=None): def _attach_video(self, exc_info=None):
with self.log_exception("Attach video"): with self.log_exception("Attach video"):
if not os.path.isfile(self.video_recorder.file_path): if not os.path.isfile(self.video_recorder.file_path):
LOGGER.warn("Can't find video {!r}".format( LOG.warning("Can't find video %s",
self.video_recorder.file_path)) self.video_recorder.file_path)
return return
shutil.move(self.video_recorder.file_path, shutil.move(self.video_recorder.file_path,

View File

@ -18,7 +18,7 @@ from tempfile import mktemp
from threading import Thread from threading import Thread
import time import time
LOGGER = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
class VideoRecorder(object): class VideoRecorder(object):
@ -34,21 +34,21 @@ class VideoRecorder(object):
def start(self): def start(self):
if self.is_launched: if self.is_launched:
LOGGER.warn('Video recording is running already') LOG.warning('Video recording is running already')
return return
if not os.environ.get('AVCONV_INSTALLED', False): if not os.environ.get('AVCONV_INSTALLED', False):
LOGGER.error("avconv isn't installed. Video recording is skipped") LOG.error("avconv isn't installed. Video recording is skipped")
return return
fnull = open(os.devnull, 'w') fnull = open(os.devnull, 'w')
LOGGER.info('Record video via {!r}'.format(' '.join(self._cmd))) LOG.info('Record video via %s', ' '.join(self._cmd))
self._popen = subprocess.Popen(self._cmd, stdout=fnull, stderr=fnull) self._popen = subprocess.Popen(self._cmd, stdout=fnull, stderr=fnull)
self.is_launched = True self.is_launched = True
def stop(self): def stop(self):
if not self.is_launched: if not self.is_launched:
LOGGER.warn('Video recording is stopped already') LOG.warning('Video recording is stopped already')
return return
self._popen.send_signal(signal.SIGINT) self._popen.send_signal(signal.SIGINT)
@ -72,11 +72,11 @@ class VideoRecorder(object):
def clear(self): def clear(self):
if self.is_launched: if self.is_launched:
LOGGER.error("Video recording is running still") LOG.error("Video recording is running still")
return return
if not os.path.isfile(self.file_path): if not os.path.isfile(self.file_path):
LOGGER.warn("{!r} is absent already".format(self.file_path)) LOG.warning("%s is absent already", self.file_path)
return return
os.remove(self.file_path) os.remove(self.file_path)