Search in sources :

Example 1 with LoggerContextVO

use of ch.qos.logback.classic.spi.LoggerContextVO in project cdap by caskdata.

the class LoggingEventSerializerTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    Map<String, String> mdcMap = Maps.newHashMap();
    mdcMap.put("mdc1", "mdc-val1");
    mdcMap.put("mdc2", null);
    mdcMap.put(null, null);
    Map<String, String> contextMap = Maps.newHashMap();
    contextMap.put("p1", "ctx-val1");
    contextMap.put("p2", null);
    contextMap.put(null, null);
    LoggingEventSerializer serializer = new LoggingEventSerializer();
    ch.qos.logback.classic.spi.LoggingEvent iLoggingEvent = new ch.qos.logback.classic.spi.LoggingEvent();
    iLoggingEvent.setThreadName("threadName1");
    iLoggingEvent.setLevel(Level.INFO);
    iLoggingEvent.setMessage("Log message1");
    iLoggingEvent.setArgumentArray(new Object[] { null, "arg2", "100", null });
    iLoggingEvent.setLoggerName("loggerName1");
    iLoggingEvent.setLoggerContextRemoteView(new LoggerContextVO("logger_context1", contextMap, 12345634234L));
    Exception e1 = new Exception(null, null);
    Exception e2 = new Exception("Test Exception2", e1);
    iLoggingEvent.setThrowableProxy(new ThrowableProxy(e2));
    iLoggingEvent.prepareForDeferredProcessing();
    ((ThrowableProxy) iLoggingEvent.getThrowableProxy()).calculatePackagingData();
    iLoggingEvent.setCallerData(new StackTraceElement[] { new StackTraceElement("com.Class1", "methodName1", "fileName1", 10), null, new StackTraceElement("com.Class2", "methodName2", "fileName2", 20), new StackTraceElement("com.Class3", "methodName3", null, 30), null });
    iLoggingEvent.setMarker(null);
    iLoggingEvent.getMDCPropertyMap().putAll(mdcMap);
    iLoggingEvent.setTimeStamp(1234567890L);
    // Serialize
    ILoggingEvent event = new LogMessage(iLoggingEvent, LoggingContextAccessor.getLoggingContext());
    byte[] serializedBytes = serializer.toBytes(event);
    // De-serialize
    ILoggingEvent actualEvent = serializer.fromBytes(ByteBuffer.wrap(serializedBytes));
    System.out.println(actualEvent);
    assertLoggingEventEquals(iLoggingEvent, actualEvent);
}
Also used : IThrowableProxy(ch.qos.logback.classic.spi.IThrowableProxy) ThrowableProxy(ch.qos.logback.classic.spi.ThrowableProxy) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) IOException(java.io.IOException) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LogMessage(co.cask.cdap.logging.appender.LogMessage) LoggerContextVO(ch.qos.logback.classic.spi.LoggerContextVO) Test(org.junit.Test)

Example 2 with LoggerContextVO

use of ch.qos.logback.classic.spi.LoggerContextVO in project cdap by caskdata.

the class LoggingEventTest method testSerialize.

@Test
public void testSerialize() throws Exception {
    Logger logger = LoggerFactory.getLogger(LoggingEventTest.class);
    ch.qos.logback.classic.spi.LoggingEvent iLoggingEvent = new ch.qos.logback.classic.spi.LoggingEvent(getClass().getName(), (ch.qos.logback.classic.Logger) logger, Level.ERROR, "Log message1", null, new Object[] { "arg1", "arg2", "100" });
    iLoggingEvent.setLoggerName("loggerName1");
    iLoggingEvent.setThreadName("threadName1");
    iLoggingEvent.setTimeStamp(1234567890L);
    iLoggingEvent.setLoggerContextRemoteView(new LoggerContextVO("loggerContextRemoteView", ImmutableMap.of("key1", "value1", "key2", "value2"), 100000L));
    Map<String, String> mdcMap = Maps.newHashMap(ImmutableMap.of("mdck1", "mdcv1", "mdck2", "mdck2"));
    iLoggingEvent.setMDCPropertyMap(mdcMap);
    LoggingEventSerializer serializer = new LoggingEventSerializer();
    byte[] encoded = serializer.toBytes(new LogMessage(iLoggingEvent, LoggingContextAccessor.getLoggingContext()));
    ILoggingEvent decodedEvent = serializer.fromBytes(ByteBuffer.wrap(encoded));
    LoggingEventSerializerTest.assertLoggingEventEquals(iLoggingEvent, decodedEvent);
}
Also used : Logger(org.slf4j.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LogMessage(co.cask.cdap.logging.appender.LogMessage) LoggerContextVO(ch.qos.logback.classic.spi.LoggerContextVO) Test(org.junit.Test)

Aggregations

ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)2 LoggerContextVO (ch.qos.logback.classic.spi.LoggerContextVO)2 LogMessage (co.cask.cdap.logging.appender.LogMessage)2 Test (org.junit.Test)2 IThrowableProxy (ch.qos.logback.classic.spi.IThrowableProxy)1 ThrowableProxy (ch.qos.logback.classic.spi.ThrowableProxy)1 IOException (java.io.IOException)1 Logger (org.slf4j.Logger)1