zhengxiaowai
12/9/2016 - 8:47 AM

json_escape.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
    nginx 的日志在存 json 的时候会把 “ 转义成 \x22
    此脚本实现类似于 Unix tail -f 但是会还原原始 json 数据
    可以单独执行不依赖于第三方 package

    Python2.+ | Python3.+
'''

import time as _time
import sys as _sys

# polyfill for python3
try:
    basestring
except NameError:
    basestring = str


def _version():
    info = _sys.version_info
    if info[0] == 2:
        return 2
    elif info[0] == 3:
        return 3
    else:
        raise RuntimeError('no support python version')

def escape(line):
    if not isinstance(line, basestring):
        raise TypeError('no support type to escape')

    if _version() == 2:
        return line.decode('unicode_escape')
    else:
        return bytes(line, 'utf-8').decode('unicode_escape')


def reader(thefile):
    thefile.seek(0,2)
    while True:
         line = thefile.readline()
         if not line:
             _time.sleep(0.1)
             continue
         yield line

def printer(line):
    if line:
        print(escape(line.strip()))


if __name__ == '__main__':
    try:
        the_file = _sys.argv[1]
        
        with open(the_file) as f:
            for line in reader(f):
                printer(line)

    except IndexError:
        raise IOError('you must choise a log file')