Skip to content

Instantly share code, notes, and snippets.

@Leechael
Last active May 4, 2019 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leechael/5d459f7466e2fab141b93edcf23edb0b to your computer and use it in GitHub Desktop.
Save Leechael/5d459f7466e2fab141b93edcf23edb0b to your computer and use it in GitHub Desktop.
Parsing exported JSON log from LeanCloud
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Export logs from leancloud:
`lean logs --format=json --from=2019-03-12 --to=2019-03-13 > 20190312.log`
Then use this script parse the log file.
"""
import re
import json
from urllib.parse import quote_plus
from datetime import datetime
RE_START_OF_ERROR = re.compile('^\[\d{4,}')
RE_STRIP_BUCKET = re.compile('(\W*\[([^\]])+\]\W*)')
def safe_json_load(src):
try:
return json.loads(src)
except:
return {}
def parse_leancloud_error_log(content):
lines = content.strip().split('\n')
parsed = list(map(safe_json_load, lines))
errors = filter(lambda x: x.get('level', None) == 'error', parsed)
contents = list(map(lambda x: x['content'], errors))
if not contents:
return []
result = []
buffer = [contents[0]]
for i in contents[1:]:
r = RE_START_OF_ERROR.findall(i)
if len(r) > 0:
result.append(''.join(buffer).strip())
buffer = [i]
else:
buffer.append(i)
return result
if __name__ == '__main__':
from sys import argv, exit
from os import path
if len(argv) < 2:
print('Usage: parse_lc_logs.py [FILE_PATH]')
exit(1)
filepath = path.realpath(argv[1])
if not path.exists(filepath):
print(f'Target `{argv[1]}` not exists.')
exit(2)
messages = []
with open(filepath) as fp:
content = fp.read()
result = parse_leancloud_error_log(content)
# print("Total {} record found.".format(len(result)))
now = datetime.now().strftime('%Y-%m-%d')
messages.append("*[{}] LeanCloud错误日志日报 @ {}*".format(argv[2], now))
for i in result:
lines = i.split('\n')
position = RE_STRIP_BUCKET.subn('', lines[0])[0]
messages.append('{} :: {}'.format(position, lines[-1]))
# print('{} :: {}'.format(position, lines[-1]))
# TODO: counting deploys? Keywords: Deploy finished)
messages.append("Total {} errors.".format(len(result)))
print(quote_plus("\n".join(messages)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment