Skip to content

Instantly share code, notes, and snippets.

@mnakama
Created May 8, 2019 15:17
Show Gist options
  • Save mnakama/b1d6920704fb6ed2c1f1005f3b67b3bc to your computer and use it in GitHub Desktop.
Save mnakama/b1d6920704fb6ed2c1f1005f3b67b3bc to your computer and use it in GitHub Desktop.
#!/bin/python3
import json
import sys
import traceback
# termcolor available via pypi
try:
from termcolor import colored
except Exception as e:
print(e, file=sys.stderr)
sevColor = {
'info': 'cyan',
'INFO': 'cyan',
'DEBUG': 'blue',
'WARN': 'yellow',
'ERROR': 'red',
}
def main():
while True:
try:
line = input()
except EOFError:
return
try:
data = json.loads(line)
except ValueError as e:
print(line)
except:
traceback.print_exc()
else:
sev = data.get('severity')
text = '{:>5s}: {}'.format(
sev,
data.get('message', data.get('msg', line)),
)
try:
color = sevColor.get(sev, 'white')
print(colored(text, color))
except (KeyError, NameError):
print(text)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment