Ignore certain modules for logging
[functest.git] / functest / utils / functest_logger.py
1 #!/usr/bin/env python
2 #
3 # jose.lausuch@ericsson.com
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 # Logging levels:
10 #  Level     Numeric value
11 #  CRITICAL  50
12 #  ERROR     40
13 #  WARNING   30
14 #  INFO      20
15 #  DEBUG     10
16 #  NOTSET    0
17 #
18 # Usage:
19 #  import functest_logger as fl
20 #  logger = fl.Logger("script_name").getLogger()
21 #  logger.info("message to be shown with - INFO - ")
22 #  logger.debug("message to be shown with - DEBUG -")
23 import logging
24 import logging.config
25 import os
26
27 import json
28
29 from functest.utils.constants import CONST
30
31 ignore = ["paramiko",
32           "stevedore.extension",
33           "keystoneauth.session",
34           "keystoneauth.identity.v3.base",
35           "novaclient.v2.client",
36           "neutronclient.v2_0.client",
37           "glanceclient.common.http",
38           "cinderclient.v2.client",
39           "cinderclient.client"]
40
41
42 class Logger(object):
43
44     def __init__(self, logger_name):
45         self.setup_logging()
46         self.logger = logging.getLogger(logger_name)
47         for module_name in ignore:
48             logging.getLogger(module_name).setLevel(logging.WARNING)
49
50     def getLogger(self):
51         return self.logger
52
53     def is_debug(self):
54         if CONST.CI_DEBUG and CONST.CI_DEBUG.lower() == "true":
55             return True
56         return False
57
58     def setup_logging(self, default_path=CONST.dir_functest_logging_cfg,
59                       default_level=logging.INFO,
60                       env_key='LOG_CFG'):
61         path = default_path
62         value = os.getenv(env_key, None)
63         if value:
64             path = value
65         if os.path.exists(path):
66             with open(path, 'rt') as f:
67                 config = json.load(f)
68                 if (config['handlers'] and
69                         config['handlers']['console']):
70                     stream_level = logging.INFO
71                     if self.is_debug():
72                         stream_level = logging.DEBUG
73                     config['handlers']['console']['level'] = stream_level
74                 logging.config.dictConfig(config)
75         else:
76             logging.basicConfig(level=default_level)