class LogColors(object): '''Provides the colors that will be used when printing Log4J levels''' def __init__(self): self.color = TermColorCodes() # defaults # color instance has dinamically assigned attributes # so pylint complaints. # pylint: disable-msg=E1101 self.warning = self.color.yellow self.warn = self.color.yellow self.error = self.color.magenta self.info = self.color.green self.trace = self.color.black self.debug = self.color.black self.fatal = self.color.red self.critical = self.color.red self.reset = self.color.reset self.backgroundemph = self.color.backgroundemph def parse_config(self, properties): for key in properties.get_keys(): code = self.color.getCode(properties.get_value(key)) if not code: continue setattr(self, key, code) def getLogColor(self, color): return self.color.getCode(color) def getLevelColor(self, level): level = level.lower() if level in loglevels.logLevels: return getattr(self, level)
def test_hostnameinconfig(self): logfile = 'out.log' fh = open(logfile, 'w') someLogTraces = ['FATAL> something went wrong', 'ERROR> not so wrong', 'WARN> be careful', 'DEBUG> looking behind the scenes', 'INFO> the app is running'] for line in someLogTraces: fh.write(line + '\n') fh.close() logcolors = LogColors() # using default colors termcolors = TermColorCodes() target = None notifier = notifications.Print(PropertiesMock()) message = Message(logcolors, target) log = Log(logfile) log.openLog() sys.stdout = MemoryWriter() hostname = socket.gethostname() for _ in range(len(someLogTraces)): line = log.readLine() line = line.rstrip() level = line.split('>') message.parse(line, log) output = (hostname + ': ' + logcolors.getLevelColor(level[0]) + line + termcolors.reset) notifier.notify(message, log) self.assertTrue(output in sys.stdout.captured) line = log.readLine() self.assertEqual('', line) message.parse(line, log) self.assertFalse(notifier.notify(message, log))
def testNotMarkMarkedNotMark(self): trace = "INFO this is an info trace" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) notifier = notifications.CornerMark(0.01) anylog = Log('out.log') message.parse(trace, anylog) padding = self.ttcols - len(notifier.MARK) output = padding * " " + termcolors.backgroundemph + notifier.MARK +\ termcolors.reset notifier.notify(message, anylog) self.assertFalse(sys.stdout.captured) def belowgap(): return 0 notifier.timer.corner_mark_ellapsed = belowgap trace = "FATAL there could be an error in the application" message.parse(trace, anylog) notifier.notify(message, anylog) self.assertTrue(sys.stdout.captured) self.assertEquals(output, sys.stdout.captured[0]) trace = "INFO this is an info trace" sys.stdout.flush() notifier.timer.corner_mark_ellapsed = overgap message.parse(trace, anylog) notifier.notify(message, anylog) self.assertFalse(sys.stdout.captured)
def __init__(self): self.color = TermColorCodes() # defaults # color instance has dinamically assigned attributes # so pylint complaints. # pylint: disable-msg=E1101 self.warning = self.color.yellow self.warn = self.color.yellow self.error = self.color.magenta self.info = self.color.green self.trace = self.color.black self.debug = self.color.black self.fatal = self.color.red self.critical = self.color.red self.reset = self.color.reset self.backgroundemph = self.color.backgroundemph
def __init__(self, gaptime): self.corner_time = float(gaptime) self.termcolors = TermColorCodes() self.len_mark = len(self.MARK) self.timer = Timer(self.corner_time) self.count = 0 self.flagged = False self.emphcolor = 'backgroundemph'
def test_trace_level(self): level = 'TRACE' trace = "TRACE level for finer informational log traces than DEBUG" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) notifier = notifications.Print() anylog = Log('out.log') message.parse(trace, anylog) output = logcolors.getLevelColor(level) + trace + termcolors.reset notifier.notify(message, anylog) self.assertEqual(output, sys.stdout.captured[0])
def testnotify(self): pattern = re.compile(r'hi, this line to be notified') trace = "info hi, this line to be notified" level = "INFO" notifier = notifications.Filter(pattern) sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) anylog = Log('out.log') message.parse(trace, anylog) notifier.notify(message, anylog) output = logcolors.getLevelColor(level) + trace + termcolors.reset self.assertEqual(output, sys.stdout.captured[0])
def testWillMarkForSpecifiedTime(self): trace = "FATAL there could be an error in the application" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) notifier = notifications.CornerMark(10) anylog = Log('out.log') message.parse(trace, anylog) padding = self.ttcols - len(notifier.MARK) output = padding * " " + termcolors.backgroundemph + notifier.MARK +\ termcolors.reset notifier.notify(message, anylog) self.assertEqual(output, sys.stdout.captured[0])
def testShouldColorizeWarningLevelAsWell(self): '''test that *warning* keyword gets colorized as well''' level = 'WARNING' trace = "WARNING there could be an error in the application" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) notifier = notifications.Print() anylog = Log('out.log') message.parse(trace, anylog) output = logcolors.getLevelColor(level) + trace + termcolors.reset notifier.notify(message, anylog) self.assertEqual(output, sys.stdout.captured[0])
def testshouldFailColorizeWithBackground(self): trace = "FATAL there could be an error in the application" level = 'WARN' sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() logcolors.parse_config(PropertiesBackGround()) message = Message(logcolors) notifier = notifications.Print() anylog = Log('out.log') message.parse(trace, anylog) output = logcolors.getLevelColor(level) + trace + termcolors.reset notifier.notify(message, anylog) self.assertNotEqual(output, sys.stdout.captured[0])
def testMarkedTARGETOverMarkableLevel(self): logfile = "/any/path/out.log" trace = "this is a FATAL targeted log trace" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() notifier = notifications.CornerMark(0.02) anylog = Log(logfile, PropertiesStub()) message = Message(logcolors, properties=PropertiesStub) padding = self.ttcols - len(notifier.MARK) output = padding * " " + termcolors.oncyanemph + notifier.MARK +\ termcolors.reset message.parse(trace, anylog) notifier.notify(message, anylog) self.assertEqual(output, sys.stdout.captured[0])
def testshouldColorizefirstLevelFoundignoringSecondinSameTrace(self): # Test for fix 5 # Should give priority to FATAL in next trace level = 'FATAL' trace = "FATAL there could be an error in the application" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) notifier = notifications.Print() anylog = Log('out.log') message.parse(trace, anylog) output = (logcolors.getLevelColor(level) + trace + termcolors.reset) notifier.notify(message, anylog) self.assertEqual(output, sys.stdout.captured[0])
def testMarkedFATALMarkedWARNING(self): trace = "FATAL this is a fatal trace" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) notifier = notifications.CornerMark(0.02) anylog = Log('out.log') message.parse(trace, anylog) notifier.notify(message, anylog) padding = self.ttcols - len(notifier.MARK) output = padding * " " + termcolors.onyellowemph + notifier.MARK +\ termcolors.reset trace = "WARN this is just a warn" message.parse(trace, anylog) notifier.notify(message, anylog) self.assertEquals(output, sys.stdout.captured[2])
def __init__(self, default_config, wait_for=time.sleep): self.arrayLog = [] self.logcolors = default_config.logcolors self.pause = default_config.pause self.silence = default_config.silence self.actions = notifications.Print() self.throttleTime = default_config.throttle self.target = default_config.target self.properties = default_config.properties self.mailAction = None self.logger = logging.getLogger('SSHLogTailer') self.sshusername = None self.hostnames = {} self.hostnameChannels = {} self.color = TermColorCodes() self.rsa_key = SSH_KEY self._wait_for = wait_for
def testshouldColorizeMultilineLogTraces(self): trace = 'FATAL> something went wrong\nin here as well' trace0, trace1 = trace.split('\n') level = 'FATAL' termcolors = TermColorCodes() # now assert trace0 and trace1 are in FATAL level sys.stdout = MemoryWriter() logcolors = LogColors() message = Message(logcolors) notifier = notifications.Print() anylog = Log('out.log') expectedLogTrace0 = logcolors.getLevelColor(level) + \ trace0 + termcolors.reset expectedLogTrace1 = logcolors.getLevelColor(level) + \ trace1 + termcolors.reset message.parse(trace0, anylog) notifier.notify(message, anylog) self.assertEqual(expectedLogTrace0, sys.stdout.captured[0]) self.assertEqual('FATAL', message.messageLevel) message.parse(trace1, anylog) notifier.notify(message, anylog) self.assertEqual(expectedLogTrace1, sys.stdout.captured[2]) self.assertEqual('FATAL', message.messageLevel)
def testMessage(self): logcolors = LogColors() # using default colors termcolors = TermColorCodes() target = None notifier = notifications.Print() message = Message(logcolors, target) log = Log(self.logfile) log.openLog() sys.stdout = MemoryWriter() #testing Colors with default pauseModes for count in range(len(self.someLogTraces)): line = log.readLine() line = line.rstrip() level = line.split('>') message.parse(line, log) output = (logcolors.getLevelColor(level[0]) + line + termcolors.reset) notifier.notify(message, log) self.assertTrue(output in sys.stdout.captured) line = log.readLine() self.assertEqual('', line) message.parse(line, log) self.assertFalse(notifier.notify(message, log))