2019-10-07 17:20:19 +08:00
|
|
|
import logging
|
2019-09-17 14:17:29 +08:00
|
|
|
import re
|
|
|
|
|
2019-10-07 17:20:19 +08:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-09-17 14:17:29 +08:00
|
|
|
|
2019-10-01 15:42:51 +08:00
|
|
|
class MonkeyLogParser(object):
|
2019-09-17 14:17:29 +08:00
|
|
|
def __init__(self, log_path):
|
|
|
|
self.log_path = log_path
|
|
|
|
self.log_contents = self.read_log()
|
|
|
|
|
|
|
|
def read_log(self):
|
2021-04-06 21:19:27 +08:00
|
|
|
with open(self.log_path, "r") as log:
|
2019-09-17 14:17:29 +08:00
|
|
|
return log.read()
|
|
|
|
|
|
|
|
def print_errors(self):
|
2019-10-06 20:20:01 +08:00
|
|
|
errors = MonkeyLogParser.get_errors(self.log_contents)
|
2019-10-07 00:59:40 +08:00
|
|
|
if len(errors) > 0:
|
2019-10-07 17:20:19 +08:00
|
|
|
LOGGER.info("Found {} errors:".format(len(errors)))
|
|
|
|
for index, error_line in enumerate(errors):
|
|
|
|
LOGGER.info("Err #{}: {}".format(index, error_line))
|
2019-10-07 00:59:40 +08:00
|
|
|
else:
|
2019-10-07 17:20:19 +08:00
|
|
|
LOGGER.info("No errors!")
|
2019-09-17 14:17:29 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_errors(log_contents):
|
|
|
|
searcher = re.compile(r"^.*:ERROR].*$", re.MULTILINE)
|
|
|
|
return searcher.findall(log_contents)
|
|
|
|
|
|
|
|
def print_warnings(self):
|
2019-10-06 20:20:01 +08:00
|
|
|
warnings = MonkeyLogParser.get_warnings(self.log_contents)
|
2019-10-07 00:59:40 +08:00
|
|
|
if len(warnings) > 0:
|
2019-10-07 17:20:19 +08:00
|
|
|
LOGGER.info("Found {} warnings:".format(len(warnings)))
|
|
|
|
for index, warning_line in enumerate(warnings):
|
|
|
|
LOGGER.info("Warn #{}: {}".format(index, warning_line))
|
2019-10-07 00:59:40 +08:00
|
|
|
else:
|
2019-10-07 17:20:19 +08:00
|
|
|
LOGGER.info("No warnings!")
|
2019-09-17 14:17:29 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_warnings(log_contents):
|
|
|
|
searcher = re.compile(r"^.*:WARNING].*$", re.MULTILINE)
|
|
|
|
return searcher.findall(log_contents)
|