[ 
https://issues.apache.org/jira/browse/LOG4J2-1795?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15831497#comment-15831497
 ] 

Remko Popma commented on LOG4J2-1795:
-------------------------------------

*LogEvent deserialization behaviour*

The reason that the test program fails is because of some counter-intuitive 
asymmetrical behaviour in how LogEvents are serialized and deserialized:
* to serialize a LogEvent you first convert it to a 
Log4jLogEvent$LogEventProxy, and serialize _that_ instead.
* however, Log4jLogEvent$LogEventProxy has a {{readResolve}} method that 
returns a _Log4jLogEvent_. This is not symmetrical! Deserializing a serialized 
Log4jLogEvent$LogEventProxy will give you a Log4jLogEvent.

ObjectInputStream::readObject will return the LogEvent you are looking for. 
Please use this object instead.

{code}
@Test
public void testSerialisationsDeserialization_fails() throws IOException, 
ClassNotFoundException {
    // step 1: convert the LogEvent into a Log4jLogEvent$LogEventProxy instance 
(which can be serialized)
    Serializable ser = Log4jLogEvent.serialize(event, true);

    // step 2: serialize the Log4jLogEvent$LogEventProxy instance to a byte[] 
array
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(ser);
    byte[] bytes = baos.toByteArray();

    // step 3: prepare to deserialize
    ObjectInputStream ois = new ObjectInputStream(new 
ByteArrayInputStream(bytes));

    // step 4: deserialize the byte[] array: this returns a Log4jLogEvent 
instance!
    //        This is *not* a Log4jLogEvent$LogEventProxy instance!
    Serializable result = (Serializable) ois.readObject(); // use this object

    // step 5: this fails because the ::deserialize method *only* accepts 
Log4jLogEvent$LogEventProxy instances
    Log4jLogEvent.deserialize(result);
}
{code}

*ParameterizedMessage*
As I noted in a previous comment, in ParameterizedMessage the formatted message 
is preserved correctly, but the array of parameter Objects is transient so 
message parameters will not survive serialization. This can be enhanced. Do you 
need this?

> LogEvent serialization deserialization
> --------------------------------------
>
>                 Key: LOG4J2-1795
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1795
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Appenders
>    Affects Versions: 2.7
>            Reporter: Alexandru Ionita
>              Labels: serializable
>
> Deserialization of a LogEvent that has been serialized with the 
> Log4jLogEvent.serialize method fails after the serialized object went through 
> a byte stream.
> Here is a test that demonstrates the issue:
> https://github.com/nucatus/LogEventSerializationPOC



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-dev-h...@logging.apache.org

Reply via email to