LoggingEventReader implements GenericStreamReader<LoggingEvent>, LoggingEventSchemaConstants { @Override public LoggingEvent read(XMLStreamReader reader) throws XMLStreamException { int type = reader.getEventType(); if(XMLStreamConstants.START_DOCUMENT == type) { do { reader.next(); type = reader.getEventType(); } while(type != XMLStreamConstants.START_ELEMENT); } if(XMLStreamConstants.START_ELEMENT == type && LOGGING_EVENT_NODE.equals(reader.getLocalName())) { LoggingEvent result = new LoggingEvent(); result.setLogger(StaxUtilities.readAttributeValue(reader, NAMESPACE_URI, LOGGER_ATTRIBUTE)); String levelStr = StaxUtilities.readAttributeValue(reader, NAMESPACE_URI, LEVEL_ATTRIBUTE); if("FATAL".equals(levelStr)) { levelStr = "ERROR"; } try { result.setLevel(LoggingEvent.Level.valueOf(levelStr)); } catch(IllegalArgumentException ex) { } String threadName = StaxUtilities.readAttributeValue(reader, NAMESPACE_URI, THREAD_NAME_ATTRIBUTE); Long threadId = null; try { String threadIdStr = StaxUtilities.readAttributeValue(reader, NAMESPACE_URI, THREAD_ID_ATTRIBUTE); if(threadIdStr != null) { threadId = Long.valueOf(threadIdStr); } } catch(NumberFormatException ex) { } String threadGroupName = StaxUtilities .readAttributeValue(reader, NAMESPACE_URI, THREAD_GROUP_NAME_ATTRIBUTE); Long threadGroupId = null; try { String idStr = StaxUtilities.readAttributeValue(reader, NAMESPACE_URI, THREAD_GROUP_ID_ATTRIBUTE); if(idStr != null) { threadGroupId = Long.valueOf(idStr); } } catch(NumberFormatException ex) { } if(threadName != null || threadId != null || threadGroupId != null || threadGroupName != null) { result.setThreadInfo(new ThreadInfo(threadId, threadName, threadGroupId, threadGroupName)); } String timeStamp = StaxUtilities.readAttributeValue(reader, NAMESPACE_URI, TIMESTAMP_ATTRIBUTE); try { result.setTimeStamp(Long.parseLong(timeStamp)); } catch(NumberFormatException e) { } reader.nextTag(); String messagePattern = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, MESSAGE_NODE); if(messagePattern != null) { result.setMessage(new Message(messagePattern)); } result.setNdc(readNdc(reader)); result.setThrowable(readThrowable(reader)); result.setCallStack(readLocationInfo(reader)); result.setMdc(readMdc(reader)); return result; } return null; }  @Override LoggingEvent read(XMLStreamReader reader);  }
@Test public void multiLineMessage() throws XMLStreamException { String eventString = "<log4j:event logger=\"de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass\" timestamp=\"1234567890000\" level=\"DEBUG\" thread=\"main\">\n" + "<log4j:message><![CDATA[Foo!]]></log4j:message>\n" + "<log4j:NDC><![CDATA[NDC1 NDC2 NDC with spaces...]]></log4j:NDC>\n" + "<log4j:throwable><![CDATA[java.lang.RuntimeException: Multi\n" + "line\n" + "message\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass.execute(Log4jSandbox.java:28)\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox.main(Log4jSandbox.java:51)\n" + "Caused by: java.lang.RuntimeException: Hi.\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass.foobar(Log4jSandbox.java:35)\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass.execute(Log4jSandbox.java:24)\n" + "\t... 1 more\n" + "]]></log4j:throwable>\n" + "<log4j:locationInfo class=\"de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass\" method=\"execute\" file=\"Log4jSandbox.java\" line=\"29\"/>\n" + "<log4j:properties>\n" + "<log4j:data name=\"key1\" value=\"value1\"/>\n" + "<log4j:data name=\"key2\" value=\"value2\"/>\n" + "</log4j:properties>\n" + "</log4j:event>"; LoggingEvent readEvent = read(eventString); logEvent(readEvent); assertEquals("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", readEvent.getLogger()); assertEquals((Long) 1_234_567_890_000L, readEvent.getTimeStamp()); assertEquals(LoggingEvent.Level.DEBUG, readEvent.getLevel()); assertEquals(new Message("Foo!"), readEvent.getMessage()); { Map<String, String> expectedMdc = new HashMap<>(); expectedMdc.put("key1", "value1"); expectedMdc.put("key2", "value2"); assertEquals(expectedMdc, readEvent.getMdc()); } { Message[] expectedNdc = { new Message("NDC1"), new Message("NDC2"), new Message("NDC"), new Message("with"), new Message("spaces..."), }; assertArrayEquals(expectedNdc, readEvent.getNdc()); } { ExtendedStackTraceElement[] expectedCallStack = { new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", "execute", "Log4jSandbox.java", 29), }; assertArrayEquals(expectedCallStack, readEvent.getCallStack()); } { ThreadInfo threadInfo = new ThreadInfo(); threadInfo.setName("main"); assertEquals(threadInfo, readEvent.getThreadInfo()); } { ThrowableInfo throwableInfo = new ThrowableInfo(); throwableInfo.setName("java.lang.RuntimeException"); throwableInfo.setMessage("Multi\nline\nmessage"); throwableInfo.setStackTrace(new ExtendedStackTraceElement[]{ new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", "execute", "Log4jSandbox.java", 28), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox", "main", "Log4jSandbox.java", 51), }); ThrowableInfo cause = new ThrowableInfo(); cause.setName("java.lang.RuntimeException"); cause.setMessage("Hi."); cause.setStackTrace(new ExtendedStackTraceElement[]{ new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", "foobar", "Log4jSandbox.java", 35), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", "execute", "Log4jSandbox.java", 24), }); cause.setOmittedElements(1); throwableInfo.setCause(cause); ThrowableInfo actual = readEvent.getThrowable(); if(logger.isInfoEnabled()) logger.info("Expected: {}", throwableInfo.toString()); if(logger.isInfoEnabled()) logger.info("Actual : {}", actual.toString()); assertEquals(throwableInfo, readEvent.getThrowable()); } }
@Test public void multiLineMessageWithEmptyLine() throws XMLStreamException { String eventString = "<log4j:event logger=\"de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass\" timestamp=\"1234567890000\" level=\"DEBUG\" thread=\"main\">\n" + "<log4j:message><![CDATA[Foo!]]></log4j:message>\n" + "<log4j:NDC><![CDATA[NDC1 NDC2 NDC with spaces...]]></log4j:NDC>\n" + "<log4j:throwable><![CDATA[java.lang.RuntimeException: Multi\n" + "line\n" + "message\n" + "\n" + "after empty line\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass.execute(Log4jSandbox.java:28)\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox.main(Log4jSandbox.java:51)\n" + "Caused by: java.lang.RuntimeException: Hi.\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass.foobar(Log4jSandbox.java:35)\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass.execute(Log4jSandbox.java:24)\n" + "\t... 1 more\n" + "]]></log4j:throwable>\n" + "<log4j:locationInfo class=\"de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass\" method=\"execute\" file=\"Log4jSandbox.java\" line=\"29\"/>\n" + "<log4j:properties>\n" + "<log4j:data name=\"key1\" value=\"value1\"/>\n" + "<log4j:data name=\"key2\" value=\"value2\"/>\n" + "</log4j:properties>\n" + "</log4j:event>"; LoggingEvent readEvent = read(eventString); logEvent(readEvent); assertEquals("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", readEvent.getLogger()); assertEquals((Long) 1_234_567_890_000L, readEvent.getTimeStamp()); assertEquals(LoggingEvent.Level.DEBUG, readEvent.getLevel()); assertEquals(new Message("Foo!"), readEvent.getMessage()); { Map<String, String> expectedMdc = new HashMap<>(); expectedMdc.put("key1", "value1"); expectedMdc.put("key2", "value2"); assertEquals(expectedMdc, readEvent.getMdc()); } { Message[] expectedNdc = { new Message("NDC1"), new Message("NDC2"), new Message("NDC"), new Message("with"), new Message("spaces..."), }; assertArrayEquals(expectedNdc, readEvent.getNdc()); } { ExtendedStackTraceElement[] expectedCallStack = { new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", "execute", "Log4jSandbox.java", 29), }; assertArrayEquals(expectedCallStack, readEvent.getCallStack()); } { ThreadInfo threadInfo = new ThreadInfo(); threadInfo.setName("main"); assertEquals(threadInfo, readEvent.getThreadInfo()); } { ThrowableInfo throwableInfo = new ThrowableInfo(); throwableInfo.setName("java.lang.RuntimeException"); throwableInfo.setMessage("Multi\nline\nmessage\n\nafter empty line"); throwableInfo.setStackTrace(new ExtendedStackTraceElement[]{ new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", "execute", "Log4jSandbox.java", 28), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox", "main", "Log4jSandbox.java", 51), }); ThrowableInfo cause = new ThrowableInfo(); cause.setName("java.lang.RuntimeException"); cause.setMessage("Hi."); cause.setStackTrace(new ExtendedStackTraceElement[]{ new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", "foobar", "Log4jSandbox.java", 35), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", "execute", "Log4jSandbox.java", 24), }); cause.setOmittedElements(1); throwableInfo.setCause(cause); ThrowableInfo actual = readEvent.getThrowable(); if(logger.isInfoEnabled()) logger.info("Expected: {}", throwableInfo.toString()); if(logger.isInfoEnabled()) logger.info("Actual : {}", actual.toString()); assertEquals(throwableInfo, readEvent.getThrowable()); } }
@Test public void full() throws XMLStreamException { String eventString = "<log4j:event logger=\"de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass\" timestamp=\"1234567890000\" level=\"DEBUG\" thread=\"main\">\n" + "<log4j:message><![CDATA[Foo!]]></log4j:message>\n" + "<log4j:NDC><![CDATA[NDC1 NDC2]]></log4j:NDC>\n" + "<log4j:throwable><![CDATA[java.lang.RuntimeException: Hello\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass.execute(Log4jSandbox.java:18)\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox.main(Log4jSandbox.java:38)\n" + "Caused by: java.lang.RuntimeException: Hi.\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass.foobar(Log4jSandbox.java:24)\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass.execute(Log4jSandbox.java:14)\n" + "\t... 1 more\n" + "]]></log4j:throwable>\n" + "<log4j:locationInfo class=\"de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass\" method=\"execute\" file=\"Log4jSandbox.java\" line=\"18\"/>\n" + "<log4j:properties>\n" + "<log4j:data name=\"key1\" value=\"value1\"/>\n" + "<log4j:data name=\"key2\" value=\"value2\"/>\n" + "</log4j:properties>\n" + "</log4j:event>"; LoggingEvent readEvent = read(eventString); logEvent(readEvent); assertEquals("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", readEvent.getLogger()); assertEquals((Long) 1_234_567_890_000L, readEvent.getTimeStamp()); assertEquals(LoggingEvent.Level.DEBUG, readEvent.getLevel()); assertEquals(new Message("Foo!"), readEvent.getMessage()); { Map<String, String> expectedMdc = new HashMap<>(); expectedMdc.put("key1", "value1"); expectedMdc.put("key2", "value2"); assertEquals(expectedMdc, readEvent.getMdc()); } { Message[] expectedNdc = { new Message("NDC1"), new Message("NDC2"), }; assertArrayEquals(expectedNdc, readEvent.getNdc()); } { ExtendedStackTraceElement[] expectedCallStack = { new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", "execute", "Log4jSandbox.java", 18), }; assertArrayEquals(expectedCallStack, readEvent.getCallStack()); } { ThreadInfo threadInfo = new ThreadInfo(); threadInfo.setName("main"); assertEquals(threadInfo, readEvent.getThreadInfo()); } { ThrowableInfo throwableInfo = new ThrowableInfo(); throwableInfo.setName("java.lang.RuntimeException"); throwableInfo.setMessage("Hello"); throwableInfo.setStackTrace(new ExtendedStackTraceElement[]{ new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", "execute", "Log4jSandbox.java", 18), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox", "main", "Log4jSandbox.java", 38), }); ThrowableInfo cause = new ThrowableInfo(); cause.setName("java.lang.RuntimeException"); cause.setMessage("Hi."); cause.setStackTrace(new ExtendedStackTraceElement[]{ new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", "foobar", "Log4jSandbox.java", 24), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox$InnerClass", "execute", "Log4jSandbox.java", 14), }); cause.setOmittedElements(1); throwableInfo.setCause(cause); ThrowableInfo actual = readEvent.getThrowable(); if(logger.isInfoEnabled()) logger.info("Expected: {}", throwableInfo.toString()); if(logger.isInfoEnabled()) logger.info("Actual : {}", actual.toString()); assertEquals(throwableInfo, readEvent.getThrowable()); } }
@Test public void singleThrowable() throws XMLStreamException { String eventString = "<log4j:event logger=\"de.huxhorn.lilith.sandbox.Log4jSandbox\" timestamp=\"1234567890000\" level=\"DEBUG\" thread=\"main\">\n" + "<log4j:message><![CDATA[Foobar!]]></log4j:message>\n" + "<log4j:NDC><![CDATA[NDC1 NDC2 NDC with spaces...]]></log4j:NDC>\n" + "<log4j:throwable><![CDATA[java.lang.Throwable\n" + "\tat de.huxhorn.lilith.sandbox.Log4jSandbox.main(Log4jSandbox.java:37)\n" + "]]></log4j:throwable>\n" + "<log4j:locationInfo class=\"de.huxhorn.lilith.sandbox.Log4jSandbox\" method=\"main\" file=\"Log4jSandbox.java\" line=\"37\"/>\n" + "<log4j:properties>\n" + "<log4j:data name=\"key1\" value=\"value1\"/>\n" + "<log4j:data name=\"key2\" value=\"value2\"/>\n" + "</log4j:properties>\n" + "</log4j:event>"; LoggingEvent readEvent = read(eventString); logEvent(readEvent); assertEquals("de.huxhorn.lilith.sandbox.Log4jSandbox", readEvent.getLogger()); assertEquals((Long) 1_234_567_890_000L, readEvent.getTimeStamp()); assertEquals(LoggingEvent.Level.DEBUG, readEvent.getLevel()); assertEquals(new Message("Foobar!"), readEvent.getMessage()); { Map<String, String> expectedMdc = new HashMap<>(); expectedMdc.put("key1", "value1"); expectedMdc.put("key2", "value2"); assertEquals(expectedMdc, readEvent.getMdc()); } { Message[] expectedNdc = { new Message("NDC1"), new Message("NDC2"), new Message("NDC"), new Message("with"), new Message("spaces..."), }; assertArrayEquals(expectedNdc, readEvent.getNdc()); } { ExtendedStackTraceElement[] expectedCallStack = { new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox", "main", "Log4jSandbox.java", 37), }; assertArrayEquals(expectedCallStack, readEvent.getCallStack()); } { ThreadInfo threadInfo = new ThreadInfo(); threadInfo.setName("main"); assertEquals(threadInfo, readEvent.getThreadInfo()); } { ThrowableInfo throwableInfo = new ThrowableInfo(); throwableInfo.setName("java.lang.Throwable"); throwableInfo.setMessage(null); throwableInfo.setStackTrace(new ExtendedStackTraceElement[]{ new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.Log4jSandbox", "main", "Log4jSandbox.java", 37), }); ThrowableInfo actual = readEvent.getThrowable(); if(logger.isInfoEnabled()) logger.info("Expected: {}", throwableInfo.toString()); if(logger.isInfoEnabled()) logger.info("Actual : {}", actual.toString()); assertEquals(throwableInfo, actual); } }