NFVBENCH-40 Add pylint to tox
[nfvbench.git] / nfvbench / log.py
1 # Copyright 2016 Cisco Systems, Inc.  All rights reserved.
2 #
3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #    not use this file except in compliance with the License. You may obtain
5 #    a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #    Unless required by applicable law or agreed to in writing, software
10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #    License for the specific language governing permissions and limitations
13 #    under the License.
14
15 import logging
16
17 _product_name = 'nfvbench'
18
19 def setup(mute_stdout=False):
20     # logging.basicConfig()
21     if mute_stdout:
22         handler = logging.NullHandler()
23     else:
24         formatter_str = '%(asctime)s %(levelname)s %(message)s'
25         handler = logging.StreamHandler()
26         handler.setFormatter(logging.Formatter(formatter_str))
27
28     # Add handler to logger
29     logger = logging.getLogger(_product_name)
30     logger.addHandler(handler)
31     # disable unnecessary information capture
32     logging.logThreads = 0
33     logging.logProcesses = 0
34     # to make sure each log record does not have a source file name attached
35     # pylint: disable=protected-access
36     logging._srcfile = None
37     # pylint: enable=protected-access
38
39 def add_file_logger(logfile):
40     if logfile:
41         file_formatter_str = '%(asctime)s %(levelname)s %(message)s'
42         file_handler = logging.FileHandler(logfile, mode='w')
43         file_handler.setFormatter(logging.Formatter(file_formatter_str))
44         logger = logging.getLogger(_product_name)
45         logger.addHandler(file_handler)
46
47 def set_level(debug=False):
48     log_level = logging.DEBUG if debug else logging.INFO
49     logger = logging.getLogger(_product_name)
50     logger.setLevel(log_level)
51
52 def getLogger():
53     logger = logging.getLogger(_product_name)
54     return logger
55
56 LOG = getLogger()