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 || !RECORD_NODE.equals(reader.getLocalName())); } if(XMLStreamConstants.START_ELEMENT == type && RECORD_NODE.equals(reader.getLocalName())) { reader.nextTag(); LoggingEvent result = new LoggingEvent(); String dateStr = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, DATE_NODE); String millisStr = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, MILLIS_NODE); Long timeStamp = null; if(millisStr != null) { try { timeStamp = Long.parseLong(millisStr); } catch(NumberFormatException ex) { } } if(timeStamp == null && dateStr != null) { if(logger.isInfoEnabled()) logger.info("Parsing date hasn't been implemented since millis is mandatory in Schema."); } result.setTimeStamp(timeStamp); String sequenceStr = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, SEQUENCE_NODE); try { result.setSequenceNumber(Long.parseLong(sequenceStr)); } catch(NumberFormatException ex) { } String loggerStr = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, LOGGER_NODE); result.setLogger(loggerStr); result.setLevel(resolveLevel(StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, LEVEL_NODE))); String classStr = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, CLASS_NODE); String methodStr = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, METHOD_NODE); if(classStr != null || methodStr != null) { ExtendedStackTraceElement[] callStack = { new ExtendedStackTraceElement(classStr, methodStr, null, -1), }; result.setCallStack(callStack); } String threadStr = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, THREAD_NODE); if(threadStr != null) { try { long threadId = Long.parseLong(threadStr); result.setThreadInfo(new ThreadInfo(threadId, null, null, null)); } catch(NumberFormatException ex) { } } String messageStr = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, MESSAGE_NODE); if(messageStr != null) { result.setMessage(new Message(messageStr)); } { String keyStr = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, KEY_NODE); String catalogStr = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, CATALOG_NODE); List<String> paramList = new ArrayList<>(); for(;;) { String paramStr = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, PARAM_NODE); if(paramStr == null) { break; } paramList.add(paramStr); } if(keyStr != null || catalogStr != null || !paramList.isEmpty()) { if(logger.isInfoEnabled()) logger.info("Ignoring the following message info: key={}, catalog={}, params={}", keyStr, catalogStr, paramList); } } result.setThrowable(readThrowableInfo(reader)); reader.require(XMLStreamConstants.END_ELEMENT, null, RECORD_NODE); for(;;) { reader.next(); type = reader.getEventType(); if(type == XMLStreamConstants.END_DOCUMENT) { break; } if(type == XMLStreamConstants.START_ELEMENT) { break; } } return result; } return null; }  @Override LoggingEvent read(XMLStreamReader reader);  }
@Test public void full() throws XMLStreamException, IOException { String eventString = "<record>\n" + " <date>2009-03-20T14:06:45</date>\n" + " <millis>1234567890000</millis>\n" + " <sequence>2</sequence>\n" + " <logger>de.huxhorn.lilith.sandbox.JulSandbox$InnerClass</logger>\n" + " <level>WARNING</level>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox$InnerClass</class>\n" + " <method>execute</method>\n" + " <thread>10</thread>\n" + " <message>Foo!</message>\n" + " <exception>\n" + " <message>java.lang.RuntimeException: Exception</message>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox</class>\n" + " <method>createRuntimeException</method>\n" + " <line>27</line>\n" + " </frame>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox</class>\n" + " <method>access$000</method>\n" + " <line>6</line>\n" + " </frame>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox$InnerClass</class>\n" + " <method>execute</method>\n" + " <line>14</line>\n" + " </frame>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox</class>\n" + " <method>main</method>\n" + " <line>47</line>\n" + " </frame>\n" + " </exception>\n" + "</record>"; LoggingEvent readEvent = read(eventString); logEvent(readEvent); assertEquals("de.huxhorn.lilith.sandbox.JulSandbox$InnerClass", readEvent.getLogger()); assertEquals((Long) 1_234_567_890_000L, readEvent.getTimeStamp()); assertEquals(LoggingEvent.Level.WARN, readEvent.getLevel()); assertEquals(new Message("Foo!"), readEvent.getMessage()); { ExtendedStackTraceElement[] expectedCallStack = { new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox$InnerClass", "execute", null, -1), }; assertArrayEquals(expectedCallStack, readEvent.getCallStack()); } { ThreadInfo threadInfo = new ThreadInfo(); threadInfo.setId(10L); assertEquals(threadInfo, readEvent.getThreadInfo()); } { ThrowableInfo throwableInfo = new ThrowableInfo(); throwableInfo.setName("java.lang.RuntimeException"); throwableInfo.setMessage("Exception"); throwableInfo.setStackTrace(new ExtendedStackTraceElement[]{ new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox", "createRuntimeException", null, 27), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox", "access$000", null, 6), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox$InnerClass", "execute", null, 14), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox", "main", null, 47), }); 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 fullWith3rdPartyLevel() throws XMLStreamException, IOException { String eventString = "<record>\n" + " <date>2009-03-20T14:06:45</date>\n" + " <millis>1234567890000</millis>\n" + " <sequence>2</sequence>\n" + " <logger>de.huxhorn.lilith.sandbox.JulSandbox$InnerClass</logger>\n" + " <level>" + (Level.WARNING.intValue() + 1) + "</level>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox$InnerClass</class>\n" + " <method>execute</method>\n" + " <thread>10</thread>\n" + " <message>Foo!</message>\n" + " <exception>\n" + " <message>java.lang.RuntimeException: Exception</message>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox</class>\n" + " <method>createRuntimeException</method>\n" + " <line>27</line>\n" + " </frame>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox</class>\n" + " <method>access$000</method>\n" + " <line>6</line>\n" + " </frame>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox$InnerClass</class>\n" + " <method>execute</method>\n" + " <line>14</line>\n" + " </frame>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox</class>\n" + " <method>main</method>\n" + " <line>47</line>\n" + " </frame>\n" + " </exception>\n" + "</record>"; LoggingEvent readEvent = read(eventString); logEvent(readEvent); assertEquals("de.huxhorn.lilith.sandbox.JulSandbox$InnerClass", readEvent.getLogger()); assertEquals((Long) 1_234_567_890_000L, readEvent.getTimeStamp()); assertEquals(LoggingEvent.Level.ERROR, readEvent.getLevel()); assertEquals(new Message("Foo!"), readEvent.getMessage()); { ExtendedStackTraceElement[] expectedCallStack = { new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox$InnerClass", "execute", null, -1), }; assertArrayEquals(expectedCallStack, readEvent.getCallStack()); } { ThreadInfo threadInfo = new ThreadInfo(); threadInfo.setId(10L); assertEquals(threadInfo, readEvent.getThreadInfo()); } { ThrowableInfo throwableInfo = new ThrowableInfo(); throwableInfo.setName("java.lang.RuntimeException"); throwableInfo.setMessage("Exception"); throwableInfo.setStackTrace(new ExtendedStackTraceElement[]{ new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox", "createRuntimeException", null, 27), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox", "access$000", null, 6), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox$InnerClass", "execute", null, 14), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox", "main", null, 47), }); 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 fullWithoutExceptionMessage() throws XMLStreamException, IOException { String eventString = "<record>\n" + " <date>2009-03-20T14:06:45</date>\n" + " <millis>1234567890000</millis>\n" + " <sequence>2</sequence>\n" + " <logger>de.huxhorn.lilith.sandbox.JulSandbox$InnerClass</logger>\n" + " <level>WARNING</level>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox$InnerClass</class>\n" + " <method>execute</method>\n" + " <thread>10</thread>\n" + " <message>Foo!</message>\n" + " <exception>\n" + " <message>java.lang.RuntimeException</message>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox</class>\n" + " <method>createRuntimeException</method>\n" + " <line>27</line>\n" + " </frame>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox</class>\n" + " <method>access$000</method>\n" + " <line>6</line>\n" + " </frame>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox$InnerClass</class>\n" + " <method>execute</method>\n" + " <line>14</line>\n" + " </frame>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox</class>\n" + " <method>main</method>\n" + " <line>47</line>\n" + " </frame>\n" + " </exception>\n" + "</record>"; LoggingEvent readEvent = read(eventString); logEvent(readEvent); assertEquals("de.huxhorn.lilith.sandbox.JulSandbox$InnerClass", readEvent.getLogger()); assertEquals((Long) 1_234_567_890_000L, readEvent.getTimeStamp()); assertEquals(LoggingEvent.Level.WARN, readEvent.getLevel()); assertEquals(new Message("Foo!"), readEvent.getMessage()); { ExtendedStackTraceElement[] expectedCallStack = { new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox$InnerClass", "execute", null, -1), }; assertArrayEquals(expectedCallStack, readEvent.getCallStack()); } { ThreadInfo threadInfo = new ThreadInfo(); threadInfo.setId(10L); assertEquals(threadInfo, readEvent.getThreadInfo()); } { ThrowableInfo throwableInfo = new ThrowableInfo(); throwableInfo.setName("java.lang.RuntimeException"); throwableInfo.setStackTrace(new ExtendedStackTraceElement[]{ new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox", "createRuntimeException", null, 27), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox", "access$000", null, 6), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox$InnerClass", "execute", null, 14), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox", "main", null, 47), }); 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 fullWithIgnoredKeyCatalogParams() throws XMLStreamException, IOException { String eventString = "<record>\n" + " <date>2009-03-20T14:06:45</date>\n" + " <millis>1234567890000</millis>\n" + " <sequence>2</sequence>\n" + " <logger>de.huxhorn.lilith.sandbox.JulSandbox$InnerClass</logger>\n" + " <level>WARNING</level>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox$InnerClass</class>\n" + " <method>execute</method>\n" + " <thread>10</thread>\n" + " <message>Foo!</message>\n" + " <key>Key</key>\n" + " <catalog>Catalog</catalog>\n" + " <param>Param1</param>\n" + " <param>Param2</param>\n" + " <exception>\n" + " <message>java.lang.RuntimeException: Exception</message>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox</class>\n" + " <method>createRuntimeException</method>\n" + " <line>27</line>\n" + " </frame>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox</class>\n" + " <method>access$000</method>\n" + " <line>6</line>\n" + " </frame>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox$InnerClass</class>\n" + " <method>execute</method>\n" + " <line>14</line>\n" + " </frame>\n" + " <frame>\n" + " <class>de.huxhorn.lilith.sandbox.JulSandbox</class>\n" + " <method>main</method>\n" + " <line>47</line>\n" + " </frame>\n" + " </exception>\n" + "</record>"; LoggingEvent readEvent = read(eventString); logEvent(readEvent); assertEquals("de.huxhorn.lilith.sandbox.JulSandbox$InnerClass", readEvent.getLogger()); assertEquals((Long) 1_234_567_890_000L, readEvent.getTimeStamp()); assertEquals(LoggingEvent.Level.WARN, readEvent.getLevel()); assertEquals(new Message("Foo!"), readEvent.getMessage()); { ExtendedStackTraceElement[] expectedCallStack = { new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox$InnerClass", "execute", null, -1), }; assertArrayEquals(expectedCallStack, readEvent.getCallStack()); } { ThreadInfo threadInfo = new ThreadInfo(); threadInfo.setId(10L); assertEquals(threadInfo, readEvent.getThreadInfo()); } { ThrowableInfo throwableInfo = new ThrowableInfo(); throwableInfo.setName("java.lang.RuntimeException"); throwableInfo.setMessage("Exception"); throwableInfo.setStackTrace(new ExtendedStackTraceElement[]{ new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox", "createRuntimeException", null, 27), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox", "access$000", null, 6), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox$InnerClass", "execute", null, 14), new ExtendedStackTraceElement("de.huxhorn.lilith.sandbox.JulSandbox", "main", null, 47), }); ThrowableInfo actual = readEvent.getThrowable(); if(logger.isInfoEnabled()) logger.info("Expected: {}", throwableInfo.toString()); if(logger.isInfoEnabled()) logger.info("Actual : {}", actual.toString()); assertEquals(throwableInfo, readEvent.getThrowable()); } }