如何将Java应用程序日志添加到系统日志中

时间:2014-06-24 06:53:28

标签: java log4j

我试过这种方式但产生以下错误: -

log4j:ERROR Precision option (0) isn't a positive integer.

public class log4jExample {

    private static Logger logger = Logger.getRootLogger();

    public static void main(String[] args) {
        try {
            SyslogAppender syslogAppender = new SyslogAppender();
            syslogAppender.setName("SYSLOG");
            syslogAppender.setSyslogHost("localhost");
            syslogAppender.setFacility("Local3");
            syslogAppender.setHeader(true);
            PatternLayout layout = new PatternLayout("%d{dd-MMM-yyyy HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n");
            syslogAppender.setLayout(layout);
            Category category = Category.getInstance(log4jExample.class.getName());//logger.getParent();
            Throwable thrwo = new Throwable();
            LoggingEvent event = new LoggingEvent("log4jExample.class", category, 1000, Priority.DEBUG, new Object(), thrwo);
            syslogAppender.append(event);
            logger.debug("LOG");
        } catch (Exception ex) {
            System.out.println("Exc " + ex);
        }
    }
}

属性文件:

log4j.rootLogger=INFO, SYSLOG log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender 
log4j.appender.SYSLOG.SyslogHost=localhost log4j.appender.SYSLOG.Facility=Local3 
log4j.appender.SYSLOG.Header=true 
log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout 
log4j.appender.SYSLOG.layout.ConversionPattern=java %d{ISO8601} %p %t %c{0}.%M - %m%n 

1 个答案:

答案 0 :(得分:1)

它几乎告诉你出了什么问题:

Precision option (0) isn't a positive integer.

正如您在PatternLayout documentation中所读到的那样,c有一个精确说明符,您使用0将其设置为%c{0}0不是正整数。将其更改为数字> = 1