python 合并当天tomcat异常日志 第二版

简介:

把找出的异常文件,归类到指定目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# !/usr/bin/env python
# coding:utf-8
import  os,sys,tempfile,time,datetime,re
import  string
from  smtplib  import  SMTP
from  email  import  MIMEText
from  email  import  Header
from  os.path  import  getsize
from  sys  import  exit
from  re  import  compile , IGNORECASE
now  =  datetime.datetime.now()
strdatetime1  =  now.strftime( "%Y%m%d" )
strdatetime  =  now.strftime( "%Y/%m/%d" )
tomcat_log  =  '/home/logs/'
last_position_logfile  =  '/home/back/test/last_position.txt'
#error_list_logfile = '/home/back/test/error_list.txt'
error_list_logfile1  =  '/home/back/test/error_list'
Date = now.strftime( "%d" )
error_list_logfile  =  error_list_logfile1  +  strdatetime1  +  ".txt"
print  error_list_logfile
errorfile = error_list_logfile
pattern  =  compile (r 'Exception|^\t+\bat\b' , IGNORECASE)
#fp = open(error_list_logfile, "w+")
def  writeToTxt(list_name, file_path):
     try :
         fp  =  open (file_path,  "a+" )
         for  item  in  list_name:
             fp.write( str (item)  +  "\n" )
         fp.close()
     except  IOError:
         print ( "fail to open file" )
 
 
# 分析文件找出异常的行
def  analysis_log( file ):
     #parent_file = os.path.dirname(file)
     error_list  =  []
     try :
         data  =  open ( file 'r' )
     except :
         exit()
     for  line  in  data:
         if  pattern.search(line):
             error_list.append( file )
                         #print error_list
                         #errors_list += 1
                         #print "%s" %file
                         break
         data.close()
         if  len (error_list) = = 1 :
             #return ''.join(error_list)
             writeToTxt(error_list,error_list_logfile)
         #return error_list
         #if errors_list == 1:
             #print "%s num is %d" %(file,errors_list)
     #print ''.join(errors_list)
 
def  walk(local_dir):
     for  root, dirs, files  in  os.walk(local_dir):
         for  filespath  in  files:
             # return filespath
             local_file  =  os.path.join(root, filespath)
             # print error_info2
             error_info  =  analysis_log(local_file)
def  walk_dir(home_dir):
     homedir = []
         for  root, dirs, files  in  os.walk(home_dir):
             for  dirname  in  dirs:
                         local_dir  =  os.path.join(root, dirname)
                         #print "Date is %s" %Date
                         result  =  string.find(local_dir,strdatetime)! = - 1
                         if  result:
                             #print local_dir.count('/')
                             if  local_dir.count( '/' ) = = 8 :
                                 homedir.append(local_dir)
         return  homedir
 
 
 
def  make_ex(errorfile):
     with  open (errorfile) as f:
         exfile  =  []
         exdict  =  {}
         for  in  f.readlines():
             expname  =  i.strip().split( '/' )[ 3 ]
             exname  =  i.strip().split( '/' )[ 8 ]
             # if expname not in exfile:
             #     exfile.append(exname)
             estr  =  i.strip()
             #print estr
             youpattern  =  re. compile ( '/(\w+)/(\w+)/(\w+)' )
             result  =  youpattern.match(estr)
             r1  =  result.group()
             r2  =  "ex/"  +  strdatetime1
             r3  =  exname
             rall  =  [r1, r2, r3]
             # exdict[r1]
             rexall  =  "/" .join(rall)  +  '/'
             #print rexall
             if  not  os.path.exists(rexall):
                 os.makedirs(rexall)
             os.system( 'cp -r %s %s'  %  (estr, rexall))
 
 
def  make_diff_ex(c):
     for  in  c:
         i = str (j)
         expname  =  i.strip().split( '/' )[ 3 ]
         exname  =  i.strip().split( '/' )[ 8 ]
         # if expname not in exfile:
         #     exfile.append(exname)
         estr  =  i.strip()
         #print estr
         youpattern  =  re. compile ( '/(\w+)/(\w+)/(\w+)' )
         result  =  youpattern.match(estr)
         if  result:
             r1  =  result.group()
             r2  =  "ex/"  +  strdatetime1
             r3  =  exname
             rall  =  [r1, r2, r3]
             # exdict[r1]
             rexall  =  "/" .join(rall)  +  '/'
             #print rexall
             if  not  os.path.exists(rexall):
                 os.makedirs(rexall)
             os.system( 'cp -r %s %s'  %  (estr, rexall))
         
 
 
 
 
 
