fix package path and move files under doctor_tests
[doctor.git] / doctor_tests / logger.py
1 ##############################################################################
2 # Copyright (c) 2016 ZTE Corporation and others.
3 #
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 # Usage:
10 #  import doctor_logger
11 #  logger = doctor_logger.Logger("script_name").getLogger()
12 #  logger.info("message to be shown with - INFO - ")
13 #  logger.debug("message to be shown with - DEBUG -")
14
15 import logging
16 import os
17
18
19 class Logger(object):
20     def __init__(self, logger_name):
21
22         CI_DEBUG = os.getenv('CI_DEBUG')
23
24         self.logger = logging.getLogger(logger_name)
25         self.logger.propagate = 0
26         self.logger.setLevel(logging.DEBUG)
27
28         formatter = logging.Formatter('%(asctime)s %(filename)s %(lineno)d '
29                                       '%(levelname)-6s %(message)s')
30
31         ch = logging.StreamHandler()
32         ch.setFormatter(formatter)
33         if CI_DEBUG is not None and CI_DEBUG.lower() == "true":
34             ch.setLevel(logging.DEBUG)
35         else:
36             ch.setLevel(logging.INFO)
37         self.logger.addHandler(ch)
38
39         filename = '%s.log' % logger_name
40         file_handler = logging.FileHandler(filename, mode='w')
41         file_handler.setFormatter(formatter)
42         file_handler.setLevel(logging.DEBUG)
43         self.logger.addHandler(file_handler)
44
45     def getLogger(self):
46         return self.logger