예제 #1
0
def error_log(request):
    log_path = 'logs/nginx/web_error.log'
    log_str = DBHandler.load_log(log_path)
    # 分页处理
    page_index = int(request.GET.get('page')) if request.GET.get('page') else 1
    data_list = log_str.split('\n')
    data_count = len(data_list)
    page = Page(request.path_info + '?page=', page_index,
                data_count // 100 + 1)
    page_str = page.page_str()
    log_list = page.video_page(data_list, 100)
    if log_list == -1:
        return HttpResponse('请求错误')
    log_str = '\n'.join(log_list)
    return render(request, 'admin_update_log.html', {
        "log": log_str,
        'page_str': page_str,
    })
예제 #2
0
def view_log(request, log_date=0):
    # 加载log文件
    if log_date == 0:
        log_date = time.strftime("%Y-%m-%d", time.localtime(time.time()))
        is_today = True
        log_path = 'uwsgi.log'
    elif log_date == time.strftime("%Y-%m-%d", time.localtime(time.time())):
        is_today = True
        log_path = 'uwsgi.log'
    else:
        is_today = False
        month = "-".join(log_date.split('-')[0:2])
        log_path = 'logs/%s/uwsgi-%s.log' % (month, log_date)
        if not (os.path.exists(log_path) and os.path.isfile(log_path)):
            log_str = '当天无日志'
            return render(
                request, 'admin_log.html', {
                    "log": log_str,
                    'log_date': log_date,
                    'is_today': is_today,
                    'page_str': '',
                })
    log = DBHandler.load_log(log_path)
    # 剔除部分字段
    import re
    log = re.sub(r'(\*\*\*\sStarting\suWSGI.+?interpreter\smode\s\*\*\*)',
                 '*** Starting uWSGI ***',
                 log,
                 flags=re.DOTALL)
    log = re.sub(r'(nsukey\=.+)', '<<wechat>>', log)
    log = re.sub(r'(\[pid:.+?\.php.+?\(HTTP\/1\.1\s404\).+\n)', '', log)
    log = re.sub(
        r'(\[pid:.+?GET\s\/\s\=\>\sgenerated.+?\(HTTP\/1\.1\s404\).+\n)', '',
        log)
    log = re.sub(r'(\[pid.+?\])', '', log)
    log = re.sub(r'(\(\)\s\{.+?bytes\})', '', log)
    log = re.sub(r'(\d+?\sheaders\sin.+?\))', '', log)
    log = re.sub(r'(generated.+?msecs)', '', log)
    log = re.sub(r'(\=\>\s+?\(HTTP\/1\.1\s200\))', '', log)
    log = re.sub(r'(\s\[\w{3}\s\w{3}\s\d{1,2}\s)', '[', log)
    log = re.sub(r'(\s\d{4}\]\s)', ']', log)
    log_str = re.sub(r'(.+?\(HTTP\/1\.1\s30\d\).+\n)', '', log)
    # print(log_str)
    # 分页处理
    page_index = int(request.GET.get('page')) if request.GET.get('page') else 1
    data_list = log_str.split('\n')
    data_count = len(data_list)
    page = Page(request.path_info + '?page=', page_index,
                data_count // 100 + 1)
    page_str = page.page_str()
    log_list = page.video_page(data_list, 100)
    if log_list == -1:
        return redirect('/view_log/%s/' % log_date)
    log_str = '\n'.join(log_list)
    if log_date == 0:
        log_date = time.strftime("%Y-%m-%d", time.localtime(time.time()))
    return render(
        request, 'admin_log.html', {
            "log": log_str,
            'log_date': log_date,
            'is_today': is_today,
            'page_str': page_str,
        })