forked from sensiml/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_tail.py
56 lines (48 loc) · 1.76 KB
/
log_tail.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
import io
import json
import sys
import time
import colorama
colorama.init()
def follow(file, sleep_sec=0.1):
"""Yield each line from a file as they are written.
`sleep_sec` is the time to sleep after empty reads."""
line = ""
while True:
tmp = file.readline()
if tmp is not None:
line += tmp
if line.endswith("\n"):
yield line
line = ""
elif sleep_sec:
time.sleep(sleep_sec)
if __name__ == "__main__":
with open(sys.argv[1], "r") as file:
file.seek(0, io.SEEK_END)
for line in follow(file):
if len(line) < 10:
continue
try:
if "/MainProcess]" in line or "ForkPoolWorker" in line:
message = line[line.index("] ") + 2 :]
else:
# print(line.index("] {"))
message = line[line.index("] {") + 2 :]
except Exception as e:
# print(colorama.Fore.RED+str(e), end='\n')
message = line
try:
if " ERROR " in line:
message = json.dumps(json.loads(message), indent=4, sort_keys=True)
print(colorama.Fore.RED + message, end="\n")
elif " INFO " in line:
message = json.dumps(json.loads(message), indent=4, sort_keys=True)
print(colorama.Fore.BLUE + message, end="\n")
elif " DEBUG " in line:
message = json.dumps(json.loads(message), indent=4, sort_keys=True)
print(colorama.Fore.GREEN + message, end="\n")
else:
print(line, end="")
except:
print(line, end="\n")