Skip to content

Instantly share code, notes, and snippets.

@attomos
Last active December 21, 2015 01:59
Show Gist options
  • Select an option

  • Save attomos/6231485 to your computer and use it in GitHub Desktop.

Select an option

Save attomos/6231485 to your computer and use it in GitHub Desktop.
Colorised JSON log watcher with a tag manager.
import time
import json
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
RESET = '\033[0m'
def termcolor(fg=None, bg=None):
codes = []
if fg is not None:
codes.append('3%d' % fg)
if bg is not None:
codes.append('10%d' % bg)
return '\033[%sm' % ';'.join(codes) if codes else ''
def colorize(message, fg=None, bg=None):
return termcolor(fg, bg) + message + RESET
file = open('./dipify-auth.log')
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
try:
line = json.loads(line)
level = line['level']
try:
tag = line['tag']
if level == 'info':
print colorize(tag, fg=BLUE) + ' ' + colorize(level, fg=GREEN) + ' ' + line['message']
elif level == 'warn':
print colorize(tag, fb=BLUE) + ' ' + colorize(level, fg=YELLOW) + ' ' + line['message']
elif level == 'error':
print colorize(tag, fg=BLUE) + ' ' + colorize(level, fg=RED) + ' ' + line['message']
except KeyError:
if level == 'info':
print colorize(level, fg=GREEN) + ' ' + line['message']
elif level == 'warn':
print colorize(level, fg=YELLOW) + ' ' + line['message']
elif level == 'error':
print colorize(level, fg=RED) + ' ' + line['message']
pass
except ValueError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment