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.


2 comments:

  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
  2. Hi, I did the above config in Mac 10.11.4, I can see the message with "logger -p local6.info 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.setSyslogHost("127.0.0.1");
    syslogAppender.setFacility("LOCAL6");
    syslogAppender.setFacilityPrinting(true);
    syslogAppender.setLayout(new PatternLayout());
    logger.addAppender(syslogAppender);

    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"));
    logger.info("I'm logging with syslog");

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

    }

    and log4j.properties is

    log4j.rootLogger=INFO, file, SYSLOG
    log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
    log4j.appender.SYSLOG.syslogHost=localhost
    log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
    log4j.appender.SYSLOG.layout.conversionPattern=%d{ISO8601} %-5p [%t] %c{2} %x - %m%n
    log4j.appender.SYSLOG.Facility=local6
    log4j.appender.SYSLOG.Threshold=debug
    log4j.appender.SYSLOG.FacilityPrinting=true

    Thank you!

    ReplyDelete