我试图解析一些日志文件,每行都以时间戳开头,例如:
[11/16/18 16:40:04:097 EST]
如果日志中没有任何错误,那么每一行的起始模式都相同。但是,如果发生某些错误,则整个错误堆栈将打印时间戳,如下所示:
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
我想做的是额外的整个错误堆栈,例如,输入是:
[11/16/18 16:40:04:098 EST] 000000ae CommandLogger 2 PerfLog <entry operation="Command : com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl" parameters="@releaseID=9.0
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
[11/16/18 16:40:04:101 EST] 000000ae SystemErr R
[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.
at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)
理想的输出应该是:
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.
at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)
尝试了以下操作但失败了,如果你能让我知道我的代码有什么问题,那就太好了
import re, sys
if len(sys.argv) > 1:
with open(sys.argv[1]) as f:
text = f.read()
else:
text = sys.stdin.read()
p_start = r'^\[\d{2}/.*'
p_end = r'^\[\d{2}/.*'
pattern = r'{p0}(?!.*{p0})(?:.*?{p1}|.*)'.format(p0=p_start, p1=p_end)
error_no_match = 'No Match'
matches = re.findall(pattern, text, flags=re.M|re.DOTALL)
if matches:
for match in matches:
print 'match:', match
print len(matches)
else:
print error_no_match
在将整个文件读入变量
text
时,可以使用见regex demo。请注意,如果文本包含CRLF结尾,则需要将
\n
替换为\r?\n
(其中CR是可选的)细节
re.M
修饰符使^
在行首匹配^
-行首\[
-a[
字符\d{2}/
-2位数字和/
字符.*
-行的其余部分(?:\n(?!\[\d{2}/).*)+
-一次或多次重复\n(?!\[\d{2}/)
-一个LF符号(如果可能有CRLF结尾,则使用\r?\n
),后面没有[
和两个数字和/
.*
-行的其余部分李>Python demo:
输出:
相关问题 更多 >
编程相关推荐