Last active
February 15, 2017 10:05
-
-
Save PVince81/261842663994b73ac5d02773d522a965 to your computer and use it in GitHub Desktop.
Prettify owncloud log and parses stack traces out of the message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import json | |
def expand_stack(data, count): | |
# detect stack | |
message = data['message'] | |
stack_start = message.find('[{'); | |
stack_end = message.rfind('}]'); | |
if stack_start < 0 or stack_end < 0: | |
return data | |
try: | |
stack = json.loads(message[stack_start:stack_end + 2]) | |
data['message'] = message[0: stack_start] + "(see stack attribute)" | |
data['stack'] = stack | |
except: | |
sys.stderr.write('Could not parse stack on line %i: %s' % (count, message)) | |
return data | |
def process_file(f): | |
count = 1 | |
for line in f: | |
try: | |
data = json.loads(line) | |
data = expand_stack(data, count) | |
print(json.dumps(data, indent=4)) | |
except: | |
sys.stderr.write('Could not parse line %i: %s' % (count, line)) | |
count = count + 1 | |
if len(sys.argv) > 1: | |
for file_name in sys.argv[1:]: | |
with open(file_name) as f: | |
process_file(f) | |
else: | |
process_file(sys.stdin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment