Python 過濾錯(cuò)誤log并導(dǎo)出的實(shí)例
前言:
測(cè)試過程中獲取App相關(guān)log后,如何快速找出crash的部分,并導(dǎo)出到新的文件呢?
感興趣的話,繼續(xù)往下看吧~
思路:遍歷多個(gè)日志文件,找出含有Error和Crash的日志,并把該行日志輸出到另一個(gè)文件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 過濾錯(cuò)誤log并導(dǎo)出的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python Requests訪問網(wǎng)絡(luò)更方便
這篇文章主要介紹了使用Python Requests訪問網(wǎng)絡(luò),Python Requests 是一個(gè)非常強(qiáng)大的 HTTP 客戶端庫(kù),用于發(fā)送 HTTP 請(qǐng)求,獲取響應(yīng)等操作,通過這個(gè)庫(kù),你可以輕松地與 Web 服務(wù)進(jìn)行交互,實(shí)現(xiàn)各種網(wǎng)絡(luò)請(qǐng)求2024-01-01python 剪切移動(dòng)文件的實(shí)現(xiàn)代碼
移動(dòng)復(fù)制文件通過os.rename方法,先進(jìn)行文件是否存在判斷,如需更加復(fù)雜相同文件判斷可以根據(jù)文件屬性進(jìn)行判斷,此處只使用同名檢查,并刪除已存在文件,來(lái)實(shí)現(xiàn)覆蓋。這篇文章主要介紹了python 剪切移動(dòng)文件的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-08-08Python中uuid模塊生成唯一標(biāo)識(shí)符的方法詳解
這篇文章主要給大家介紹了關(guān)于Python中uuid模塊生成唯一標(biāo)識(shí)符的相關(guān)資料,uuid庫(kù)是Python標(biāo)準(zhǔn)庫(kù)中的一個(gè)功能強(qiáng)大的庫(kù),可以用于生成全局唯一標(biāo)識(shí)符(UUID),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08Python趣味編程實(shí)現(xiàn)手繪風(fēng)視頻示例
本文與計(jì)算機(jī)視覺相關(guān),使用Python將圖片由自然風(fēng)轉(zhuǎn)化為手繪風(fēng),期間未對(duì)圖片進(jìn)行任何預(yù)處理、后處理;代碼中只借助了兩個(gè)常見庫(kù),核心計(jì)算由Numpy負(fù)責(zé) ,Pillow 負(fù)責(zé)圖片讀寫2021-10-10python中無(wú)法導(dǎo)入本地安裝好的第三方庫(kù)問題
這篇文章主要介紹了python中無(wú)法導(dǎo)入本地安裝好的第三方庫(kù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Python 處理數(shù)據(jù)庫(kù)事務(wù)的操作方法
在Python中,處理數(shù)據(jù)庫(kù)事務(wù)通常涉及使用特定的數(shù)據(jù)庫(kù)驅(qū)動(dòng)如sqlite3、PyMySQL和psycopg2等,這些庫(kù)提供事務(wù)管理功能,允許開發(fā)者手動(dòng)控制事務(wù)的提交和回滾,本文給大家介紹Python如何處理數(shù)據(jù)庫(kù)事務(wù),感興趣的朋友一起看看吧2024-10-10基于Python實(shí)現(xiàn)一個(gè)春節(jié)倒計(jì)時(shí)腳本
春節(jié)即將到來(lái),本文將為大家介紹一個(gè)通過Python實(shí)現(xiàn)的春節(jié)倒計(jì)時(shí)腳本,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以自己動(dòng)手嘗試一下2022-01-01Python數(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-08python如何獲得list或numpy數(shù)組中最大元素對(duì)應(yīng)的索引
這篇文章主要介紹了python如何獲得list或numpy數(shù)組中最大元素對(duì)應(yīng)的索引,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11