'''
def temps(errorfile):
     tmpfile = tempfile.NamedTemporaryFile(delete=False)
     with open(errorfile,'rw') as f:
         readfile="\n".join(f.readlines)
         tmpfile.write(str(f.readlines()))
         return tmpfile
'''
def  temps(errorfile):
     tmpfile = []
     with  open (errorfile, 'r' ) as f:
         tmpfile.append(f.readlines())
         return  tmpfile
 
 
 
def  list_diff(a, b):
     ret  =  []
     for  in  a:
         if  not  in  b:
             ret.append(i)
     return  ret
 
if  __name__  = =  '__main__' :
     if  not  os.path.exists(errorfile):
         fp  =  open (error_list_logfile,  "w+" )
         tomcat_last_dir  =  walk_dir(tomcat_log)
         for  in  tomcat_last_dir:
             walk(i)
         make_ex(errorfile)
     else :
         a = temps(errorfile)
         fp  =  open (error_list_logfile,  "w+" )
         tomcat_last_dir  =  walk_dir(tomcat_log)
         for  in  tomcat_last_dir:
             walk(i)
         #tmpfiles = []
         b = temps(errorfile)
         c = list_diff(a,b)
         if  c:
             make_diff_ex(c)

本来想用tempfile模块的,还要研究下。




本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1915646,如需转载请自行联系原作者

相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
9月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
1680 5
|
Java 应用服务中间件 Linux
Tomcat运行日志字符错乱/项目启动时控制台日志乱码问题
总结: 通过以上几种方法,概括如下:指定编码格式、设置JVM的文件编码、修改控制台输出编码、修正JSP页面编码和设置过滤器。遵循这些步骤,你可以依次排查和解决Tomcat运行日志字符错乱及项目启动时控制台日志乱码问题。希望这些建议能对你的问题提供有效的解决方案。
2290 16
|
监控 Java 应用服务中间件
Tomcat log日志解析
理解和解析Tomcat日志文件对于诊断和解决Web应用中的问题至关重要。通过分析 `catalina.out`、`localhost.log`、`localhost_access_log.*.txt`、`manager.log`和 `host-manager.log`等日志文件,可以快速定位和解决问题,确保Tomcat服务器的稳定运行。掌握这些日志解析技巧,可以显著提高运维和开发效率。
1743 13
|
应用服务中间件
Tomcat日志中文乱码
Tomcat日志中文乱码
248 0
|
Java 应用服务中间件
解决IDEA tomcat控制台只有server日志
请注意,确保在调试或开发阶段使用更详细的日志级别(如 `DEBUG`或 `TRACE`),但在生产环境中应将其设置为更高的级别以减少日志量。
1145 0
|
XML 安全 Java
【日志框架整合】Slf4j、Log4j、Log4j2、Logback配置模板
本文介绍了Java日志框架的基本概念和使用方法,重点讨论了SLF4J、Log4j、Logback和Log4j2之间的关系及其性能对比。SLF4J作为一个日志抽象层,允许开发者使用统一的日志接口,而Log4j、Logback和Log4j2则是具体的日志实现框架。Log4j2在性能上优于Logback,推荐在新项目中使用。文章还详细说明了如何在Spring Boot项目中配置Log4j2和Logback,以及如何使用Lombok简化日志记录。最后,提供了一些日志配置的最佳实践,包括滚动日志、统一日志格式和提高日志性能的方法。
5039 32
【日志框架整合】Slf4j、Log4j、Log4j2、Logback配置模板
|
监控 安全 Apache
什么是Apache日志?为什么Apache日志分析很重要?
Apache是全球广泛使用的Web服务器软件,支持超过30%的活跃网站。它通过接收和处理HTTP请求,与后端服务器通信,返回响应并记录日志,确保网页请求的快速准确处理。Apache日志分为访问日志和错误日志,对提升用户体验、保障安全及优化性能至关重要。EventLog Analyzer等工具可有效管理和分析这些日志,增强Web服务的安全性和可靠性。
624 9
|
监控 容灾 算法
阿里云 SLS 多云日志接入最佳实践:链路、成本与高可用性优化
本文探讨了如何高效、经济且可靠地将海外应用与基础设施日志统一采集至阿里云日志服务(SLS),解决全球化业务扩展中的关键挑战。重点介绍了高性能日志采集Agent(iLogtail/LoongCollector)在海外场景的应用,推荐使用LoongCollector以获得更优的稳定性和网络容错能力。同时分析了多种网络接入方案,包括公网直连、全球加速优化、阿里云内网及专线/CEN/VPN接入等,并提供了成本优化策略和多目标发送配置指导,帮助企业构建稳定、低成本、高可用的全球日志系统。
1308 55

推荐镜像

更多