Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / common / log.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd 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
10 import logging
11
12
13 def _init_log_to_file(log_file, level, _format):
14     file_handler = logging.FileHandler(log_file)
15     file_handler.setLevel(level)
16     file_handler.setFormatter(logging.Formatter(_format))
17     return file_handler
18
19
20 def _init_log_to_console(level, _format):
21     console = logging.StreamHandler()
22     console.setLevel(level)
23     console.setFormatter(logging.Formatter(_format))
24     return console
25
26
27 def _init_log(log_file, level=logging.INFO, clevel=logging.INFO):
28     _format = '%(asctime)s <%(levelname)s> [%(funcName)s.%(lineno)d]: %(message)s'
29     # _format = '%(asctime)s [%(levelname)s] %(message)s'
30     _verbose = '%(levelname)s %(asctime)s [%(filename)s:%(lineno)d] %(funcName)s ### %(message)s'
31     _simple = '<%(levelname)s> [%(filename)s:%(lineno)d] ### %(message)s'
32     file_handler = _init_log_to_file(log_file, level, _verbose)
33     console = _init_log_to_console(clevel, _simple)
34     return file_handler, console
35
36
37 def setup_logging(
38         level=logging.INFO,
39         log_file="/var/log/esp_test.log",
40         clevel=logging.WARNING):
41     log = logging.getLogger()
42     log.setLevel(level)
43     file_handler, console = _init_log(log_file, level, clevel)
44     log.addHandler(file_handler)
45     log.addHandler(console)
46
47
48 if __name__ == "__main__":
49     setup_logging()
50     logger = logging.getLogger("common")
51     logger.info('this is a test.')
52     logger.warning('this is a test.')
53     logger.error('this is a test.')