Python 過濾錯誤log并導(dǎo)出的實(shí)例
前言:
測試過程中獲取App相關(guān)log后,如何快速找出crash的部分,并導(dǎo)出到新的文件呢?
感興趣的話,繼續(xù)往下看吧~
思路:遍歷多個日志文件,找出含有Error和Crash的日志,并把該行日志輸出到另一個文件result.txt中。
def find_log(path):
file_list = os.listdir(path)
for file in file_list:
file_name = file
log_path = os.path.join(path, file)
with open(log_path, 'rb') as f:
lines = f.readlines()
index = 0
for line in lines:
index += 1
if 'Crash' in line.decode("utf8", "ignore") or 'Error' in line.decode("utf8", "ignore"):
ss = re.findall(r'(.*Crash.*)', line.decode("utf8", "ignore"))
zz = re.findall(r'(.*Error.*)', line.decode("utf8", "ignore"))
if len(zz) > 0:
with open('result.txt', 'a') as ff:
ff.write('文件名:'+file_name + ' 第' + str(index) + '行: ' + zz[0] + '\n')
elif len(ss) > 0:
with open('result.txt', 'a') as ff:
ff.write('文件名:'+file_name + ' 第' + str(index) + '行: ' + ss[0] + '\n')
else:
break
result.txt文件展示如下:
文件名:amstart.log 第611行: 01-12 11:10:33.534 E/FirebaseCrash(14844): Failed waiting for crash api to load.
文件名:amstart.log 第612行: 01-12 11:10:33.534 E/FirebaseCrash(14844): java.lang.InterruptedException
文件名:amstart.log 第613行: 01-12 11:10:33.534 E/FirebaseCrash(14844): at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedNanos(AbstractQueuedSynchronizer.java:1013)
文件名:amstart.log 第614行: 01-12 11:10:33.534 E/FirebaseCrash(14844): at java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireSharedNanos(AbstractQueuedSynchronizer.java:1302)
文件名:amstart.log 第615行: 01-12 11:10:33.534 E/FirebaseCrash(14844): at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:249)
文件名:amstart.log 第616行: 01-12 11:10:33.534 E/FirebaseCrash(14844): at com.google.firebase.crash.FirebaseCrash.zzbsk(Unknown Source)
文件名:amstart.log 第617行: 01-12 11:10:33.534 E/FirebaseCrash(14844): at com.google.firebase.crash.FirebaseCrash.zza(Unknown Source)
文件名:amstart.log 第618行: 01-12 11:10:33.534 E/FirebaseCrash(14844): at com.google.firebase.crash.zza.run(Unknown Source)
文件名:amstart.log 第619行: 01-12 11:10:33.534 E/FirebaseCrash(14844): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
文件名:amstart.log 第620行: 01-12 11:10:33.534 E/FirebaseCrash(14844): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
文件名:amstart.log 第621行: 01-12 11:10:33.534 E/FirebaseCrash(14844): at java.lang.Thread.run(Thread.java:818)
文件名:uninstall.log 第213行: 01-12 11:16:33.382 W/ActivityManager( 1068): Error in app com.baidu.mtc.new_monkey running instrumentation ComponentInfo{com.baidu.mtc.new_monkey.test/android.support.test.runner.AndroidJUnitRunner}:
文件名:uninstall.log 第219行: 01-12 11:16:33.383 W/ActivityManager( 1068): Error shutting down UiAutomationConnection
文件名:logcat.log 第31653行: 01-12 11:13:48.556 E/Gn_Assist(17385): GnVoiceService dispatchRecoError className is empty
文件名:logcat.log 第31654行: 01-12 11:13:48.556 E/Gn_Assist(17385): FocusException getErrorMsg ERROR_NO_MATCH
文件名:install.log 第26514行: 01-12 11:09:40.641 W/System.err(14314): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
以上這篇Python 過濾錯誤log并導(dǎo)出的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python Requests訪問網(wǎng)絡(luò)更方便
這篇文章主要介紹了使用Python Requests訪問網(wǎng)絡(luò),Python Requests 是一個非常強(qiáng)大的 HTTP 客戶端庫,用于發(fā)送 HTTP 請求,獲取響應(yīng)等操作,通過這個庫,你可以輕松地與 Web 服務(wù)進(jìn)行交互,實(shí)現(xiàn)各種網(wǎng)絡(luò)請求2024-01-01
Python中uuid模塊生成唯一標(biāo)識符的方法詳解
這篇文章主要給大家介紹了關(guān)于Python中uuid模塊生成唯一標(biāo)識符的相關(guān)資料,uuid庫是Python標(biāo)準(zhǔn)庫中的一個功能強(qiáng)大的庫,可以用于生成全局唯一標(biāo)識符(UUID),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
Python趣味編程實(shí)現(xiàn)手繪風(fēng)視頻示例
本文與計(jì)算機(jī)視覺相關(guān),使用Python將圖片由自然風(fēng)轉(zhuǎn)化為手繪風(fēng),期間未對圖片進(jìn)行任何預(yù)處理、后處理;代碼中只借助了兩個常見庫,核心計(jì)算由Numpy負(fù)責(zé) ,Pillow 負(fù)責(zé)圖片讀寫2021-10-10
Python 處理數(shù)據(jù)庫事務(wù)的操作方法
在Python中,處理數(shù)據(jù)庫事務(wù)通常涉及使用特定的數(shù)據(jù)庫驅(qū)動如sqlite3、PyMySQL和psycopg2等,這些庫提供事務(wù)管理功能,允許開發(fā)者手動控制事務(wù)的提交和回滾,本文給大家介紹Python如何處理數(shù)據(jù)庫事務(wù),感興趣的朋友一起看看吧2024-10-10
基于Python實(shí)現(xiàn)一個春節(jié)倒計(jì)時腳本
春節(jié)即將到來,本文將為大家介紹一個通過Python實(shí)現(xiàn)的春節(jié)倒計(jì)時腳本,文中的示例代碼簡潔易懂,感興趣的小伙伴可以自己動手嘗試一下2022-01-01
Python數(shù)據(jù)分析之使用scikit-learn構(gòu)建模型
這篇文章主要介紹了Python數(shù)據(jù)分析之使用scikit-learn構(gòu)建模型,sklearn提供了model_selection模型選擇模塊、preprocessing數(shù)據(jù)預(yù)處理模塊、decompisition特征分解模塊,更多相關(guān)內(nèi)容需要朋友可以參考下面文章內(nèi)容2022-08-08
python如何獲得list或numpy數(shù)組中最大元素對應(yīng)的索引
這篇文章主要介紹了python如何獲得list或numpy數(shù)組中最大元素對應(yīng)的索引,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

