我正在使用Log4j 2.1版(最新的稳定版)并尝试使用Syslog appender登录到syslog服务器。我使用官方网站(http://logging.apache.org/log4j/2.x/manual/appenders.html#SyslogAppender)提供的配置:
<Syslog name="RFC5424" format="RFC5424" host="localhost" port="514"
protocol="UDP" appName="MyApp" includeMDC="true"
facility="LOCAL0" enterpriseNumber="18060" newLine="true"
messageId="Audit" id="App"/>
当我启动我的应用程序并执行LogManager.getLogger()时,我收到一个错误(堆栈跟踪下面),所以,在我向错误跟踪器报告错误之前,我做错了什么?
014-11-17 18:26:23,640 ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.SyslogAppender for element Syslog. java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:766)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:706)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:698)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:358)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:161)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:359)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:420)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:138)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:147)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:175)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:426)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:408)
at Log.<clinit>(Log.java:7)
Caused by: java.lang.IllegalArgumentException: No structured id name was supplied
at org.apache.logging.log4j.message.StructuredDataId.<init>(StructuredDataId.java:92)
at org.apache.logging.log4j.core.layout.Rfc5424Layout.<init>(Rfc5424Layout.java:139)
at org.apache.logging.log4j.core.layout.Rfc5424Layout.createLayout(Rfc5424Layout.java:657)
at org.apache.logging.log4j.core.appender.SyslogAppender.createAppender(SyslogAppender.java:133)
... 19 more
答案 0 :(得分:8)
您需要包含mdcId
属性。 e.g:
<Syslog name="RFC5424" format="RFC5424" host="localhost" port="514"
protocol="UDP" appName="MyApp" mdcId="mdc" includeMDC="true"
facility="LOCAL0" enterpriseNumber="18060" newLine="true"
messageId="Audit" id="App"/>