python怎么查找错误位置_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

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值