4 ## Copyright (c) 2020 Intel Corporation
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
10 ## http://www.apache.org/licenses/LICENSE-2.0
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.
20 from logging.handlers import RotatingFileHandler
21 from logging import handlers
26 class bcolors(object):
37 class RapidLog(object):
39 Class to deal with rapid logging
44 def log_init(log_file, loglevel, screenloglevel, version):
45 log = logging.getLogger(__name__)
46 makeFileHandler = True
47 makeStreamHandler = True
48 if len(log.handlers) > 0:
49 for handler in log.handlers:
50 if isinstance(handler, logging.FileHandler):
51 makeFileHandler = False
52 elif isinstance(handler, logging.StreamHandler):
53 makeStreamHandler = False
56 screen_formatter = logging.Formatter("%(message)s")
57 # create a console handler
58 # and set its log level to the command-line option
60 console_handler = logging.StreamHandler(sys.stdout)
61 #console_handler.setLevel(logging.INFO)
62 numeric_screenlevel = getattr(logging, screenloglevel.upper(), None)
63 if not isinstance(numeric_screenlevel, int):
64 raise ValueError('Invalid screenlog level: %s' % screenloglevel)
65 console_handler.setLevel(numeric_screenlevel)
66 console_handler.setFormatter(screen_formatter)
67 # add handler to the logger
69 log.addHandler(console_handler)
72 file_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
73 # get a top-level logger,
75 # BUT PREVENT IT from propagating messages to the root logger
77 numeric_level = getattr(logging, loglevel.upper(), None)
78 if not isinstance(numeric_level, int):
79 raise ValueError('Invalid log level: %s' % loglevel)
80 log.setLevel(numeric_level)
84 # create a file handler
85 # and set its log level
87 file_handler = logging.handlers.RotatingFileHandler(log_file, backupCount=10)
88 file_handler.setLevel(numeric_level)
89 file_handler.setFormatter(file_formatter)
91 # add handler to the logger
93 log.addHandler(file_handler)
95 # Check if log exists and should therefore be rolled
96 needRoll = os.path.isfile(log_file)
99 # This is a stale log, so roll it
102 log.debug('\n---------\nLog closed on %s.\n---------\n' % time.asctime())
104 # Roll over on application start
105 file_handler.doRollover()
108 log.debug('\n---------\nLog started on %s.\n---------\n' % time.asctime())
110 log.debug("rapid version: " + version)
115 for handler in RapidLog.log.handlers:
116 if isinstance(handler, logging.FileHandler):
118 RapidLog.log.removeHandler(handler)
121 def exception(exception_info):
122 RapidLog.log.exception(exception_info)
123 raise Exception(exception_info)
125 def critical(critical_info):
126 RapidLog.log.critical(critical_info)
127 raise Exception(critical_info)
130 def error(error_info):
131 RapidLog.log.error(error_info)
134 def debug(debug_info):
135 RapidLog.log.debug(debug_info)
139 RapidLog.log.info(info)