Tuesday, October 10, 2006

log4j and UNIX syslog

I couldn't find Java code examples anywhere on how to get log4j to log to Unix syslog. I'm sure they're out there, but I'll be damned if I could find them. I spent a few hours with log4j today and managed to decode what's going on. Here it is in all it's glory:


import org.apache.log4j.net.SyslogAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;

class FOO {

public static void main(String args[]) {
FOO foo = new FOO();
}

public FOO () {
SimpleLayout layout = new SimpleLayout();
SyslogAppender appender = new SyslogAppender(layout, "localhost", SyslogAppender.LOG_DAEMON);
Logger logger = Logger.getRootLogger();
logger.addAppender(appender);
logger.info("This is just a test");
}
}


I'd write more about how cool log4j is, but I'm under a deadline and have to get back to work...

P.S. If this still doesn't work, then you probably forgot to make your syslog daemon listen to a socket....

No comments: