这篇文章主要介绍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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。