diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-07-31 15:53:32 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-07-31 15:53:32 +0300 |
commit | 7d799b85d6644fc1676b21277ce758b5bd313a5b (patch) | |
tree | 48afc3f9221a1d20e4133937903f9ec4948159cc | |
parent | df26f3c4af7d4ca34a3ffd85a91a600590623d3b (diff) | |
download | slog-7d799b85d6644fc1676b21277ce758b5bd313a5b.tar.gz |
Add: scripts to handle slog files
-rwxr-xr-x | slog-errors | 61 | ||||
-rwxr-xr-x | slog-pretty | 33 |
2 files changed, 94 insertions, 0 deletions
diff --git a/slog-errors b/slog-errors new file mode 100755 index 0000000..920e87d --- /dev/null +++ b/slog-errors @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# +# Read Qvarn structured log files, look for errors, and output all +# messages related to a failed HTTP request (status code 5xx). + + +import json +import sys + + +def parse_log_file(filename): + with open(filename) as f: + for line in f: + yield json.loads(line) + + +def find_contexts(msgs): + return set(get_context(msg) for msg in msgs) + + +def get_context(msg): + return msg['_context'], msg['_process_id'], msg['_thread_id'] + + +def is_in_context(msg, context): + return get_context(msg) == context + + +def filter_msgs(msgs, func): + for msg in msgs: + if func(msg): + yield msg + +def has_error_status(msgs): + for msg in msgs: + if msg['msg_type'] == 'error': + return True + if msg['msg_type'] == 'http-response' and msg['status'] >= 400: + return True + if '_traceback' in msg: + return True + return False + + +def show_msgs(msgs): + sys.stdout.write(json.dumps(msgs)) + sys.stdout.write('\n') + + +msgs = [] +for filename in sys.argv[1:]: + for msg in parse_log_file(filename): + msg['_filename'] = filename + msgs.append(msg) + + +contexts = find_contexts(msgs) +for context in contexts: + context_msgs = list(filter_msgs(msgs, lambda m: is_in_context(m, context))) + if has_error_status(context_msgs): + show_msgs(context_msgs) diff --git a/slog-pretty b/slog-pretty new file mode 100755 index 0000000..80f07fb --- /dev/null +++ b/slog-pretty @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +import json +import sys + +import yaml + + +def text_representer(dumper, data): + if '\n' in data: + return dumper.represent_scalar( + u'tag:yaml.org,2002:str', data, style='|') + return dumper.represent_scalar( + u'tag:yaml.org,2002:str', data, style='') + +yaml.add_representer(str, text_representer) +yaml.add_representer(unicode, text_representer) + + +def dump(f): + for line in f: + obj = json.loads(line.strip()) + yaml.dump( + obj, stream=sys.stdout, indent=4, default_flow_style=False, + explicit_start=True, explicit_end=True) + + +if len(sys.argv) == 1: + dump(sys.stdin) +else: + for filename in sys.argv[1:]: + with open(filename) as f: + dump(f) |