bugfix: copy hosts file
[bottlenecks.git] / utils / logger.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 # Logging levels:
11 #  Level     Numeric value
12 #  CRITICAL  50
13 #  ERROR     40
14 #  WARNING   30
15 #  INFO      20
16 #  DEBUG     10
17 #  NOTSET    0
18
19 import logging
20 import os
21
22 from utils.parser import Parser as conf_parser
23 conf_parser.config_init()
24
25
26 class Logger:
27     def __init__(self, logger_name):
28
29         # if user set --debug as a cli parameter
30         # we will set this variable "Debug" to output debug info.
31         DEBUG = os.getenv('DEBUG')
32
33         self.logger = logging.getLogger(logger_name)
34         self.logger.propagate = 0
35         self.logger.setLevel(logging.DEBUG)
36
37         ch = logging.StreamHandler()
38         log_formatter = ('%(asctime)s '
39                          '%(filename)s:%(lineno)d '
40                          '%(levelname)s %(message)s')
41
42         formatter = logging.Formatter(log_formatter)
43
44         ch.setFormatter(formatter)
45         if DEBUG is not None and DEBUG.lower() == "true":
46             ch.setLevel(logging.DEBUG)
47         else:
48             ch.setLevel(logging.INFO)
49         self.logger.addHandler(ch)
50
51         result_path = conf_parser.bottlenecks_config["log_dir"]
52         if not os.path.exists(result_path):
53             os.makedirs(result_path)
54         result_file = os.path.join(result_path, 'bottlenecks.log')
55         hdlr = logging.FileHandler(result_file)
56
57         hdlr.setFormatter(formatter)
58         hdlr.setLevel(logging.DEBUG)
59         self.logger.addHandler(hdlr)
60
61     def getLogger(self):
62         return self.logger