-
Notifications
You must be signed in to change notification settings - Fork 0
/
json2text_log.py
154 lines (116 loc) · 3.66 KB
/
json2text_log.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
import abc
import argparse
import json
import sys
"""
convert log from json format to human readable text format
the json log can be read from stdin or from a file
The text log can be written to the file or stdout
"""
class JsonLogReader:
@abc.abstractmethod
def read_log(self):
"""
read one log record
:return: a json log record or None if no more record is available
"""
raise NotImplementedError()
@abc.abstractmethod
def close(self):
"""
close the log reader
:return: None
"""
raise NotImplementedError()
class TextLogWriter:
@abc.abstractmethod
def write_log(self, text_log):
"""
write a text log
:param text_log: the log in text format
:return: None
"""
raise NotImplementedError()
@abc.abstractmethod
def close(self):
"""
close the log writer
:return: None
"""
raise NotImplementedError()
class StdinJsonLogReader(JsonLogReader):
def read_log(self):
for log in sys.stdin:
try:
return json.loads(log)
except Exception as ex:
pass
return None
def close(self):
pass
class FileJsonLogReader(JsonLogReader):
def __init__(self, file_name):
self._fp = open(file_name)
def read_log(self):
for line in self._fp:
try:
return json.loads(line)
except:
pass
return None
def close(self):
self._fp.close()
class StdoutTextLogWriter(TextLogWriter):
def write_log(self, text_log):
sys.stdout.write(text_log)
sys.stdout.write("\n")
def close(self):
pass
class FileTextLogWriter(TextLogWriter):
def __init__(self, file_name):
self._fp = open(file_name, "w")
def write_log(self, text_log):
self._fp.write(text_log)
self._fp.write("\n")
def close(self):
self._fp.close()
class Json2TextLogConverter:
def __init__(self, fields, delimiter, log_reader, log_writer):
self._fields = fields
self._delimiter = delimiter
self._log_reader = log_reader
self._log_writer = log_writer
def run(self):
while True:
json_log = self._log_reader.read_log()
if json_log is None:
break
values = []
for field in self._fields:
if field in json_log:
values.append(json_log[field])
self._log_writer.write_log(self._delimiter.join(values))
def parse_args():
parser = argparse.ArgumentParser(description="convert log from json to text format")
parser.add_argument("--fields", nargs="+", help="the fields will be print in the text", required=True)
parser.add_argument("--delimiter", help="the delimiter used between the fields", default=" ")
parser.add_argument("--input-file", help="the input file, read log from stdin if it is missing")
parser.add_argument("--output-file", help="the output file, print log to stdout if it is missing")
return parser.parse_args()
def main():
args = parse_args()
if args.input_file is None:
log_reader = StdinJsonLogReader()
else:
log_reader = FileJsonLogReader(args.input_file)
if args.output_file is None:
log_writer = StdoutTextLogWriter()
else:
log_writer = FileTextLogWriter(args.output_file)
converter = Json2TextLogConverter(args.fields, args.delimiter, log_reader, log_writer)
converter.run()
log_reader.close()
log_writer.close()
if __name__ == "__main__":
main()