1
votes

I'm trying to create a simple UDP server to handle packages from Log4j2 Socket appender but I'm getting a "java.io.StreamCorruptedException: invalid stream header: 7372003E" from the second package received on.

APPENDER:

<Socket name="Socket" host="localhost" port="9500" protocol="UDP" >
    <SerializedLayout /> 
</Socket>

SNIPPET UDP SERVER:

public void startSerialized() throws Exception {
    serverSocket = new DatagramSocket(9500);
    LogEvent logEvent = null;
    ObjectInputStream obj = null;
    ByteArrayInputStream bis = null;
    while (true) {
        try {
            receivePacket = new DatagramPacket(packet, packet.length);
            serverSocket.receive(receivePacket);
            bis = new ByteArrayInputStream(receivePacket.getData());
            obj = new ObjectInputStream(bis);
            logEvent = (LogEvent) obj.readObject();
            System.out.println(logEvent.toString());    
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bis.close();
            obj.close();
        }           
    }
}

Any suggestion guys?

1

1 Answers

0
votes

I used the same code from your post but I was successfully able to get the debug log to server side. Below is my log4j2.xml

    <Socket name="Socket" host="localHost" port="9092" protocol="UDP" >
        <SerializedLayout />
    </Socket>
</appenders>


<loggers>

    <logger name="rezg.gdsws.util" level="info">
        <appender-ref ref="Socket" />
    </logger>

    <logger name="org.apache" level="debug" />  <!-- Otherwise when we enable debug Apache debugs also printed -->

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</loggers>