I'm trying to parse some log file, which each line starts with time stamp, such as:
[11/16/18 16:40:04:097 EST]
If there isn't any error in the log, then every line will have the same starting pattern. However, if some error occurs, then the whole error stack will be printed with the time stamp as following:
[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:
What I want to do is extra the whole error stack, for example, the input is:
[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)
The ideal out put should be:
[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)
Tried the following and failed, if you can let me know what's wrong with my code, it would be great.
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