我想解析一個log4j生成的xml日誌。在xml中是一個帶有throwable的節點(如果有的話)。這個(多行,標籤)文本被封裝在一個CDATA標籤中。Groovys XmlParser忽略CDATA CR/CL
這是整個文件的摘錄:
<log4j:event logger="org.codehaus.groovy.grails.web.errors.GrailsExceptionResolver" timestamp="1330083921521" level="ERROR" thread="http-8080-1">
<log4j:message><![CDATA[Exception occurred when processing request: [GET] /test/log/show
Stacktrace follows:]]></log4j:message>
<log4j:throwable><![CDATA[org.xml.sax.SAXParseException: XML document structures must start and end within the same entity.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1231)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at test.LogController$_closure2.doCall(LogController.groovy:21)
at test.LogController$_closure2.doCall(LogController.groovy)
at java.lang.Thread.run(Thread.java:662)
]]></log4j:throwable>
</log4j:event>
我groovys XmlParser的解析它:
def parser = new XmlParser(false, false).parse(new File("stack.log"))
return parser.'log4j:event'.collect { l ->
LogEntry entry = new LogEntry()
entry.with {
level = l.'@level'
message = l.'log4j:message'.text()
thread = l.'@thread'
logger = l.'@logger'
timestamp = new Date(l.'@timestamp' as long)
throwable = l.'log4j:throwable'?.text() ?: ''
}
entry
}
的 '拋出' 字段包含的所有文字,但沒有CR/LF。
有人知道如何應對嗎?
謝謝您的優先...
你有沒有一點XML的例子? – 2012-02-24 14:47:33
我編輯帖子以顯示一個小例子... – matcauthon 2012-02-24 16:27:07