遍历多个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 的行并保存在另一个文件的更多相关文章

  1. linux服务器上tomcat日志中的中文乱码

    转: 修改tomcat应用日志默认编码格式 前言 今天开发跟我说tomcat日志中的中文不能正常显示,根据以往的经验,我觉得可能跟服务器的编码有关,于是尝试各种方法,但还是没能解决问题. 后来我突然想 ...

  2. linux日志中查找关键字、前几行、结尾几行,Linux的find用法示例

    linux在日志中查找关键字.前几行.结尾几行,Linux的find用法示例 1.linux在日志中查找关键字.前几行.结尾几行 1.1查看日志 前 n行: 1.2查看日志 尾 n行: 1.3根据 关 ...

  3. 关于JBoss日志中的报错Exception in thread "AWT-EventQueue-0"的解决记录

    一.前情提要 操作系统:Windows Server 2008 R2,JDK版本:1.6.0_45,应用容器:JBoss 4.2.3 GA.所部署的应用均为Web型项目,没有任何图形相关的项目. 二. ...

  4. python 合并两个文件并将合并内容保存在另一个文件中

    简单地文件合并方法 思路如下: 分别读取两个文件中的内容,并将其保存在一个列表中,将列表通过join()函数转为字符,并将新字符保存在新的文件中. 其中,test1.txt中的内容为: test2.t ...

  5. Java中的Error和Exception

    Exception和Error都是继承了Throwable类,在Java中只有Throwable类型的实例才可以被抛出(throw)或者捕获(catch),它是异常处理机制的基本组成类型. Excep ...

  6. 谈一谈Java中的Error和Exception

    Error和Exception的联系 继承结构:Error和Exception都是继承于Throwable,RuntimeException继承自Exception. Error和RuntimeExc ...

  7. 配置好Nginx后,通过flume收集日志到hdfs(记得生成本地log时,不要生成一个文件,)

    生成本地log最好生成多个文件放在一个文件夹里,特别多的时候一个小时一个文件 配置好Nginx后,通过flume收集日志到hdfs 可参考flume的文件 用flume的案例二 执行的注意点 avro ...

  8. linux如何在日志中查找关键字、前几行、结尾几行

    如何使用命令行快速查看项目日志是每个开发人员必备技能,尤其在没有专门日志搜集系统的情况下,想要知道目前项目运行状态最好的办法就是打开log日志一瞅即明白. 复杂的到用时再查不晚,但是简单的还是有必要掌 ...

  9. 【转】python模块分析之logging日志(四)

    [转]python模块分析之logging日志(四) python的logging模块是用来写日志的,是python的标准模块. 系列文章 python模块分析之random(一) python模块分 ...

随机推荐

  1. 【校招面试 之 C/C++】第28题 C++ 内存泄漏的检查

    1.memwatch的使用 (1)首先去官网上下载源码: http://www.linkdata.se/sourcecode/memwatch/ 解压得到memwatch.c以及memwatch.h两 ...

  2. Spring框架的AOP技术之通知类型

    1. 通知类型 * @Before -- 前置通知 * @AfterReturing -- 后置通知 * @Around -- 环绕通知(目标对象方法默认不执行的,需要手动执行) * @After - ...

  3. windows下用tcc编译Lua

    脚本来源:Demon's Blog,http://demon.tw/software/compile-lua-with-tcc.html 版权归原作者所有 使用方法: 1.下载tcc编译器,本文解压目 ...

  4. Linux下使用openssl生成证书

    利用OpenSSL生成库和命令程序,在生成的命令程序中包括对加/解密算法的测试,openssl程序,ca程序.利用openssl,ca可生成用于C/S模式的证书文件以及CA文件. 参考:http:// ...

  5. PAT 1035 插入与归并(25)(代码+思路+测试点分析)

    1035 插入与归并(25 分) 根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到 ...

  6. Python使用wxPython、py2exe编写桌面程序-乾颐堂

    Python是支持可视化编程,即编写gui程序,你可以用它来编写自己喜欢的桌面程序.使用wxPython来做界面非常的简单,只是不能像C#一样拖动控件,需要自行写代码布局.在完成编写之后,由于直接的p ...

  7. The server is busy, please refresh

  8. Spring boot 默认静态资源路径与手动配置访问路径的方法

    这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下   在application.propertis中配置 ##端口号 ...

  9. HTML <img> 标签的 alt 属性

    定义和用法 alt 属性是一个必需的属性,它规定在图像无法显示时的替代文本. 假设由于下列原因用户无法查看图像,alt 属性可以为图像提供替代的信息: 网速太慢 src 属性中的错误 浏览器禁用图像 ...

  10. 项目解析1、登录验证用户是否存在 储备知识 Python 之 decorator装饰器

    下面是我对 装饰器 这一小节的总结, 以及自己的理解. 注:[本文中的代码参考上述教程] 很多时候我会把Python的很多语法与C++相融合,在C++中,函数的名称即为函数的地址,我们可以通过定义成为 ...