Monday, 8 March 2010

Fun with SyslogAppender

When you use org.apache.log4j.net.SyslogAppender, you might want to test it with your local syslog. It is fairly easy once you know how to do it, but most of info I could find in the web was for Linux users, and it turns out a Mac OS syslog configuration for this is a bit different, so here's a version for mac users:

1. Get your SyslogAppender set, let's say we'll be using LOCAL1 facility:
Logger logger = Logger.getLogger(SysLogAppenderLocalTest.class);
SyslogAppender syslogAppender = new SyslogAppender();
syslogAppender.setSyslogHost("localhost");
syslogAppender.setFacility("LOCAL1");
syslogAppender.setLayout(new PatternLayout());
logger.addAppender(syslogAppender);
logger.info("I'm logging with syslog");

2. Configure your syslog to log to certain file when LOCAL1 is used:
In /etc/syslog.conf add a line:
local1.* /var/log/whatever.log
set whatever.log filename to whatever you fancy.
Test this with command-line logger:

logger -p local1.info TESTING

you're 'whatever/log' file should have an entry with "TESTING" string

3. Enable remote writing to syslog:
SyslogAppender is using org.apache.log4j.helpers.SyslogWriter, and that class is using the java.net.DatagramPacket which is writing to syslog remotely. So we need remote write enabled on our local syslog.
On Mac OS 10.5 go to
/System/Library/LaunchDaemons/com.apple.syslogd.plist
file and uncomment NetworkListener key with it's dict tag (at the end f the file).
Reload properites for syslogd:
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.syslogd.plist
sudo launchctl load /System/Library/LaunchDaemons/com.apple.syslogd.plist

And you should be ready to go. After you run your java app logs you send do the logger with sysLogAppender should show up in your 'whatever.log' file.


1 comment:

  1. Thank you for this post! I was struggling with this exact problem and would have had a really hard time finding the solution. You saved me a lot of time!

    ReplyDelete