Menu

#276 Add log4jtcp appender

future
open
None
5
2013-11-21
2013-10-09
Jan Rüegg
No

It would be great (and should not be a big effort) to create a log4j tcp appender. With this, it would be very easy to use a good logging tool like http://code.google.com/p/otroslogviewer/ out of the box. (otroslogviewer supports tcp log4j streams, but no udp.)

Discussion

  • Václav Haisman

    Václav Haisman - 2013-10-09
    • assigned_to: Václav Zeman
     
  • Václav Haisman

    Václav Haisman - 2013-10-09

    It should be possible to add this. However, first I would like to know if there is any description of the protocol/payload formatting on the stream?

     
  • Jan Rüegg

    Jan Rüegg - 2013-10-09

    Actually, I can already make it work, when forwarding the udp stream to tcp with socat:

    Logger:
    http://code.google.com/p/otroslogviewer/
    Tools -> start socket listener -> config "log4j xml" + port "8080"

    Source code:
    uncomment in hal/baselogger.cpp:
    #include <log4cplus log4judpappender.h="">
    log4cplus::helpers::SharedObjectPtr<log4cplus::Appender> networkAppender(new log4cplus::Log4jUdpAppender("localhost", 8080));
    log4cplus::Logger::getRoot().addAppender(networkAppender);

    Udp -> TCP:
    http://www.dest-unreach.org/socat/
    (windows: http://blog.gentilkiwi.com/programmes/socat#englishversion)
    command:
    socat -v udp4-listen:8080,fork tcp:localhost:8080

    I guess this should make it pretty straightforward to adapt the existing log4j udp logger to use tcp...

     
  • Jan Rüegg

    Jan Rüegg - 2013-10-15

    I just looked at the source code: Basically, taking the "socketappender.cxx", renaming it to "log4jtcpappender.cxx" and replacing (in the append function) this:

    helpers::SocketBuffer buffer(LOG4CPLUS_MAX_MESSAGE_SIZE - sizeof(unsigned int));
    convertToBuffer (buffer, event, serverName);
    helpers::SocketBuffer msgBuffer(LOG4CPLUS_MAX_MESSAGE_SIZE);

    msgBuffer.appendInt(static_cast<unsigned>(buffer.getSize()));
    msgBuffer.appendBuffer(buffer);
    

    with this from the log4judpappender.cxx append function:

    tstring & str = formatEvent (event);
    
    internal::appender_sratch_pad & appender_sp
        = internal::get_appender_sp ();
    tostringstream & buffer = appender_sp.oss;
    detail::clear_tostringstream (buffer);
    
    buffer << LOG4CPLUS_TEXT("<log4j:event logger=\"")
           << outputXMLEscaped (event.getLoggerName())
           << LOG4CPLUS_TEXT("\" level=\"")
           // TODO: Some escaping of special characters is needed here.
           << outputXMLEscaped (getLogLevelManager().toString(event.getLogLevel()))
           << LOG4CPLUS_TEXT("\" timestamp=\"")
           << event.getTimestamp().getFormattedTime(LOG4CPLUS_TEXT("%s%q"))
           << LOG4CPLUS_TEXT("\" thread=\"") << event.getThread()
           << LOG4CPLUS_TEXT("\">")
    
           << LOG4CPLUS_TEXT("<log4j:message>")
           // TODO: Some escaping of special characters is needed here.
           << outputXMLEscaped (str)
           << LOG4CPLUS_TEXT("</log4j:message>")
    
           << LOG4CPLUS_TEXT("<log4j:NDC>")
           // TODO: Some escaping of special characters is needed here.
           << outputXMLEscaped (event.getNDC())
           << LOG4CPLUS_TEXT("</log4j:NDC>")
    
           << LOG4CPLUS_TEXT("<log4j:locationInfo class=\"\" file=\"")
           // TODO: Some escaping of special characters is needed here.
           << outputXMLEscaped (event.getFile())
           << LOG4CPLUS_TEXT("\" method=\"")
           << outputXMLEscaped (event.getFunction())
           << LOG4CPLUS_TEXT("\" line=\"")
           << event.getLine()
           << LOG4CPLUS_TEXT("\"/>")
           << LOG4CPLUS_TEXT("</log4j:event>");
    
    LOG4CPLUS_TSTRING_TO_STRING (buffer.str ()).swap (appender_sp.chstr);
    

    should be enough to make it work...

     
  • Jan Rüegg

    Jan Rüegg - 2013-11-21

    Would a patch help to speed things up? It would be great to have this functionality, and I don't think its a lot of work. But I can try to do it myself maybe...

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.