python脚本查找日志中的错误行并导出到文件

脚本位置

城建大厦服务器

/root/logana.sh
https://coding.net/u/himan/p/script/git/raw/master/loganalys.py

脚本作用

logana.sh

搜索7天内的日志文件,调用loganalys.py文件进行错误日志过滤

loganalys.py

过滤日志文件将带有ERROR|Error|Exception|EXCEPTION这些字段的日志行输出到result.txt文本中。

使用方法

登陆城建大厦服务器

执行

sh /root/logana.sh

日志文件详细内容

vim /root/logana.sh

newbatchjob="/rizhi/web_batchjob_new/batchjob/"
batchjob="/rizhi/web_batchjob/batchjob/"
back="/rizhi/web_pc_back/web_back2/"
baobiao="/rizhi/service_pcbaobiao/pcbaobiao/"
rm -rf /tmp/logana_temp/*
mkdir -p /tmp/logana_temp${newbatchjob} /tmp/logana_temp${batchjob} /tmp/logana_temp${back} /tmp/logana_temp${baobiao}
find ${newbatchjob} -type f -mtime -7 -exec cp {} /tmp/logana_temp${newbatchjob} \; &
find ${batchjob} -type f -mtime -7 -exec cp {} /tmp/logana_temp${batchjob} \; &
find ${back} -type f -mtime -7 -exec cp {} /tmp/logana_temp${back} \; &
find ${baobiao} -type f -mtime -7 -exec cp {} /tmp/logana_temp${baobiao} \; && find /tmp/logana_temp${baobiao}  | xargs gzip -d &
wait
cd /tmp/logana_temp${newbatchjob} ; curl -sSL https://coding.net/u/himan/p/script/git/raw/master/loganalys.py | python3 && cp result.txt /tmp/logana_temp/result_newbatchjob.txt &
cd /tmp/logana_temp${batchjob} ; curl -sSL https://coding.net/u/himan/p/script/git/raw/master/loganalys.py | python3 && cp result.txt /tmp/logana_temp/result_batchjob.txt &
cd /tmp/logana_temp${back} ; curl -sSL https://coding.net/u/himan/p/script/git/raw/master/loganalys.py | python3 && cp result.txt /tmp/logana_temp/result_back.txt &
cd /tmp/logana_temp${baobiao} ; curl -sSL https://coding.net/u/himan/p/script/git/raw/master/loganalys.py | python3 && cp result.txt /tmp/logana_temp/result_baobiao.txt &
wait
echo "All Done!"
ls -l /tmp/logana_temp/*.txt

curl -sSL https://coding.net/u/himan/p/script/git/raw/master/loganalys.py

import re, os, sys
final = []
pwd = os.getcwd()
def listfilename():
    cwd = os.getcwd()
    list = os.listdir(cwd)
    for i in list:
        list2 = os.path.join(cwd, i)
        if os.path.isdir(list2):
            os.chdir(list2)
            listfilename()
        else:
            final.append(list2)

if len(sys.argv) != 1:
    filenames = sys.argv[1:]
else:
    listfilename()
    filenames = final

for file in final:
    print(file)
    filename = file
    log_dir = os.path.join(pwd, filename)
    with open(log_dir, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        error_line = []
        for line in lines:
            if 'Error' in line or 'ERROR' in line or 'Exception' in line or 'EXCEPTION' in line:
                line1 = re.findall(r'(ERROR|Error|Exception|EXCEPTION) (.*)', line)
                if len(line1) >= 1:
                    line1 = line1[0]
                    error_line.append(line1[1])
                else:
                    break
    print('日志错误总数为------> ' + str(len(error_line)) + '个')
    print('日志错误类型数量为------> ' + str(len(list(set(error_line)))) + '个')
    for i in list(set(error_line)):
        with open('result.txt', 'a') as result:
            #result.write('日志错误总数为------> ' + str(len(error_line)) + '个'+ '\n' )
            #result.write('日志错误类型数量为------> ' + str(len(list(set(error_line)))) + '个' + '\n')
            result.write(file + '\n')
            result.write(i + '\n')
            print(i)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,286评论 19 139
  • 本文翻译自logging howto 基础教程 日志是跟踪软件运行时发生事件的一种手段。Python开发者在代码中...
    大蟒传奇阅读 4,285评论 0 17
  • 本文翻译自logging-cookbook 本文主要讲述一些Python记录日志的最佳实践。 多模块记录日志 多次...
    大蟒传奇阅读 6,310评论 0 28
  • 安装virtualenv。Mac下使用$ sudo apt-get install python-virtuale...
    严谨风阅读 240评论 0 0
  • 【读经】 耶30 【金句】 你们却要侍奉耶和华—你们的神和我为你们所要兴起的王大卫。”(耶利米书 30:9 和合本...
    chanor阅读 344评论 0 0