1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
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 ##############################################################################
10 from __future__ import absolute_import
15 # this module must only import other modules that do
16 # not require loggers to be created, so this cannot
17 from yardstick.common import constants
18 from yardstick.common import exceptions
22 # do not use yardstick.common.utils.makedirs
23 # since yardstick.common.utils creates a logger
24 # and so it cannot be imported before this code
25 os.makedirs(constants.LOG_DIR)
27 if e.errno != errno.EEXIST:
30 LOG_FILE = os.path.join(constants.LOG_DIR, 'yardstick.log')
31 LOG_FORMATTER = '%(asctime)s [%(levelname)s] %(name)s %(filename)s:%(lineno)d %(message)s'
33 _LOG_FORMATTER = logging.Formatter(LOG_FORMATTER)
34 _LOG_STREAM_HDLR = logging.StreamHandler()
35 _LOG_FILE_HDLR = logging.FileHandler(LOG_FILE)
37 LOG = logging.getLogger(__name__)
42 LOG.setLevel(logging.DEBUG)
44 _LOG_STREAM_HDLR.setFormatter(_LOG_FORMATTER)
45 if os.environ.get('CI_DEBUG', '').lower() in {'1', 'y', "yes", "true"}:
46 _LOG_STREAM_HDLR.setLevel(logging.DEBUG)
48 _LOG_STREAM_HDLR.setLevel(logging.INFO)
50 # don't append to log file, clobber
51 _LOG_FILE_HDLR.setFormatter(_LOG_FORMATTER)
52 _LOG_FILE_HDLR.setLevel(logging.DEBUG)
54 del logging.root.handlers[:]
55 logging.root.addHandler(_LOG_STREAM_HDLR)
56 logging.root.addHandler(_LOG_FILE_HDLR)
57 logging.debug("logging.root.handlers = %s", logging.root.handlers)