python 实现过滤出tomcat日志中含有ERROR 或Exception 的行并保存在另一个文件
遍历多个tomcat日志文件,找出含有ERROR 和Exception 的日志,并把该行日志输出到另一个文件中:(这里为了体现python模块导入的知识,所有建立了多个文件夹和模块)
项目结构:
consetting.py:
# 日志文件目录
F_PATH = r'C:\Users\shenping\PycharmProjects\Shenping_TEST\day_5\script\glive\logs'
# 错误日志存储目录
D_PATH = r'C:\Users\shenping\PycharmProjects\Shenping_TEST\day_5\script\glive\data'
rwfile.py:
def op_file(filename,content =None):
f = open(filename,'a+',encoding='utf-8')
f.seek(0)
if content:
f.writelines(content)
res = None
else:
res = f.readlines()
f.close()
return res
operationlog.py:
import re,os,datetime
from lib.rwfile import op_file # 遍历日志文件:
# 找出含有 error 或 exception 的行,按格式写入文件
def operation_log(filename,f_path,d_path):
index = 0
log_file = os.path.join(f_path, filename)
f = op_file(log_file)
lis = [] # 存储错误日志
for j in f:
index += 1
m = re.search('error',j,re.IGNORECASE)
n = re.search('exception',j,re.IGNORECASE)
if m or n:
data_file = os.path.join(d_path, str(datetime.date.today())+'_tomcat.log')
log_str = "文件名:"+filename+" 第"+str(index)+"行:"+j+'\n'
lis.append(log_str)
op_file(data_file,lis)
start.py:
# 启动文件
import os,sys cur_path = os.path.abspath(__file__)
base_dir = os.path.dirname(os.path.dirname(cur_path))
sys.path.insert(0,base_dir)
from conf.consetting import F_PATH,D_PATH
from lib.operationlog import operation_log log_name = os.listdir(F_PATH)
for x in log_name:
operation_log(x,F_PATH,D_PATH)
print('========================================华丽的分割线=========================================================')
运行后结果:
文件名:catalina.2017-10-21.log 第5行:21-Oct-2017 14:16:32.118 ERROR [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
文件名:catalina.2017-10-21.log 第9行:21-Oct-2017 14:16:32.119 ERROR [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [threadDeathWatcher-2-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
文件名:catalina.2017-10-21.log 第14行:21-Oct-2017 14:16:32.120 ERROR [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [logback-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
文件名:catalina.2017-10-21.log 第515行:21-Oct-2017 16:08:39.217 ERROR [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [glive-task-pool-1-thread-12] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
文件名:catalina.2017-10-22.log 第216行:22-Oct-2017 15:17:49.169 Exception [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
文件名:catalina.2017-10-22.log 第248行:22-Oct-2017 15:17:54.089 Exception [main] org.apache.tomcat.util.digester.SetPropertiesRule.begin [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'debug' to '' did not find a matching property.
文件名:catalina.2017-10-22.log 第249行:22-Oct-2017 15:17:54.113 Exception [main] org.apache.tomcat.util.digester.SetPropertiesRule.begin [SetPropertiesRule]{Server/Service/Engine/Host/Valve} Setting property 'resolveHosts' to 'false' did not find a matching property.
文件名:catalina.2017-10-23.log 第3行:23-Oct-2017 10:51:55.461 IllegalStateException [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [ROOT] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
文件名:catalina.2017-10-23.log 第4行:23-Oct-2017 10:51:55.462 ERROR [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [ROOT] registered the JDBC driver [com.mysql.fabric.jdbc.FabricMySQLDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
文件名:catalina.2017-10-23.log 第9行:23-Oct-2017 10:51:55.464 IllegalStateException [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [threadDeathWatcher-3-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
文件名:catalina.2017-10-23.log 第3356行:23-Oct-2017 21:47:50.795 IllegalStateException [logback-1] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load [ch.qos.logback.core.spi.ContextAwareBaseBeanINFO]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
文件名:catalina.2017-10-23.log 第3357行: java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [ch.qos.logback.core.spi.ContextAwareBaseBeanINFO]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
文件名:catalina.2017-10-23.log 第3398行:23-Oct-2017 21:47:50.800 ERROR [logback-1] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load [java.lang.ObjectBeanINFO]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
文件名:catalina.2017-10-23.log 第3399行: java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [java.lang.ObjectBeanINFO]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
文件名:catalina.2017-10-23.log 第3444行:23-Oct-2017 21:47:50.802 ERROR [logback-1] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load [java.lang.ObjectCustomizer]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
文件名:catalina.2017-10-23.log 第3445行: java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [java.lang.ObjectCustomizer]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
文件名:catalina.2017-10-23.log 第3491行: java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [ch.qos.logback.core.spi.ContextAwareBaseCustomizer]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
文件名:catalina.2017-10-23.log 第3532行: java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [ch.qos.logback.core.rolling.RollingPolicyBaseCustomizer]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
文件名:catalina.2017-10-23.log 第3570行: java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [ch.qos.logback.core.rolling.TimeBasedRollingPolicyCustomizer]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
文件名:catalina.2017-10-23.log 第3605行: java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [ch.qos.logback.core.rolling.helper.DateTokenConverter]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
文件名:catalina.2017-10-23.log 第3637行: java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [ch.qos.logback.core.status.INFOStatus]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
文件名:catalina.2017-10-23.log 第3667行: java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [ch.qos.logback.core.status.INFOStatus]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
文件名:catalina.2017-10-24.log 第5行:24-Oct-2017 08:02:51.428 IllegalStateException [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
文件名:catalina.2017-10-24.log 第520行:24-Oct-2017 14:42:09.350 Exception [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
文件名:catalina.2017-10-24.log 第670行:24-Oct-2017 14:42:09.374 Exception [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@e5d976d]) and a value of type [io.netty.util.internal.InternalThreadLocalMap] (value [io.netty.util.internal.InternalThreadLocalMap@b0bd2a4]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
文件名:catalina.2017-10-24.log 第682行:24-Oct-2017 14:42:09.376 Exception [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@55c7591]) and a value of type [io.grpc.Context] (value [io.grpc.Context@390c5a57]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
文件名:catalina.2017-10-24.log 第704行:24-Oct-2017 14:42:09.378 Exception [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@55c7591]) and a value of type [io.grpc.Context] (value [io.grpc.Context@390c5a57]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
python 实现过滤出tomcat日志中含有ERROR 或Exception 的行并保存在另一个文件的更多相关文章
- linux服务器上tomcat日志中的中文乱码
转: 修改tomcat应用日志默认编码格式 前言 今天开发跟我说tomcat日志中的中文不能正常显示,根据以往的经验,我觉得可能跟服务器的编码有关,于是尝试各种方法,但还是没能解决问题. 后来我突然想 ...
- linux日志中查找关键字、前几行、结尾几行,Linux的find用法示例
linux在日志中查找关键字.前几行.结尾几行,Linux的find用法示例 1.linux在日志中查找关键字.前几行.结尾几行 1.1查看日志 前 n行: 1.2查看日志 尾 n行: 1.3根据 关 ...
- 关于JBoss日志中的报错Exception in thread "AWT-EventQueue-0"的解决记录
一.前情提要 操作系统:Windows Server 2008 R2,JDK版本:1.6.0_45,应用容器:JBoss 4.2.3 GA.所部署的应用均为Web型项目,没有任何图形相关的项目. 二. ...
- python 合并两个文件并将合并内容保存在另一个文件中
简单地文件合并方法 思路如下: 分别读取两个文件中的内容,并将其保存在一个列表中,将列表通过join()函数转为字符,并将新字符保存在新的文件中. 其中,test1.txt中的内容为: test2.t ...
- Java中的Error和Exception
Exception和Error都是继承了Throwable类,在Java中只有Throwable类型的实例才可以被抛出(throw)或者捕获(catch),它是异常处理机制的基本组成类型. Excep ...
- 谈一谈Java中的Error和Exception
Error和Exception的联系 继承结构:Error和Exception都是继承于Throwable,RuntimeException继承自Exception. Error和RuntimeExc ...
- 配置好Nginx后,通过flume收集日志到hdfs(记得生成本地log时,不要生成一个文件,)
生成本地log最好生成多个文件放在一个文件夹里,特别多的时候一个小时一个文件 配置好Nginx后,通过flume收集日志到hdfs 可参考flume的文件 用flume的案例二 执行的注意点 avro ...
- linux如何在日志中查找关键字、前几行、结尾几行
如何使用命令行快速查看项目日志是每个开发人员必备技能,尤其在没有专门日志搜集系统的情况下,想要知道目前项目运行状态最好的办法就是打开log日志一瞅即明白. 复杂的到用时再查不晚,但是简单的还是有必要掌 ...
- 【转】python模块分析之logging日志(四)
[转]python模块分析之logging日志(四) python的logging模块是用来写日志的,是python的标准模块. 系列文章 python模块分析之random(一) python模块分 ...
随机推荐
- for of 与 for in 的区别
遍历数组通常使用for循环,ES5的话也可以使用forEach,ES5具有遍历数组功能的还有map.filter.some.every.reduce.reduceRight等,只不过他们的返回结果不一 ...
- 对象之int介绍
#Auther Bob #--*--conding:utf-8 --*-- #创建两个int的对象,age1和age2 age1 = 10 age2 = int(1) #查看对象的类 print(ty ...
- eclipse奇怪问题之端口占用记录
启动程序报端口占用(实际并没有启动占用端口的程序) 打开cmd查找占用端口的进程能查到,但又无法杀掉(命令和任务管理器都杀不掉),用了PCHunter(xuetr)结束进程后还会刷新还会重新出现 重启 ...
- 13-matlab图片转化
图片格式: 处理函数: rgb2gray() gray2rgb()
- Boost 库uuid 的使用
UUID 简介 通用唯一识别码(英语:Universally Unique Identifier,简称UUID)是一种软件建构的标准,亦为开放软件基金会组织在分布式计算环境领域的一部分. uuid 版 ...
- linux下安装oracle数据库详细教程
一.安装yum源 下载或拷贝RedHat的iso镜像到本地,比如 /repo/iso/ rhel-server-6.6-x86_64-dvd.iso 1.建立ISO文件存放目录(/repo/iso)和 ...
- Nginx下SSL证书设置和反向代理
上来就贴代码: server { listen ; server_name **.****.net; #填写绑定证书的域名 ssl on; ssl_certificate /opt/nginx-/co ...
- 白盒静态自动化测试工具:PMD使用指南
参考文献:http://www.oschina.net/p/pmd/http://www.cnblogs.com/flyme/archive/2011/09/09/2172548.htmlhttp:/ ...
- VS2015 python
http://pgqlife.info/2015/05/05/VS-Python/ 配置文档
- Linux编程规范
1)在使用C语言进行编程时,源文件都必须加---文件头 /******************************************************** *文件名:test.c *创 ...