add logger common process for releng scripts
[releng.git] / utils / test / scripts / logger_utils.py
1 #!/usr/bin/env python
2 #
3 # feng.xiaowei@zte.com.cn
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
24 import logging
25 import os
26
27
28 class Logger(object):
29     file_path = '/var/log'
30     formatter = logging.Formatter('%(asctime)s - %(name)s - '
31                                   '%(levelname)s - %(message)s')
32
33     def __init__(self, logger_name):
34
35         IF_DEBUG = os.getenv('IF_DEBUG')
36
37         self.logger_name = logger_name
38         self.logger = logging.getLogger(logger_name)
39         self.logger.propagate = 0
40         self.logger.setLevel(logging.DEBUG)
41
42         ch = logging.StreamHandler()
43         ch.setFormatter(self.formatter)
44         if IF_DEBUG is not None and IF_DEBUG.lower() == "true":
45             ch.setLevel(logging.DEBUG)
46         else:
47             ch.setLevel(logging.INFO)
48         self.logger.addHandler(ch)
49
50         hdlr = logging.FileHandler('%s/%s.log' % (self.file_path, logger_name))
51         hdlr.setFormatter(self.formatter)
52         hdlr.setLevel(logging.DEBUG)
53         self.logger.addHandler(hdlr)
54
55     @property
56     def get(self):
57         return self.logger
58
59
60 class KibanaDashboardLogger(Logger):
61     file_path = '/var/log/kibana_dashboard'
62
63     def __init__(self, logger_name):
64         super(KibanaDashboardLogger, self).__init__(logger_name)
65