Writing log data to syslog using log4j

Sandeep Rao picture Sandeep Rao · Nov 5, 2012 · Viewed 30.2k times · Source

I'm unable to write log messages into syslog. Any help would be great. Here is my simple log4j program

import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class log4jExample
{
  /* Get actual class name to be printed on */
  static Logger log = Logger.getLogger(log4jExample.class.getName());

  public static void main(String[] args) throws IOException,SQLException
  {

     log.error("Hello this is an error message");
     log.info("Hello this is an info message");
     log.fatal("Fatal error message");
  }
}

My syslog properties file

# configure the root logger
log4j.rootLogger=INFO, SYSLOG


# configure Syslog facility LOCAL1 appender
log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.threshold=WARN
log4j.appender.SYSLOG.syslogHost=localhost
log4j.appender.SYSLOG.facility=LOCAL4
log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.conversionPattern=[%p] %c:%L - %m%n

Answer

Sandeep Rao picture Sandeep Rao · Nov 5, 2012

Add the following lines to rsyslog.conf file

$ModLoad imudp
$UDPServerRun 514

It worked for me.

Need to restart the rsyslog after modfications.