2b9e047b55c7801e272cad071daf3ad4107f18c0
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / rapid_log.py
1 #!/usr/bin/python
2
3 ##
4 ## Copyright (c) 2020 Intel Corporation
5 ##
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
9 ##
10 ##     http://www.apache.org/licenses/LICENSE-2.0
11 ##
12 ## Unless required by applicable law or agreed to in writing, software
13 ## distributed under the License is distributed on an "AS IS" BASIS,
14 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ## See the License for the specific language governing permissions and
16 ## limitations under the License.
17 ##
18
19 import logging
20 from logging.handlers import RotatingFileHandler
21 from logging import handlers
22 import os
23 import sys
24 import time
25
26 class bcolors(object):
27     HEADER = '\033[95m'
28     OKBLUE = '\033[94m'
29     OKGREEN = '\033[92m'
30     WARNING = '\033[93m'
31     FAIL = '\033[91m'
32     ENDC = '\033[0m'
33     BOLD = '\033[1m'
34     UNDERLINE = '\033[4m'
35     FLASH = '\033[5m'
36
37 class RapidLog(object):
38     """
39     Class to deal with rapid logging
40     """
41     log = None
42
43     @staticmethod
44     def log_init(log_file, loglevel, screenloglevel, version):
45         # create formatters
46         screen_formatter = logging.Formatter("%(message)s")
47         file_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
48
49         # get a top-level logger,
50         # set its log level,
51         # BUT PREVENT IT from propagating messages to the root logger
52         #
53         log = logging.getLogger()
54         numeric_level = getattr(logging, loglevel.upper(), None)
55         if not isinstance(numeric_level, int):
56             raise ValueError('Invalid log level: %s' % loglevel)
57         log.setLevel(numeric_level)
58         log.propagate = 0
59
60         # create a console handler
61         # and set its log level to the command-line option 
62         # 
63         console_handler = logging.StreamHandler(sys.stdout)
64         #console_handler.setLevel(logging.INFO)
65         numeric_screenlevel = getattr(logging, screenloglevel.upper(), None)
66         if not isinstance(numeric_screenlevel, int):
67             raise ValueError('Invalid screenlog level: %s' % screenloglevel)
68         console_handler.setLevel(numeric_screenlevel)
69         console_handler.setFormatter(screen_formatter)
70
71         # create a file handler
72         # and set its log level
73         #
74         file_handler = logging.handlers.RotatingFileHandler(log_file, backupCount=10)
75         #file_handler = log.handlers.TimedRotatingFileHandler(log_file, 'D', 1, 5)
76         file_handler.setLevel(numeric_level)
77         file_handler.setFormatter(file_formatter)
78
79         # add handlers to the logger
80         #
81         log.addHandler(file_handler)
82         log.addHandler(console_handler)
83
84         # Check if log exists and should therefore be rolled
85         needRoll = os.path.isfile(log_file)
86
87
88         # This is a stale log, so roll it
89         if needRoll:    
90             # Add timestamp
91             log.debug('\n---------\nLog closed on %s.\n---------\n' % time.asctime())
92
93             # Roll over on application start
94             log.handlers[0].doRollover()
95
96         # Add timestamp
97         log.debug('\n---------\nLog started on %s.\n---------\n' % time.asctime())
98
99         log.debug("runrapid.py version: " + version)
100         RapidLog.log = log
101
102     @staticmethod
103     def exception(exception_info):
104         RapidLog.log.exception(exception_info)
105         raise Exception(exception_info)
106
107     def critical(critical_info):
108         RapidLog.log.critical(critical_info)
109         raise Exception(critical_info)
110
111     @staticmethod
112     def error(error_info):
113         RapidLog.log.error(error_info)
114
115     @staticmethod
116     def debug(debug_info):
117         RapidLog.log.debug(debug_info)
118
119     @staticmethod
120     def info(info):
121         RapidLog.log.info(info)