-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytail.py
85 lines (73 loc) · 2.15 KB
/
pytail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import argparse
import time
import colored
import re
parser = argparse.ArgumentParser(
prog='pytail',
description='tail and apply ansi properties based on log patterns',
epilog='report bugs to [email protected]')
parser.add_argument('filename')
args = parser.parse_args()
class filter:
def __init__(self, match):
self.combine = False
self.matches = []
self.matches.append(match)
return
def add_match(self, match):
self.matches.append(match)
return
def combine_enable(self):
self.combine = True
return
def combine_disable(self):
self.combine = False
return
def suppress(self, input):
for match in self.matches:
if re.search(match, input):
return True
return False
class pattern:
def __init__(self, match, fg, bg, attr):
self.fg=fg
self.bg=bg
self.rexp=match
self.attr=attr
return
def color(self, input):
fg = colored.fg(self.fg)
bg = colored.bg(self.bg)
attr = colored.attr(self.attr)
reset = colored.attr('reset')
message = f'{fg}{bg}{attr}{input}{reset}'
return message
def logp(line, patternlist, filterlist):
for filter in filterlist:
if filter.suppress(line):
message = ''
break
else:
for pattern in patternlist:
if re.search(pattern.rexp,line):
message = pattern.color(line)
break
else:
message = line
return message
def monitor(filename,patternlist,filterlist):
fileh = open(filename, 'r')
discard = fileh.readlines()
while True:
new = fileh.readlines()
if new:
print("%s" % logp(new[0].rstrip(),patternlist,filterlist))
else:
pass
time.sleep(1)
return
patterns = []
patterns.append(pattern('CDT', 'white', 'blue', 'bold'))
filters = []
filters.append(filter('DO NOT PRINT'))
monitor(args.filename,patterns,filters)