Created
February 25, 2016 16:28
-
-
Save darland/77fa7c6ac49631a36135 to your computer and use it in GitHub Desktop.
requirements -> Jinja2
This file contains 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/env python | |
import os | |
import sys | |
import json | |
import argparse | |
from jinja2 import Environment, FileSystemLoader | |
template = '[{{timestamp}}] {{fields.level}} {{message}}' | |
t = Environment().from_string(template) | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--filter", help="--filter @fields.level=ERROR") | |
parser.add_argument("--format", help="--format template.j2 ") | |
args = parser.parse_args() | |
if args.filter: | |
f = args.filter.split('=') | |
keys = f[0].split('.') | |
print(keys) | |
if args.format: | |
try: | |
THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | |
j2_env = Environment(loader=FileSystemLoader(THIS_DIR)) | |
t = j2_env.get_template(args.format) | |
except: | |
print("Template Not Found, Install deafult template") | |
try: | |
while True: | |
json_string = sys.stdin.readline() | |
if not json_string: | |
continue | |
parser_string = json.loads(json_string) | |
if args.filter: | |
if len(keys)==1: | |
if parser_string[keys[0]]!=f[1]: | |
continue | |
else: | |
if parser_string[keys[0]][keys[1]]!=f[1]: | |
continue | |
for k, v in parser_string.items(): | |
if k.startswith('@'): | |
parser_string[k[1::]] = parser_string.pop(k) | |
print(t.render(**parser_string)) | |
except KeyboardInterrupt: | |
print('It is ALL') | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment