Merge "Fix Malformed Table in User Guide docs" into stable/euphrates
[yardstick.git] / yardstick / __init__.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 from __future__ import absolute_import
11 import logging
12 import os
13 import errno
14
15 # this module must only import other modules that do
16 # not require loggers to be created, so this cannot
17 # include yardstick.common.utils
18 from yardstick.common import constants
19
20 try:
21     # do not use yardstick.common.utils.makedirs
22     # since yardstick.common.utils creates a logger
23     # and so it cannot be imported before this code
24     os.makedirs(constants.LOG_DIR)
25 except OSError as e:
26     if e.errno != errno.EEXIST:
27         raise
28
29 LOG_FILE = os.path.join(constants.LOG_DIR, 'yardstick.log')
30 LOG_FORMATTER = '%(asctime)s [%(levelname)s] %(name)s %(filename)s:%(lineno)d %(message)s'
31
32 _LOG_FORMATTER = logging.Formatter(LOG_FORMATTER)
33 _LOG_STREAM_HDLR = logging.StreamHandler()
34 _LOG_FILE_HDLR = logging.FileHandler(LOG_FILE)
35
36 LOG = logging.getLogger(__name__)
37
38
39 def _init_logging():
40
41     LOG.setLevel(logging.DEBUG)
42
43     _LOG_STREAM_HDLR.setFormatter(_LOG_FORMATTER)
44     if os.environ.get('CI_DEBUG', '').lower() in {'1', 'y', "yes", "true"}:
45         _LOG_STREAM_HDLR.setLevel(logging.DEBUG)
46     else:
47         _LOG_STREAM_HDLR.setLevel(logging.INFO)
48
49     # don't append to log file, clobber
50     _LOG_FILE_HDLR.setFormatter(_LOG_FORMATTER)
51     _LOG_FILE_HDLR.setLevel(logging.DEBUG)
52
53     del logging.root.handlers[:]
54     logging.root.addHandler(_LOG_STREAM_HDLR)
55     logging.root.addHandler(_LOG_FILE_HDLR)
56     logging.debug("logging.root.handlers = %s", logging.root.handlers)