Log4j 2
  1. Log4j 2
  2. LOG4J2-228

Log4j2 UDP socket configure error. Class not found org.apache.logging.log4j.core.impl.Log4jLogEvent$LogEventProxy

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-beta4
    • Fix Version/s: 2.0-beta7
    • Component/s: None
    • Labels:
      None

      Description

      On Wednesday 10 April 2013 04:15 PM, Imesh Damith wrote:
      Hi,

      I'm trying to use UDP server to log my messages. For this I use LOG4J2.
      But I got below error in the server side.

      ClassNotFoundException Error : java.lang.ClassNotFoundException:
      org.apache.logging.log4j.core.impl.Log4jLogEvent$LogEventProxy

      Below is my server side code.

      ByteArrayInputStream bis = new ByteArrayInputStream(packet.getData());
      ObjectInputStream obj = new ObjectInputStream(bis);
      LogEvent logEvent = (LogEvent) obj.readObject();

      System.out.println(logEvent.toString());

      Below is my pom.xml file dependency.

      <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.0-beta4</version>
      </dependency>
      <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.0-beta4</version>
      </dependency>

      Thanks in advance,
      Imesh

      1. TCPServer_test.java
        3 kB
        Remko Popma
      2. pom.xml
        4 kB
        Remko Popma
      3. Logs_test.java
        0.4 kB
        Remko Popma
      4. log4j2.xml
        0.6 kB
        Remko Popma

        Issue Links

          Activity

          Hide
          Imesh Damith added a comment -

          Hi Ralph,

          Thank you indeed. Which release this will be available?

          Imesh

          Show
          Imesh Damith added a comment - Hi Ralph, Thank you indeed. Which release this will be available? Imesh
          Ralph Goers made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Fix Version/s 2.0-beta7 [ 12324400 ]
          Resolution Fixed [ 1 ]
          Hide
          Ralph Goers added a comment -

          Fix committed in revision 1487289. Please verify and close.

          Show
          Ralph Goers added a comment - Fix committed in revision 1487289. Please verify and close.
          Hide
          Remko Popma added a comment -

          Imesh, here are some relevant classes:

          o.a.l.l.c.appender.SocketAppender (and parent classes)
          o.a.l.l.c.net.DatagramOutputStream
          o.a.l.l.c.net.DatagramSocketManager
          o.a.l.l.c.net.TCPSocketManager
          o.a.l.l.c.layout.SerializedLayout

          SerializedLayout overwrites #writeStreamHeader to do nothing so the 2nd event does not write the object header to the stream.
          There is probably a reason for this though, but I don't know what that would be. Ralph would know.

          Potentially related Jira tickets:
          LOG4J2-142
          LOG4J2-96
          LOG4J2-246

          Show
          Remko Popma added a comment - Imesh, here are some relevant classes: o.a.l.l.c.appender.SocketAppender (and parent classes) o.a.l.l.c.net.DatagramOutputStream o.a.l.l.c.net.DatagramSocketManager o.a.l.l.c.net.TCPSocketManager o.a.l.l.c.layout.SerializedLayout SerializedLayout overwrites #writeStreamHeader to do nothing so the 2nd event does not write the object header to the stream. There is probably a reason for this though, but I don't know what that would be. Ralph would know. Potentially related Jira tickets: LOG4J2-142 LOG4J2-96 LOG4J2-246
          Hide
          Imesh Damith added a comment -

          Hi Ralph,

          If you can tell which files to check I can have a look. I had a look yesterday by going through your comments here and on LOG4J2-96 but I'm lost without much info.

          -Imesh

          Show
          Imesh Damith added a comment - Hi Ralph, If you can tell which files to check I can have a look. I had a look yesterday by going through your comments here and on LOG4J2-96 but I'm lost without much info. -Imesh
          Hide
          Gary Gregory added a comment -

          Linking to another UDP issue: LOG4J2-246

          Show
          Gary Gregory added a comment - Linking to another UDP issue: LOG4J2-246
          Gary Gregory made changes -
          Link This issue relates to LOG4J2-246 [ LOG4J2-246 ]
          Hide
          Ralph Goers added a comment -

          Imesh,

          Is this your first time working with an open source project? It certainly seems like it. We are all volunteers. I understand that you are experiencing a problem for which you need a solution. But in every open source project the only way to insure that something gets done is to do it yourself. That said, I am on vacation this week and I am hopeful I will have time to investigate this.

          Show
          Ralph Goers added a comment - Imesh, Is this your first time working with an open source project? It certainly seems like it. We are all volunteers. I understand that you are experiencing a problem for which you need a solution. But in every open source project the only way to insure that something gets done is to do it yourself. That said, I am on vacation this week and I am hopeful I will have time to investigate this.
          Hide
          Remko Popma added a comment -

          Imesh,

          I was actually serious when I suggested submitting a patch.
          The log4j2 web site shows how you can check out the source, and you only need to install Maven and you can build everything with one command (mvn clean install).

          This may be the fastest way to ensure that the issue is fixed in the next beta release,
          and you will get credit for your contribution on the web site (the changes report page).

          Best regards,
          -Remko

          Show
          Remko Popma added a comment - Imesh, I was actually serious when I suggested submitting a patch. The log4j2 web site shows how you can check out the source, and you only need to install Maven and you can build everything with one command (mvn clean install). This may be the fastest way to ensure that the issue is fixed in the next beta release, and you will get credit for your contribution on the web site (the changes report page). Best regards, -Remko
          Hide
          Imesh Damith added a comment -

          Hi Ramoko,

          Please let me know how to get rid of this error. Should I say to community that log4j2 doesn't support UDP ?
          And write our own mechanism to use UDP ?

          I saw the same issue happened some other ppl also.

          Please help.

          -Imesh

          Show
          Imesh Damith added a comment - Hi Ramoko, Please let me know how to get rid of this error. Should I say to community that log4j2 doesn't support UDP ? And write our own mechanism to use UDP ? I saw the same issue happened some other ppl also. Please help. -Imesh
          Hide
          Remko Popma added a comment - - edited

          Hi Imesh,

          I am in a similar situation as Ralph, work has been very busy for me the last few weeks. (But no holiday planned )
          The quickest way to get this fixed may be to submit a patch...

          -Remko

          Show
          Remko Popma added a comment - - edited Hi Imesh, I am in a similar situation as Ralph, work has been very busy for me the last few weeks. (But no holiday planned ) The quickest way to get this fixed may be to submit a patch... -Remko
          Hide
          Imesh Damith added a comment -

          Hi Ralph,

          I understand you. Is there anyone else who can help me? Ramko can't help me?

          -Imesh

          Show
          Imesh Damith added a comment - Hi Ralph, I understand you. Is there anyone else who can help me? Ramko can't help me? -Imesh
          Hide
          Ralph Goers added a comment -

          Unfortunately, saying you are waiting doesn't create more time in my day. I'm very busy in my $day job this week so I may not get to it. I'm taking vacation next week which usually means I get more time for ASF stuff.

          Show
          Ralph Goers added a comment - Unfortunately, saying you are waiting doesn't create more time in my day. I'm very busy in my $day job this week so I may not get to it. I'm taking vacation next week which usually means I get more time for ASF stuff.
          Hide
          Imesh Damith added a comment -

          HI Ralph,

          Had any luck to check this?

          I'm waiting for a fix.

          Show
          Imesh Damith added a comment - HI Ralph, Had any luck to check this? I'm waiting for a fix.
          Hide
          Imesh Damith added a comment -

          HI Ralph,

          Yes, this code is for UDP packet sender and a receiver. I used Log4j2.xml configuration from client end to send debug log to a particular UDP server.
          Packets are DatagramPacket. Below is how I define a packet.

          byte[] buf = buf = new byte[100000];

          DatagramPacket packet = new DatagramPacket(buf, buf.length);
          socket.receive(packet);
          .
          .
          .

          Sorry about misleading log of TCPServer. actually this is about UDP.

          Show
          Imesh Damith added a comment - HI Ralph, Yes, this code is for UDP packet sender and a receiver. I used Log4j2.xml configuration from client end to send debug log to a particular UDP server. Packets are DatagramPacket. Below is how I define a packet. byte[] buf = buf = new byte [100000] ; DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); . . . Sorry about misleading log of TCPServer. actually this is about UDP.
          Hide
          Ralph Goers added a comment -

          This problem report is a little confusing as the title references UDP and the code above mentions packets, but the debug logging says TCPServer. I am going to assume that UDP is really being used as I look into this.

          Show
          Ralph Goers added a comment - This problem report is a little confusing as the title references UDP and the code above mentions packets, but the debug logging says TCPServer. I am going to assume that UDP is really being used as I look into this.
          Hide
          Imesh Damith added a comment -

          HI Ralph, Ramko,

          Any fix you propose for this? I'm still waiting.

          Show
          Imesh Damith added a comment - HI Ralph, Ramko, Any fix you propose for this? I'm still waiting.
          Hide
          Remko Popma added a comment -

          Imesh, thanks for the additional info. This is very helpful!

          Ralph, the second event is 4 bytes shorter than the first event.
          I believe the problem is in SerializedLayout, which does something special to only write the header for the first event.
          This works well for a (TCP) continuous stream where the recipient creates the ObjectInputStream once and reuses that stream for all events, but in Imesh's case (with DatagramPackets) he creates a new ObjectInputStream for every event, so the new ObjectInputStream expects a header again.
          Perhaps the appender should tell SerializedLayout whether to include the header every time or only once?

          Show
          Remko Popma added a comment - Imesh, thanks for the additional info. This is very helpful! Ralph, the second event is 4 bytes shorter than the first event. I believe the problem is in SerializedLayout, which does something special to only write the header for the first event. This works well for a (TCP) continuous stream where the recipient creates the ObjectInputStream once and reuses that stream for all events, but in Imesh's case (with DatagramPackets) he creates a new ObjectInputStream for every event, so the new ObjectInputStream expects a header again. Perhaps the appender should tell SerializedLayout whether to include the header every time or only once?
          Hide
          Imesh Damith added a comment - - edited

          I get error when I try to get obj.available(). Also I add the received packet length to the log. pls refer below code and log after that.

          static DatagramSocket socket = new DatagramSocket(9095);

          public void myServerMethod() {
          socket = new DatagramSocket(9095);
          byte[] buf = new byte[100000];
          DatagramPacket packet = new DatagramPacket(buf, buf.length);

          logger.debug("Packet length "+packet.getLength());
          while (true) {

          logger.debug ("UDPServer Waiting for client on port "+socket.getLocalPort());
          socket.receive(packet);

          logger.debug("Packet length "+packet.getLength());
          if (packet.getLength()>0)

          { ByteArrayInputStream bis = new ByteArrayInputStream(packet.getData()); ObjectInputStream obj = new ObjectInputStream(bis); LogEvent logEvent = (LogEvent) obj.readObject(); logger.debug("Got it : " + logEvent.toString()); logger.debug("after read "+obj.available()); // here we read the obj.available(); and first instance there is an error throwing. }

          } // end of while
          } // end of myServerMethod

          Below is the printed log.

          UDPServer Waiting for client on port 9095
          Packet length 869
          Got it : Logger=rezg.gdsws.util.Logs Level=FATAL Message=test1
          IOException Error : java.io.StreamCorruptedException
          ***********************************************
          UDPServer Waiting for client on port 9095
          Packet length 865
          IOException Error : java.io.StreamCorruptedException: invalid stream header
          ***********************************************

          Show
          Imesh Damith added a comment - - edited I get error when I try to get obj.available(). Also I add the received packet length to the log. pls refer below code and log after that. static DatagramSocket socket = new DatagramSocket(9095); public void myServerMethod() { socket = new DatagramSocket(9095); byte[] buf = new byte [100000] ; DatagramPacket packet = new DatagramPacket(buf, buf.length); logger.debug("Packet length "+packet.getLength()); while (true) { logger.debug ("UDPServer Waiting for client on port "+socket.getLocalPort()); socket.receive(packet); logger.debug("Packet length "+packet.getLength()); if (packet.getLength()>0) { ByteArrayInputStream bis = new ByteArrayInputStream(packet.getData()); ObjectInputStream obj = new ObjectInputStream(bis); LogEvent logEvent = (LogEvent) obj.readObject(); logger.debug("Got it : " + logEvent.toString()); logger.debug("after read "+obj.available()); // here we read the obj.available(); and first instance there is an error throwing. } } // end of while } // end of myServerMethod Below is the printed log. UDPServer Waiting for client on port 9095 Packet length 869 Got it : Logger=rezg.gdsws.util.Logs Level=FATAL Message=test1 IOException Error : java.io.StreamCorruptedException *********************************************** UDPServer Waiting for client on port 9095 Packet length 865 IOException Error : java.io.StreamCorruptedException: invalid stream header ***********************************************
          Hide
          Remko Popma added a comment - - edited

          After you read the first object from the byte array in the first packet, can you call obj#available() to see if there is any data left in the stream?
          Also, can you check the length of both received packages?

          [Edit] Looks like my message crossed Ralph's. Ralph, is there a JIRA for the TCP socket issue?
          [Edit2] LOG4J2-96 (Note to self: look things up first.)

          Show
          Remko Popma added a comment - - edited After you read the first object from the byte array in the first packet, can you call obj#available() to see if there is any data left in the stream? Also, can you check the length of both received packages? [Edit] Looks like my message crossed Ralph's. Ralph, is there a JIRA for the TCP socket issue? [Edit2] LOG4J2-96 (Note to self: look things up first.)
          Ralph Goers made changes -
          Assignee Remko Popma [ remkop@yahoo.com ] Ralph Goers [ ralph.goers@dslextreme.com ]
          Hide
          Ralph Goers added a comment -

          This is probably the same problem we had with TCP sockets. I will look into this.

          Show
          Ralph Goers added a comment - This is probably the same problem we had with TCP sockets. I will look into this.
          Hide
          Imesh Damith added a comment - - edited

          Hi Ramko,

          I have another problem. When I try to pass, say two log messages in a row, in my server side I can get only first log successfully. for the second one it gave IOException Error : java.io.StreamCorruptedException: invalid stream header

          client side is sending below two log messages.
          logger.fatal ("test1")
          logger.fatal("test 2")

          Server side code.

          static DatagramSocket socket ;

          public void myServerMethod () {
          socket = new DatagramSocket(9095);

          DatagramPacket packet = new DatagramPacket(buf, buf.length);
          byte[] buf = new byte[100000];

          while(true){
          try{
          logger.debug ("UDPServer Waiting for client on port "+socket.getLocalPort());
          socket.receive(packet);
          if (packet.getLength()>0)

          { ByteArrayInputStream bis = new ByteArrayInputStream(packet.getData()); ObjectInputStream obj = new ObjectInputStream(bis); LogEvent logEvent = (LogEvent) obj.readObject(); logger.debug("Got it : " + logEvent.toString()); }

          }catch (IOException e1)

          { logger.fatal(" IOException Error : " + e1); }

          logger.info("***********************************************");
          } // end of while

          } // end of method
          And my log print as below.

          UDPServer Waiting for client on port 9095
          Got it : Logger=rezg.gdsws.util.Logs Level=FATAL Message=test1 // log properly print for the 1st msg
          ***********************************************
          UDPServer Waiting for client on port 9095
          IOException Error : java.io.StreamCorruptedException: invalid stream header // Error for the 2nd msg.
          ***********************************************

          Do you have any idea for this?

          Thanks in advance,
          Imesh

          Show
          Imesh Damith added a comment - - edited Hi Ramko, I have another problem. When I try to pass, say two log messages in a row, in my server side I can get only first log successfully. for the second one it gave IOException Error : java.io.StreamCorruptedException: invalid stream header client side is sending below two log messages. logger.fatal ("test1") logger.fatal("test 2") Server side code. static DatagramSocket socket ; public void myServerMethod () { socket = new DatagramSocket(9095); DatagramPacket packet = new DatagramPacket(buf, buf.length); byte[] buf = new byte [100000] ; while(true){ try{ logger.debug ("UDPServer Waiting for client on port "+socket.getLocalPort()); socket.receive(packet); if (packet.getLength()>0) { ByteArrayInputStream bis = new ByteArrayInputStream(packet.getData()); ObjectInputStream obj = new ObjectInputStream(bis); LogEvent logEvent = (LogEvent) obj.readObject(); logger.debug("Got it : " + logEvent.toString()); } }catch (IOException e1) { logger.fatal(" IOException Error : " + e1); } logger.info("***********************************************"); } // end of while } // end of method And my log print as below. UDPServer Waiting for client on port 9095 Got it : Logger=rezg.gdsws.util.Logs Level=FATAL Message=test1 // log properly print for the 1st msg *********************************************** UDPServer Waiting for client on port 9095 IOException Error : java.io.StreamCorruptedException: invalid stream header // Error for the 2nd msg. *********************************************** Do you have any idea for this? Thanks in advance, Imesh
          Remko Popma made changes -
          Assignee Remko Popma [ remkop@yahoo.com ]
          Hide
          Remko Popma added a comment -

          Hi Imesh, great, so the original problem is solved? That's good to hear.

          About sending custom objects, I haven't tried this, but you could have your custom object implement the log4j Message interface, and also implement java.io.Serializable.
          Then logger.info(myMessageObject);
          should serialize myMessageObject and send it to the UDP listener process.

          Show
          Remko Popma added a comment - Hi Imesh, great, so the original problem is solved? That's good to hear. About sending custom objects, I haven't tried this, but you could have your custom object implement the log4j Message interface, and also implement java.io.Serializable. Then logger.info(myMessageObject); should serialize myMessageObject and send it to the UDP listener process.
          Hide
          Imesh Damith added a comment -

          Hi Ramko,

          I was able to manage this. There was a problem in the file name in classpath. I was able to get debug log string value to sever side without any problem.

          But I have another problem, I want to pass my own object to server side.
          How can I read that from Server?

          bis = new ByteArrayInputStream(UDP_Packet);
          obj = new ObjectInputStream(bis);
          logEvent = (LogEvent) obj.readObject();

          System.out.println("Got Message : " + logEvent.getMessage().getFormattedMessage());

          Above is how i was able to get the String debug to server. ( logger.debug("Test") )

          but how to read the object if I set logger.debug ( myObject ) ;

          Thanks in advance.
          Imesh

          Show
          Imesh Damith added a comment - Hi Ramko, I was able to manage this. There was a problem in the file name in classpath. I was able to get debug log string value to sever side without any problem. But I have another problem, I want to pass my own object to server side. How can I read that from Server? bis = new ByteArrayInputStream(UDP_Packet); obj = new ObjectInputStream(bis); logEvent = (LogEvent) obj.readObject(); System.out.println("Got Message : " + logEvent.getMessage().getFormattedMessage()); Above is how i was able to get the String debug to server. ( logger.debug("Test") ) but how to read the object if I set logger.debug ( myObject ) ; Thanks in advance. Imesh
          Hide
          Remko Popma added a comment -

          Imesh,
          Does the server-side code (the process that is receiving UDP messages) have both the log4j-api and the log4j-core jar files in the classpath?

          Show
          Remko Popma added a comment - Imesh, Does the server-side code (the process that is receiving UDP messages) have both the log4j-api and the log4j-core jar files in the classpath?
          Remko Popma made changes -
          Field Original Value New Value
          Attachment pom.xml [ 12581182 ]
          Attachment log4j2.xml [ 12581183 ]
          Attachment Logs_test.java [ 12581184 ]
          Attachment TCPServer_test.java [ 12581185 ]
          Hide
          Remko Popma added a comment -

          Attaching files received from Imesh:


          Hi Remko,

          Sorry, I do not have access to your JIRA. (User name pwd).

          Here with I have attached relevant files. My whole program needs to configure but within these classes you should be able to understand.

          client code (Logs_test.java class) send the debug log and TCPServer_test.java class receive it.

          But there is an error catching in line 111 ( class not found).

          Let me know if I miss any jar file. My pom.xml file also attached.

          Regards,
          Imesh

          Show
          Remko Popma added a comment - Attaching files received from Imesh: Hi Remko, Sorry, I do not have access to your JIRA. (User name pwd). Here with I have attached relevant files. My whole program needs to configure but within these classes you should be able to understand. client code (Logs_test.java class) send the debug log and TCPServer_test.java class receive it. But there is an error catching in line 111 ( class not found). Let me know if I miss any jar file. My pom.xml file also attached. Regards, Imesh
          Remko Popma created issue -

            People

            • Assignee:
              Ralph Goers
              Reporter:
              Remko Popma
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development