Python 过滤错误log并导出的实例
作者:小喜_ww 时间:2023-10-21 10:18:36
前言:
测试过程中获取App相关log后,如何快速找出crash的部分,并导出到新的文件呢?
感兴趣的话,继续往下看吧~
思路:遍历多个日志文件,找出含有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
来源:https://www.jianshu.com/p/f0d68772da41
标签:Python,过滤log,导出
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入理解python中的闭包和装饰器
2023-03-20 06:27:48
![](https://img.aspxhome.com/file/2023/6/79766_0s.jpg)
python爬虫之selenium库的安装及使用教程
2021-04-22 20:20:46
![](https://img.aspxhome.com/file/2023/8/78678_0s.png)
Python笔记之facade模式
2023-10-18 22:59:50
![](https://img.aspxhome.com/file/2023/3/134983_0s.jpg)
深入分析python中整型不会溢出问题
2022-11-20 01:19:13
解决pycharm下载库时出现Failed to install package的问题
2021-07-30 14:15:49
![](https://img.aspxhome.com/file/2023/0/72890_0s.png)
php usort 使用用户自定义的比较函数对二维数组中的值进行排序
2023-11-21 23:08:28
Python设计模式之工厂方法模式实例详解
2021-02-26 05:40:59
![](https://img.aspxhome.com/file/2023/9/76129_0s.png)
python中heapq堆排算法的实现
2022-10-13 23:11:40
线上MySQL的自增id用尽怎么办
2024-01-22 01:39:38
![](https://img.aspxhome.com/file/2023/7/114007_0s.jpg)
Python中的推导式使用详解
2022-10-30 00:42:49
图文详解mysql中with...as用法
2024-01-27 20:52:43
![](https://img.aspxhome.com/file/2023/2/128032_0s.png)
Go web入门Go pongo2模板引擎
2023-07-22 22:16:43
![](https://img.aspxhome.com/file/2023/7/99647_0s.png)
Python中Playwright 与 pyunit 结合使用详解
2022-04-30 10:42:42
QQ邮箱的一个文本编辑器代码
2024-04-29 14:07:13
TensorFlow神经网络创建多层感知机MNIST数据集
2022-03-29 20:09:19
Python中max函数用于二维列表的实例
2022-07-04 15:49:55
js 禁用只读文本框获得焦点时的退格键
2024-04-19 10:25:41
深入研究ASP中的Session
2007-09-13 12:37:00
php5.3 不支持 session_register() 此函数已启用的解决方法
2023-11-16 01:59:39
Swoole webSocket客服IM消息系统方案解析
2023-06-14 20:17:22
![](https://img.aspxhome.com/file/2023/7/55507_0s.png)