python腳本查找日志中的錯誤行并導(dǎo)出到文件

腳本位置

城建大廈服務(wù)器

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

腳本作用

logana.sh

搜索7天內(nèi)的日志文件,調(diào)用loganalys.py文件進行錯誤日志過濾

loganalys.py

過濾日志文件將帶有ERROR|Error|Exception|EXCEPTION這些字段的日志行輸出到result.txt文本中。

使用方法

登陸城建大廈服務(wù)器

執(zhí)行

sh /root/logana.sh

日志文件詳細內(nèi)容

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('日志錯誤總數(shù)為------> ' + str(len(error_line)) + '個')
    print('日志錯誤類型數(shù)量為------> ' + str(len(list(set(error_line)))) + '個')
    for i in list(set(error_line)):
        with open('result.txt', 'a') as result:
            #result.write('日志錯誤總數(shù)為------> ' + str(len(error_line)) + '個'+ '\n' )
            #result.write('日志錯誤類型數(shù)量為------> ' + str(len(list(set(error_line)))) + '個' + '\n')
            result.write(file + '\n')
            result.write(i + '\n')
            print(i)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評論 19 139
  • 本文翻譯自logging howto 基礎(chǔ)教程 日志是跟蹤軟件運行時發(fā)生事件的一種手段。Python開發(fā)者在代碼中...
    大蟒傳奇閱讀 4,357評論 0 17
  • 本文翻譯自logging-cookbook 本文主要講述一些Python記錄日志的最佳實踐。 多模塊記錄日志 多次...
    大蟒傳奇閱讀 6,381評論 0 28
  • 安裝virtualenv。Mac下使用$ sudo apt-get install python-virtuale...
    嚴謹風閱讀 254評論 0 0
  • 【讀經(jīng)】 耶30 【金句】 你們卻要侍奉耶和華—你們的神和我為你們所要興起的王大衛(wèi)。”(耶利米書 30:9 和合本...
    chanor閱讀 401評論 0 0

友情鏈接更多精彩內(nèi)容