Log4j_2和Socket UDP appender

时间:2012-11-12 14:22:57

标签: java sockets udp appender log4j2

我正在尝试创建一个简单的UDP服务器来处理来自Log4j2 Socket appender的软件包,但是我收到了来自第二个软件包的“java.io.StreamCorruptedException:invalid stream header:7372003E”。

附加器:

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

SNIPPET UDP服务器:

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();
        }           
    }
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我使用了你帖子中的相同代码,但我成功地将调试日志发送到服务器端。下面是我的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>