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):
46 screen_formatter = logging.Formatter("%(message)s")
47 file_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
49 # get a top-level logger,
51 # BUT PREVENT IT from propagating messages to the root logger
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)
60 # create a console handler
61 # and set its log level to the command-line option
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)
71 # create a file handler
72 # and set its log level
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)
79 # add handlers to the logger
81 log.addHandler(file_handler)
82 log.addHandler(console_handler)
84 # Check if log exists and should therefore be rolled
85 needRoll = os.path.isfile(log_file)
88 # This is a stale log, so roll it
91 log.debug('\n---------\nLog closed on %s.\n---------\n' % time.asctime())
93 # Roll over on application start
94 log.handlers[0].doRollover()
97 log.debug('\n---------\nLog started on %s.\n---------\n' % time.asctime())
99 log.debug("runrapid.py version: " + version)
103 def exception(exception_info):
104 RapidLog.log.exception(exception_info)
105 raise Exception(exception_info)
107 def critical(critical_info):
108 RapidLog.log.critical(critical_info)
109 raise Exception(critical_info)
112 def error(error_info):
113 RapidLog.log.error(error_info)
116 def debug(debug_info):
117 RapidLog.log.debug(debug_info)
121 RapidLog.log.info(info)