您好,登錄后才能下訂單哦!
這篇文章主要介紹Python如何過濾錯誤log并導出,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
思路:遍歷多個日志文件,找出含有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并導出”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。