Log4j2使用syslog appender和RFC5424

时间:2014-11-11 02:23:12

标签: java linux log4j2 syslog

当我尝试将一些消息写入远程Linux系统日志时,我使用log4j2和appender syslog以及format = RFC5424,而BSD.My配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="warn" name="MyApp" packages="">
        <Appenders>
            <Syslog name="bsd" host="9.115.114.133" port="514" protocol="UDP" facility="local5"/>
            <Syslog name="RFC5424" format="RFC5424" host="9.115.114.133" 
                protocol="UDP" appName="MyApp" includeMDC="true"
                 facility="LOCAL5" enterpriseNumber="18060" newLine="true"
                  messageId="Audit" id="App"/>    
         </Appenders>
         <Loggers>
         <Logger name="test.main" level="warn">
         <AppenderRef ref="RFC5424"/>
         </Logger>
         <Root level="error">
        <AppenderRef ref="bsd"/>
        </Root>
        </Loggers>

当我运行测试程序时,我总是得到如下错误: 引起:java.lang.IllegalArgumentException:未提供结构化ID名称     在org.apache.logging.log4j.message.StructuredDataId。(StructuredDataId.java:92)     在org.apache.logging.log4j.core.layout.Rfc5424Layout。(Rfc5424Layout.java:139)     在org.apache.logging.log4j.core.layout.Rfc5424Layout.createLayout(Rfc5424Layout.java:657)     在org.apache.logging.log4j.core.appender.SyslogAppender.createAppender(SyslogAppender.java:133)     ......还有20个

2014-11-10 03:21:46,252错误Null对象为Appenders中的Syslog返回。 2014-11-10 03:21:46,267错误无法找到适用于logger test.main的appender RFC5424。

我怎么能摆脱这个错误?谢谢。

1 个答案:

答案 0 :(得分:6)

您需要包含mdcId属性:

<Syslog name="RFC5424" format="RFC5424" host="9.115.114.133" 
        protocol="UDP" appName="MyApp" mdcId="mdc" includeMDC="true"
        facility="LOCAL5" enterpriseNumber="18060" newLine="true"
        messageId="Audit" id="App"/>