基于生產(chǎn)監(jiān)控告警需求,需要對(duì)Java日志進(jìn)行解析,提取相關(guān)信息,作為告警通知消息的內(nèi)容部分。
創(chuàng)新互聯(lián)于2013年開(kāi)始,先為霍山等服務(wù)建站,霍山等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為霍山企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
具體怎么提取,提取哪些內(nèi)容呢?這里筆者分析了大量不同形態(tài)的生產(chǎn)日志,最后總結(jié)出4種形態(tài),如下,制定出以下提取邏輯。
上圖中,款選部分即為要提取的主要內(nèi)容,即異常發(fā)生時(shí)所在文件,代碼行,自定義異常相關(guān)描述,異常類型,異常描述,這里提取的相關(guān)說(shuō)明和異常描述將統(tǒng)一作為異常的詳細(xì)描述
類似形態(tài)1,如果沒(méi)有獨(dú)占一行的“異常類型”,那就取最后Caused by:后面的異常類型,及其描述
形態(tài)1,形態(tài)2不匹配的情況下,匹配形態(tài)3,該形態(tài)中,異常類型和描述是包含在自定義異常相關(guān)描述里面的
前三者都不匹配的情況下,匹配最后這種形態(tài)。沒(méi)有異常類型,僅日志級(jí)別“ERROR”可以標(biāo)識(shí)它是條異常日志。
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import re
log_list = [
'''
2021-10-18 09:22:41,079:ERROR http-nio-9330-exec-4 (DirectJDKLog.java:181) - Servlet.service() for servlet [dispatcherServlet] in context with path [/finance] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469) ~[?:1.8.0_202]
at java.util.TreeMap.put(TreeMap.java:552) ~[?:1.8.0_202]
''',
'''
2021-10-18 09:22:55,222:WARN kafka-async-consumer-2 (FeignClientsErrorDecoder.java:43) - read Exception failed!
com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: java.io.InputStreamReader@a3; line: 1, column: 0]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270) ~[jackson-databind-2.8.4.jar!/:2.8.4]
''',
'''
2021-10-18 09:22:52,975:ERROR [,] parallel-2 (AccessLogWebFilter.java:60) - [accessId=616ccc49ff642e00010a4e8c] 發(fā)生網(wǎng)關(guān)內(nèi)部錯(cuò)誤
org.springframework.web.server.ResponseStatusException: 504 GATEWAY_TIMEOUT "Response took longer than timeout: PT35S"; nested exception is org.springframework.cloud.gateway.support.TimeoutException: Response took longer than timeout: PT35S
at org.springframework.cloud.gateway.filter.NettyRoutingFilter.lambda$filter$5(NettyRoutingFilter.java:211) ~[spring-cloud-gateway-core-2.1.3.RELEASE.jar!/:2.1.3.RELEASE]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_202]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_202]
Caused by: org.springframework.cloud.gateway.support.TimeoutException: Response took longer than timeout: PT35S
at
''',
'''
2021-10-18 09:22:41,905:WARN http-nio-8080-exec-60 (VehicleOeImpl.java:1000) - 批量更新第三方價(jià)格失敗1---->
org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Query was empty
### The error may involve com.cmall.ec.webapp.maindata.web.dao.vehicleOe.ThirdPartyOeMapper.updateList-Inline
### The error may involve com.cmall.ec.webapp.maindata.web.dao.vehicleOe.ThirdPartyOeMapper.updateList-Inline
### The error occurred while setting parameters
### SQL:
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Query was empty
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Query was empty
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:91)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Query was empty
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:434)
... 70 more
''',
'''
2021-10-17 18:39:33,066:ERROR http-nio--exec-34 TID: fb93d345bc92afad0f771.3235. (DirectJDKLog.java:181) - Servlet.service() for servlet [dispatcherServlet] in context with path [/orders/seller] threw exception [Request processing failed; nested exception is com.cmall.commons.service.exception.HttpMessageException: [400]標(biāo)名為空] with root cause
Exception: 標(biāo)名為空
at com.cmall.commons.utils.Assert.fail(Assert.java:553) ~[icec-cloud-commons-0.4.5.jar!/:?]
at com.cmall.commons.utils.Assert.notBlank(Assert.java:112) ~[icec-cloud-commons-0.4.5.jar!/:?]
''',
'''
2021-10-18 09:22:23,849:ERROR http-nio--exec-2 TID: ed41cdfb8d5d4953ac.80. (DicountAssembler.java:266) - 查詢商品類優(yōu)惠結(jié)果失敗:DiscountProductRequest(companyId=IYl6MgdkiG9KoBMYUJo, userLoginId=5cad996c47bf5f7ccd, provinceGeoId=CN-43, cityGeoId=284)
feign.FeignException: status 500 reading DiscountProductClient#listDiscountProducts(DiscountProductRequest); content:
{"timestamp":,"status":500,"error":"Internal Server Error","exception":"com.cmall.commons.service.exception.HttpMessageException","message":"優(yōu)惠前的不含稅價(jià)格不能為空","path":"/discountPromotion/listDiscountProducts"}
at feign.FeignException.errorStatus(FeignException.java:62) ~[feign-core-9.3.1.jar!/:?]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_202]
''',
'''2021-10-18 09:22:23,849:ERROR [-] (DicountAssembler.java:266) - task supervisor threw an exception ....''',
'''
2021-10-18 09:13:13,940:ERROR kafka-async-consumer-9 (ConsumeSupport.java:104) - kafka消費(fèi)失敗, cid:616cca245cc0aead690, message:{"jsonMessageType":"com.cmall.ec.cloud.scheduletask.values.kafka.command.DistributedDelayCommand","id":"616cca24d52d1d000dd","usage":"EVENT","service":"schedule-task-service","topic":"prod-quote-command-delay","timeStamp":,"delayTimes":,"producerTaskId":"B","inquiryId":"B","resolveBatchId":"616cca019cf7bc","resolveIds":["616cca00d52d1d000138be9a"],"type":"AUTO","retryCount":2,"producer":"DistributedDelayCommand"}, cause:java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor4946.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.cmall.messagebus.exception.MessageBaseException: 系統(tǒng)自動(dòng)報(bào)價(jià)失敗-->org.springframework.dao.DuplicateKeyException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '616cca225cc0ae1d0d2' for key 'PRIMARY'
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '616cca225cc0ae1d0d2' for key 'PRIMARY'
; SQL []; Duplicate entry '616cca225cc0ae1d0d2' for key 'PRIMARY'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '616cca225cc0ae1d0d2' for key 'PRIMARY'
at com.cmall.ec.cloud.quotation.handler.QuotationAllocationHandler.intelligentAutoQuoteDispatcher(QuotationAllocationHandler.java:80)
... 9 more
''',
'''
2021-10-16 14:37:19,951:ERROR DiscoveryClient-1 (TimedSupervisorTask.java:79) - task supervisor threw an exception
java.lang.OutOfMemoryError: Java heap space
''',
'''2021-10-16 14:37:19,951:ERROR DiscoveryClient-1 (TimedSupervisorTask.java:79) - task supervisor threw an exception
java.lang.OutOfMemoryError: Java heap space
at''',
'''
2021-10-23 14:03:00,785:ERROR kafka-async-consumer-1 (ConsumeSupport.java:104) - kafka消費(fèi)失敗, cid:6173a593f90dce3fe, message:{"jsonMessageType":"com.cmall.ec.cloud.events.order.OrderSent","id":"6173a593cb769e0001402a3b","usage":"EVENT","service":"orders-service","topic":"prod-order","timeStamp":,"orderId":"S","shipmentType":"LOGISTICS","logisticsCompany":"快送","shipmentNum":"","shipGroupId":"6173ac35d00014c390c","remark":"","productStoreId":"SZQD0001","userLoginId":"5e0aa6649cf","username":"許慶杰","items":[{"productId":"00001","quantity":1}],"isLogisticsFeePayOnLine":true}, cause:java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor1909.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.baomidou.mybatisplus.exceptions.MybatisPlusException: Error: Cannot execute insertBatch Method. Cause
at com.baomidou.mybatisplus.service.impl.ServiceImpl.insertBatch(ServiceImpl.java:137)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
... 9 more
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error flushing statements. Cause: org.apache.ibatis.executor.BatchExecutorException: com.cmall.ec.cloud.service.dao.mapper.ShipFeeMapper.insert (batch index #1) failed. Cause: java.sql.BatchUpdateException: Duplicate entry 'SF' for key 'PRIMARY'
### Cause: org.apache.ibatis.executor.BatchExecutorException: com.cmall.ec.cloud.service.dao.mapper.ShipFeeMapper.insert (batch index #1) failed. Cause: java.sql.BatchUpdateException: Duplicate entry 'SF' for key 'PRIMARY'
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
... 51 more
Caused by: org.apache.ibatis.executor.BatchExecutorException: com.cmall.ec.cloud.service.dao.mapper.ShipFeeMapper.insert (batch index #1) failed. Cause: java.sql.BatchUpdateException: Duplicate entry 'SF' for key 'PRIMARY'
at java.lang.reflect.Method.invoke(Method.java:498)
... 52 more
Caused by: java.sql.BatchUpdateException: Duplicate entry 'SF' for key 'PRIMARY'
at sun.reflect.GeneratedConstructorAccessor1540.newInstance(Unknown Source)
... 61 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'SF' for key 'PRIMARY'
at sun.reflect.GeneratedConstructorAccessor1537.newInstance(Unknown Source)
... 71 more
''',
'''
2021-10-25 16:29:15,853:ERROR reactor-http-epoll-3 (CompositeLog.java:122) - 500 Server Error for HTTP POST "/job-service/api/registry"
reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response
''',
'''2021-11-06 07:09:04,781:WARN http-nio--exec-85 (AdminService.java:97) - 任務(wù)執(zhí)行失敗, code:500, reason:java.lang.OutOfMemoryError: Java heap space''',
'''2022-01-08 13:46:30,668:ERROR http-nio-9524-exec-5 (WaitSettleDealServiceImpl.java:147) - 添加退訂單到賬單失敗com.cmall.commons.service.exception.HttpMessageException: [411]退貨單添加失敗,該分組已經(jīng)結(jié)束對(duì)賬,無(wú)法添加退貨單'''
]
exception_match_pattern_list = [
':(ERROR|WARN) .+\s\(([^\s]+?\.java):(\d+)\)(.*)\n([^:\s>\u4e00-\u9fa5]*Exception|[^:\s>\u4e00-\u9fa5]*Error)(.*?)(\s+at\s|$)',
':(ERROR|WARN) .+\s\(([^\s]+?\.java):(\d+)\).*([^\n]*Caused by: )([^:\s>]*?Exception|[^:\s]*?Error)(.*?)(\s+at\s*|$)',
':(ERROR|WARN) .+\s\(([^\s]+?\.java):(\d+)\)([^\n]*?)([^:\s>\u4e00-\u9fa5]*Exception|[^:\s>\u4e00-\u9fa5]*Error)([^\n]*?)(\s+at\s|$)',
':(ERROR|WARN) .+\s\(([^\s]+?\.java):(\d+)\)(.*?)\n*?\s*?([^:\s>\u4e00-\u9fa5]*Exception|[^:\s>\u4e00-\u9fa5]*Error)*?(.*?)(\s+at\s|$)'
]
for log_index, log in enumerate(log_list):
flag = 0
for pattern_index, flag_pattern in enumerate(exception_match_pattern_list):
match_result = re.findall(flag_pattern, log, re.DOTALL)
if match_result:
print('匹配第%s個(gè)Pattern' % (pattern_index+1), '匹配結(jié)果:', match_result[0])
flag = 1
break
if not flag:
print('第%s條日志,不匹配任何正則表達(dá)式' % (log_index + 1))
提取效果
匹配第1個(gè)Pattern 匹配結(jié)果: ('ERROR', 'DirectJDKLog.java', '181', ' - Servlet.service() for servlet [dispatcherServlet] in context with path [/finance] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause', 'java.lang.NullPointerException', '', '\n\tat ')
匹配第1個(gè)Pattern 匹配結(jié)果: ('WARN', 'FeignClientsErrorDecoder.java', '43', ' - read Exception failed!', 'com.fasterxml.jackson.databind.JsonMappingException', ': No content to map due to end-of-input', '\n at ')
匹配第1個(gè)Pattern 匹配結(jié)果: ('ERROR', 'AccessLogWebFilter.java', '60', ' - [accessId=616ccc49ff642e00010a4e8c] 發(fā)生網(wǎng)關(guān)內(nèi)部錯(cuò)誤', 'org.springframework.web.server.ResponseStatusException', ': 504 GATEWAY_TIMEOUT "Response took longer than timeout: PT35S"; nested exception is org.springframework.cloud.gateway.support.TimeoutException: Response took longer than timeout: PT35S', '\n\tat ')
匹配第1個(gè)Pattern 匹配結(jié)果: ('WARN', 'VehicleOeImpl.java', '1000', ' - 批量更新第三方價(jià)格失敗1---->', 'org.springframework.jdbc.BadSqlGrammarException', ':\n### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Query was empty\n### The error may involve com.cmall.ec.webapp.maindata.web.dao.vehicleOe.ThirdPartyOeMapper.updateList-Inline\n### The error may involve com.cmall.ec.webapp.maindata.web.dao.vehicleOe.ThirdPartyOeMapper.updateList-Inline\n### The error occurred while setting parameters\n### SQL:\n### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Query was empty\n; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Query was empty', '\n\tat ')
匹配第1個(gè)Pattern 匹配結(jié)果: ('ERROR', 'DirectJDKLog.java', '181', ' - Servlet.service() for servlet [dispatcherServlet] in context with path [/orders/seller] threw exception [Request processing failed; nested exception is com.cmall.commons.service.exception.HttpMessageException: [400]標(biāo)名為空] with root cause', 'Exception', ': 標(biāo)名為空', '\n\tat ')
匹配第1個(gè)Pattern 匹配結(jié)果: ('ERROR', 'DicountAssembler.java', '266', ' - 查詢商品類優(yōu)惠結(jié)果失敗:DiscountProductRequest(companyId=IYl6MgdkiG9KoBMYUJo, userLoginId=5cad996c47bf5f7ccd, provinceGeoId=CN-43, cityGeoId=284)', 'feign.FeignException', ': status 500 reading DiscountProductClient#listDiscountProducts(DiscountProductRequest); content:\n{"timestamp":,"status":500,"error":"Internal Server Error","exception":"com.cmall.commons.service.exception.HttpMessageException","message":"優(yōu)惠前的不含稅價(jià)格不能為空","path":"/discountPromotion/listDiscountProducts"}', '\n\tat ')
匹配第4個(gè)Pattern 匹配結(jié)果: ('ERROR', 'DicountAssembler.java', '266', '', '', ' - task supervisor threw an exception ....', '')
匹配第2個(gè)Pattern 匹配結(jié)果: ('ERROR', 'ConsumeSupport.java', '104', 'Caused by: ', 'com.cmall.messagebus.exception.MessageBaseException', ": 系統(tǒng)自動(dòng)報(bào)價(jià)失敗-->org.springframework.dao.DuplicateKeyException:\n### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '616cca225cc0ae1d0d2' for key 'PRIMARY'\n### The error may involve defaultParameterMap\n### The error occurred while setting parameters\n### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '616cca225cc0ae1d0d2' for key 'PRIMARY'\n; SQL []; Duplicate entry '616cca225cc0ae1d0d2' for key 'PRIMARY'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '616cca225cc0ae1d0d2' for key 'PRIMARY'", '\n\tat ')
匹配第1個(gè)Pattern 匹配結(jié)果: ('ERROR', 'TimedSupervisorTask.java', '79', ' - task supervisor threw an exception', 'java.lang.OutOfMemoryError', ': Java heap space', '')
匹配第1個(gè)Pattern 匹配結(jié)果: ('ERROR', 'TimedSupervisorTask.java', '79', ' - task supervisor threw an exception', 'java.lang.OutOfMemoryError', ': Java heap space\nat', '')
匹配第2個(gè)Pattern 匹配結(jié)果: ('ERROR', 'ConsumeSupport.java', '104', 'Caused by: ', 'com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException', ": Duplicate entry 'SF' for key 'PRIMARY'", '\n\tat ')
匹配第1個(gè)Pattern 匹配結(jié)果: ('ERROR', 'CompositeLog.java', '122', ' - 500 Server Error for HTTP POST "/job-service/api/registry"', 'reactor.netty.http.client.PrematureCloseException', ': Connection prematurely closed BEFORE response', '')
匹配第3個(gè)Pattern 匹配結(jié)果: ('WARN', 'AdminService.java', '97', ' - 任務(wù)執(zhí)行失敗, code:500, reason:', 'java.lang.OutOfMemoryError', ': Java heap space', '')
匹配第3個(gè)Pattern 匹配結(jié)果: ('ERROR', 'WaitSettleDealServiceImpl.java', '147', ' - 添加退訂單到賬單失敗', 'com.cmall.commons.service.exception.HttpMessageException', ': [411]退貨單添加失敗,該分組已經(jīng)結(jié)束對(duì)賬,無(wú)法添加退貨單', '')