Press "Enter" to skip to content

方便的显示Apache日志更改后的新信息

看Apache的错误日志快看瞎我的眼睛了,因为如果直接打开看的话需要辨别如下信息:

  1. 哪些行是我这次的新信息
  2. 在新信息中错误/警告/提示的原因,相对应的文件名以及行数位置

为了更方便浏览日志,有了下面这段简单的脚本。

它比较此次运行和上次运行中日志文件的不同并以便于观察的格式列出相应信息(通过集合去除重复的提示信息,比如不同时间造成的错误信息但它们是相同的错误)。

参数1->查看错误信息
参数2->查看提示信息
参数3->查看警告信息

import re
import sys
import os

"""
@filename:hlpEye.py
@author:hupeiwei.com
@function:Extract different parts(error,notice and warning) of Apache's log file since last run of this script
@environment:python3 Apache2.4 Ubuntu 18.04(LTS)
@usage:
    python3 hlpEye.py 1 -> extract error info
    python3 hlpEye.py 2 -> extract notice info
    python3 hlpEye.py 3 -> extract warning info
"""

#read-only variable
_target = '/var/log/apache2/error.log' #Apache's log file
_tmp = '/tmp/hlpEye.bak'               #temporary file to store log file of last run time
pat_rm_prefix = r'\[.*\](.*)'          #regular expression to filter some additional fields (such as [Sun Sep 23 17:12:56.361995 2018] [php7:emerg] [pid 1283] [client 127.0.0.1:40878] )
pat_slct = r'(.*)?in(.*)?on(.*)?'      #regular expression to extract error/notice/warning infomation,file name and line position

def init_pro():
    os.system("sudo cp " + _target + " " + _tmp)

def cmp_file():
    lst = [set(),set(),set()]
    res = os.popen("sudo diff " + _target + " " + _tmp + " | sed '1d'")
    for s in res:
        s = re.findall(pat_rm_prefix,s)[0]
        if 'error:' in s.lower():
            lst[0].add(s)
        elif 'notice:' in s.lower():
            lst[1].add(s)
        elif 'warning:' in s.lower():
            lst[2].add(s)
    init_pro()
    return lst

def show(_set):
    print("\n-----------Info-----------------------------------------------------------------------------------File---------------------------Line(,referer)\n")
    for s in _set:
        s = re.findall(pat_slct,s)[0]
        print(s[0] + "              " + s[1] + "        " + s[2])
        print('\n')

def main():
    arg = sys.argv
    if len(arg) != 2:
        print("[-]Error para!\n")
        return
    if not os.path.exists(_tmp):
        init_pro()
        print("[+]First build temporary file !\n")
    lst = cmp_file()
    arg = arg[1]
    if arg == '1':
        show(lst[0])
    elif arg == '2':
        show(lst[1])
    elif arg == '3':
        show(lst[2])
    else:
        print('argument should be limit to 1~3')
    return

if __name__ == '__main__':
    main()

显示示例:

在这里插入图片描述

Be First to Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax