Added sending run status including number of errors and warnings
Change-Id: I5085e17dcef431d6c22c156c95a3e161b428735d
This commit is contained in:
parent
4be4850928
commit
3168f4f696
@ -39,6 +39,9 @@ class FluentLogHandler(logging.Handler):
|
||||
'''Delimitate a new run in the stream of records with a new timestamp
|
||||
'''
|
||||
self.runlogdate = str(datetime.now())
|
||||
# reset counters
|
||||
self.__warning_counter = 0
|
||||
self.__error_counter = 0
|
||||
|
||||
def emit(self, record):
|
||||
data = {
|
||||
@ -46,4 +49,40 @@ class FluentLogHandler(logging.Handler):
|
||||
"loglevel": record.levelname,
|
||||
"message": self.formatter.format(record)
|
||||
}
|
||||
self.__update_stats(record.levelno)
|
||||
self.sender.emit(None, data)
|
||||
|
||||
# send stats related to the current run and reset state for a new run
|
||||
def send_run_summary(self, run_summary_required):
|
||||
if run_summary_required or self.__get_highest_level() == logging.ERROR:
|
||||
data = {
|
||||
"runlogdate": self.runlogdate,
|
||||
"loglevel": "RUN_SUMMARY",
|
||||
"message": self.__get_highest_level_desc(),
|
||||
"numloglevel": self.__get_highest_level(),
|
||||
"numerrors": self.__error_counter,
|
||||
"numwarnings": self.__warning_counter
|
||||
}
|
||||
self.sender.emit(None, data)
|
||||
|
||||
def __get_highest_level(self):
|
||||
if self.__error_counter > 0:
|
||||
return logging.ERROR
|
||||
elif self.__warning_counter > 0:
|
||||
return logging.WARNING
|
||||
return logging.INFO
|
||||
|
||||
def __get_highest_level_desc(self):
|
||||
highest_level = self.__get_highest_level()
|
||||
if highest_level == logging.INFO:
|
||||
return "GOOD RUN"
|
||||
elif highest_level == logging.WARNING:
|
||||
return "RUN WITH WARNINGS"
|
||||
else:
|
||||
return "RUN WITH ERRORS"
|
||||
|
||||
def __update_stats(self, levelno):
|
||||
if levelno == logging.WARNING:
|
||||
self.__warning_counter += 1
|
||||
elif levelno == logging.ERROR:
|
||||
self.__error_counter += 1
|
||||
|
20
vmtp/vmtp.py
20
vmtp/vmtp.py
@ -52,6 +52,7 @@ flow_num = 0
|
||||
return_code = 0
|
||||
fluent_logger = None
|
||||
|
||||
|
||||
class FlowPrinter(object):
|
||||
@staticmethod
|
||||
def print_desc(desc):
|
||||
@ -1239,10 +1240,21 @@ def run_vmtp(opts):
|
||||
|
||||
|
||||
def main():
|
||||
opts = parse_opts_from_cli()
|
||||
log.setup('vmtp', debug=opts.debug, logfile=opts.logfile)
|
||||
run_vmtp(opts)
|
||||
sys.exit(return_code)
|
||||
run_summary_required = False
|
||||
try:
|
||||
opts = parse_opts_from_cli()
|
||||
log.setup('vmtp', debug=opts.debug, logfile=opts.logfile)
|
||||
run_vmtp(opts)
|
||||
# If an exit occurs in run_vmtp such as printing version do not log run summary
|
||||
run_summary_required = True
|
||||
except Exception as e:
|
||||
LOG.exception(e)
|
||||
finally:
|
||||
if fluent_logger:
|
||||
# only send a summary record if there was an actual vmtp run or
|
||||
# if an error/exception was logged.
|
||||
fluent_logger.send_run_summary(run_summary_required)
|
||||
sys.exit(return_code)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
x
Reference in New Issue
Block a user