Skip to content

Instantly share code, notes, and snippets.

@RexGibson
Forked from frank-kutzey/looker_log_jsonify.py
Last active June 15, 2017 15:50
Show Gist options
  • Save RexGibson/4c4d6d140d664c060b0401a1d474538d to your computer and use it in GitHub Desktop.
Save RexGibson/4c4d6d140d664c060b0401a1d474538d to your computer and use it in GitHub Desktop.
converts looker logs from plain text log to json like format - usefull for importing logs to EBK or ELK
#!/usr/bin/env python
from json import dumps
import sys
while True:
string = sys.stdin.readline()
if not string:
break
try:
level = 'UNKNOWN'
if '[DEBUG|' in string:
level = 'DEBUG'
if '[VERBOSE|' in string:
level = 'VERBOSE'
if '[INFO|' in string:
level = 'INFO'
if '[WARN|' in string:
level = 'WARN'
if '[ERROR|' in string:
level = 'ERROR'
if '[FATAL|' in string:
level = 'FATAL'
if '::' in string: #this is a log start line
#print the previous result
print(dumps(result))
#start building the next one
tmp_string = string[string.find('] ::') - 20:string.find('] ::')]
looker_log_type = string[string.find('] ::') - 20:string.find('] ::')][tmp_string.rfind('|') + 1:255].strip()
message=string[string.find('::') + 3:string.find('::') + 1000].strip()
result = dict(
createtime=string[0:string.find('[')].strip().replace(' ', 'T').replace('T+', ' +'),
looker_log_type=looker_log_type,
level=level,
message=string[string.find('::') + 3:string.find('::') + 1000].strip()
)
else:
result['message'] += string
except Exception as error:
sys.stderr.write(error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment