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 testPipeOutShouldSendMessageParseThreeParams(self): sys.stdin = ['error > one error', 'warning > one warning'] sys.stdout = MemoryWriter() defaults = getDefaults() defaults.actions.append(notifications.Print()) logtailer = LogTailer(defaults) logtailer.pipeOut() self.assertTrue('error > one error' in sys.stdout.captured[0])
def test_oneline_space_between_traces(self): sys.stdout = MemoryWriter() notifier = notifications.Print(PropertiesTraceStub()) logtrace = "this is a log trace in red" message = MessageMock(logtrace) log = LogMock() notifier.notify(message, log) expected_logtrace = "\n" + logtrace self.assertEqual(expected_logtrace, sys.stdout.captured[0])
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 testshouldNotColorizeifLevelKeyInaWord(self): # Testing boundary regex as for suggestion of # Carlo Bertoldi trace = "this is a logtrace where someinfoword could be found" sys.stdout = MemoryWriter() logcolors = LogColors() message = Message(logcolors) notifier = notifications.Print() anylog = Log('out.log') message.parse(trace, anylog) notifier.notify(message, anylog) self.assertEqual(trace, sys.stdout.captured[0]) self.assertEqual('', message.messageLevel)
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 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 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))
def _getDefaults(self): defaults = DefaultConfig() defaults.actions = [notifications.Print()] return defaults