summaryrefslogtreecommitdiffstats
path: root/functest/utils/functest_logger.py
blob: ba52829f77837b63c0ca80959ec4116b6175a073 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
#
# jose.lausuch@ericsson.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Logging levels:
#  Level     Numeric value
#  CRITICAL  50
#  ERROR     40
#  WARNING   30
#  INFO      20
#  DEBUG     10
#  NOTSET    0
#
# Usage:
#  import functest_logger as fl
#  logger = fl.Logger("script_name").getLogger()
#  logger.info("message to be shown with - INFO - ")
#  logger.debug("message to be shown with - DEBUG -")
import logging
import logging.config
import os

import json

from functest.utils.constants import CONST

ignore = ["paramiko",
          "stevedore.extension",
          "keystoneauth.session",
          "keystoneauth.identity.v3.base",
          "novaclient.v2.client",
          "neutronclient.v2_0.client",
          "glanceclient.common.http",
          "cinderclient.v2.client",
          "cinderclient.client"]


class Logger(object):

    instance = None

    def __new__(cls, logger_name):
        if cls.instance is None:
            cls.instance = object.__new__(cls)
        return cls.instance

    def __init__(self, logger_name):
        self.setup_logging()
        self.logger = logging.getLogger(logger_name)
        for module_name in ignore:
            logging.getLogger(module_name).setLevel(logging.WARNING)

    def getLogger(self):
        return self.logger

    def is_debug(self):
        if CONST.CI_DEBUG and CONST.CI_DEBUG.lower() == "true":
            return True
        return False

    def setup_logging(self, default_path=CONST.dir_functest_logging_cfg,
                      default_level=logging.INFO,
                      env_key='LOG_CFG'):
        path = default_path
        value = os.getenv(env_key, None)
        if value:
            path = value
        if os.path.exists(path):
            with open(path, 'rt') as f:
                config = json.load(f)
                if (config['handlers'] and
                        config['handlers']['console']):
                    stream_level = logging.INFO
                    if self.is_debug():
                        stream_level = logging.DEBUG
                    config['handlers']['console']['level'] = stream_level
                logging.config.dictConfig(config)
        else:
            logging.basicConfig(level=default_level)