Monday, 8 March 2010

Fun with SyslogAppender

When you use, 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.setLayout(new PatternLayout());
logger.addAppender(syslogAppender);"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 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 which is writing to syslog remotely. So we need remote write enabled on our local syslog.
On Mac OS 10.5 go to
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/
sudo launchctl load /System/Library/LaunchDaemons/

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. 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!

  2. Hi, I did the above config in Mac 10.11.4, I can see the message with "logger -p TESTING" in /var/log/vast.log, but with java code below, I can not see the message, any suggestions or how can we debug it?

    public void testSyslog() {
    Logger logger = Logger.getRootLogger();
    SyslogAppender syslogAppender = new SyslogAppender();
    syslogAppender.setLayout(new PatternLayout());

    System.out.println("SyslogAppender facility is " + syslogAppender.getFacility());
    System.out.println("SyslogAppender facility is " + syslogAppender.getFacilityPrinting());
    System.out.println("SyslogAppender facility is " + syslogAppender.getFacility("syslog"));"I'm logging with syslog");

    System.out.println(" test is completed");


    and is

    log4j.rootLogger=INFO, file, SYSLOG
    log4j.appender.SYSLOG.layout.conversionPattern=%d{ISO8601} %-5p [%t] %c{2} %x - %m%n

    Thank you!