Search in sources :

Example 1 with FilteredObjectInputStream

use of org.apache.logging.log4j.util.FilteredObjectInputStream in project logging-log4j2 by apache.

the class LogEventTest method testSerialization.

@SuppressWarnings("BanSerializableRead")
@Test
public void testSerialization() throws Exception {
    final LogEvent event1 = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    this.getClass().getName()).setLoggerFqcn(// 
    "org.apache.logging.log4j.core.Logger").setLevel(// 
    Level.INFO).setMessage(// 
    new SimpleMessage("Hello, world!")).build();
    final Exception parent = new IllegalStateException("Test");
    final Throwable child = new LoggingException("This is a test", parent);
    final LogEvent event2 = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    this.getClass().getName()).setLoggerFqcn(// 
    "org.apache.logging.log4j.core.Logger").setLevel(// 
    Level.INFO).setMessage(// 
    new SimpleMessage("Hello, world!")).setThrown(// 
    child).build();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(event1);
    oos.writeObject(event2);
    final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    final ObjectInputStream ois = new FilteredObjectInputStream(bais);
    try {
        ois.readObject();
    } catch (final IOException ioe) {
        fail("Exception processing event1");
    }
    try {
        ois.readObject();
    } catch (final IOException ioe) {
        fail("Exception processing event2");
    }
}
Also used : LoggingException(org.apache.logging.log4j.LoggingException) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) LoggingException(org.apache.logging.log4j.LoggingException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) Test(org.junit.jupiter.api.Test)

Example 2 with FilteredObjectInputStream

use of org.apache.logging.log4j.util.FilteredObjectInputStream in project logging-log4j2 by apache.

the class RingBufferLogEventTest method testSerializationDeserialization.

@SuppressWarnings("BanSerializableRead")
@Test
public void testSerializationDeserialization() throws IOException, ClassNotFoundException {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    final String loggerName = "logger.name";
    final Marker marker = null;
    final String fqcn = "f.q.c.n";
    final Level level = Level.TRACE;
    final Message data = new SimpleMessage("message");
    final Throwable t = new InternalError("not a real error");
    final ContextStack contextStack = null;
    final String threadName = "main";
    final StackTraceElement location = null;
    evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, new FixedPreciseClock(12345, 678), new DummyNanoClock(1));
    ((StringMap) evt.getContextData()).putValue("key", "value");
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(evt);
    final ObjectInputStream in = new FilteredObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final RingBufferLogEvent other = (RingBufferLogEvent) in.readObject();
    assertEquals(loggerName, other.getLoggerName());
    assertEquals(marker, other.getMarker());
    assertEquals(fqcn, other.getLoggerFqcn());
    assertEquals(level, other.getLevel());
    assertEquals(data, other.getMessage());
    assertNull("null after serialization", other.getThrown());
    assertEquals(new ThrowableProxy(t), other.getThrownProxy());
    assertEquals(evt.getContextData(), other.getContextData());
    assertEquals(contextStack, other.getContextStack());
    assertEquals(threadName, other.getThreadName());
    assertEquals(location, other.getSource());
    assertEquals(12345, other.getTimeMillis());
    assertEquals(678, other.getInstant().getNanoOfMillisecond());
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) ObjectOutputStream(java.io.ObjectOutputStream) ThrowableProxy(org.apache.logging.log4j.core.impl.ThrowableProxy) ByteArrayInputStream(java.io.ByteArrayInputStream) FixedPreciseClock(org.apache.logging.log4j.core.time.internal.FixedPreciseClock) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) Level(org.apache.logging.log4j.Level) ObjectInputStream(java.io.ObjectInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) Test(org.junit.Test)

Example 3 with FilteredObjectInputStream

use of org.apache.logging.log4j.util.FilteredObjectInputStream in project logging-log4j2 by apache.

the class MutableLogEventTest method deserialize.

@SuppressWarnings("BanSerializableRead")
private Log4jLogEvent deserialize(final byte[] binary) throws IOException, ClassNotFoundException {
    final ByteArrayInputStream inArr = new ByteArrayInputStream(binary);
    final ObjectInputStream in = useObjectInputStream ? new ObjectInputStream(inArr) : new FilteredObjectInputStream(inArr);
    final Log4jLogEvent result = (Log4jLogEvent) in.readObject();
    return result;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) ObjectInputStream(java.io.ObjectInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream)

Example 4 with FilteredObjectInputStream

use of org.apache.logging.log4j.util.FilteredObjectInputStream in project logging-log4j2 by apache.

the class Log4jLogEventTest method deserialize.

@SuppressWarnings("BanSerializableRead")
private Log4jLogEvent deserialize(final byte[] binary) throws IOException, ClassNotFoundException {
    final ByteArrayInputStream inArr = new ByteArrayInputStream(binary);
    final ObjectInputStream in = new FilteredObjectInputStream(inArr);
    final Log4jLogEvent result = (Log4jLogEvent) in.readObject();
    return result;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) ObjectInputStream(java.io.ObjectInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream)

Example 5 with FilteredObjectInputStream

use of org.apache.logging.log4j.util.FilteredObjectInputStream in project logging-log4j2 by apache.

the class LogEventTest method testNanoTimeIsNotSerialized2.

@SuppressWarnings("BanSerializableRead")
@Test
public void testNanoTimeIsNotSerialized2() throws Exception {
    final LogEvent event1 = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    this.getClass().getName()).setLoggerFqcn(// 
    "org.apache.logging.log4j.core.Logger").setLevel(// 
    Level.INFO).setMessage(// 
    new SimpleMessage("Hello, world!")).setThreadId(// this must be initialized or the test fails
    1).setThreadName(// 
    "this must be initialized or the test fails").setThreadPriority(// this must be initialized or the test fails
    2).setNanoTime(// 
    0).build();
    final LogEvent event2 = new Log4jLogEvent.Builder(event1).build();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(event1);
    final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    final ObjectInputStream ois = new FilteredObjectInputStream(bais);
    final LogEvent actual = (LogEvent) ois.readObject();
    assertEquals(event2, actual, "both zero nanoTime");
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) Test(org.junit.jupiter.api.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)6 ObjectInputStream (java.io.ObjectInputStream)6 FilteredObjectInputStream (org.apache.logging.log4j.util.FilteredObjectInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)4 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)3 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)1 Level (org.apache.logging.log4j.Level)1 LoggingException (org.apache.logging.log4j.LoggingException)1 Marker (org.apache.logging.log4j.Marker)1 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)1 ThrowableProxy (org.apache.logging.log4j.core.impl.ThrowableProxy)1 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)1 FixedPreciseClock (org.apache.logging.log4j.core.time.internal.FixedPreciseClock)1 Message (org.apache.logging.log4j.message.Message)1 MutableThreadContextStack (org.apache.logging.log4j.spi.MutableThreadContextStack)1 StringMap (org.apache.logging.log4j.util.StringMap)1 Test (org.junit.Test)1