Logging from your Java Application

Would you rather log JSON over HTTP? Check out this blog post: http://loggly.com/blog/2011/09/logging-out-of-your-java-code/

To use syslog4j to log out of your java application, you'll need to set up the standard SyslogAppender to route from your app to your local syslog (port 514, using one of the Local* facilities), then set up syslog to forward from there to your logs.loggly.com port

For example, assuming you have a java app named “foo” which can log to the Local2 facility, and your logs.loggly.com port is 12345, you'll need to add something like this to your log4j.properties file:

    log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
    log4j.appender.SYSLOG.SyslogHost=localhost
    log4j.appender.SYSLOG.Facility=Local2
    log4j.appender.SYSLOG.FacilityPrinting=true

and something like this to your syslog config (this is for syslog-ng):

    filter f_foo { facility(local2); };
    destination d_foo { udp("logs.loggly.com" port(12345)); };
    log { source(s_udp); filter(f_foo); destination(d_foo); };

If you have multiple java application servers, and you want teach one to use its own input, you can define separate Local* facilities for each one, and just duplicate this pattern. E.g If you have another java app named bar that can log to Local3, and you want it to use a different input (port 23456, say), then you will end up with something like this in your syslog config:

    filter f_foo { facility(local2); };
    destination d_foo { udp("logs.loggly.com" port(12345)); };
    log { source(s_udp); filter(f_foo); destination(d_foo); };
    
    filter f_bar { facility(local3); };
    destination d_bar { udp("logs.loggly.com" port(23456)); };
    log { source(s_udp); filter(f_bar); destination(d_bar); };

and the log4j.properties file for bar will include something like this:

    log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
    log4j.appender.SYSLOG.SyslogHost=localhost
    log4j.appender.SYSLOG.Facility=Local3
    log4j.appender.SYSLOG.FacilityPrinting=true
Navigation
Print/export
Toolbox