我试图以JSON格式将Java日志记录到Syslog中,但我发现了一个奇怪的问题,Syslog标头没有按预期设置。
我正在Mac上测试这一点,并使用wireshark在数据包发送到UDP 514端口(通过环回接口)时抓取数据包
我的logback.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<conversionRule conversionWord="syslogStart" converterClass="ch.qos.logback.classic.pattern.SyslogStartConverter"/>
<appender name="stash" class="net.logstash.logback.appender.LogstashSocketAppender">
<host>localhost</host>
<port>514</port>
<prefix class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%syslogStart{LOCAL5}</pattern>
</layout>
</prefix>
</appender>
<root level="INFO">
<appender-ref ref="stash"/>
</root>
</configuration>
当我查看wireshark的输出时,我只看到JSON被记录(没有给出PRI头字段)
[truncated]Syslog message: (unknown): {"@timestamp":"2016-03-22T12:13:37.270+11:00","@version":1,"message":"Started App in 4.327 seconds (JVM running for 4.92)","logger_name":"au.com.xxx.App","threa
如果我切换到标准的logback Syslog appender(非JSON输出)
...
<appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
<syslogHost>127.0.0.1</syslogHost>
<Facility>LOCAL5</Facility>
<SuffixPattern>%-5level MyApp %logger %msg</SuffixPattern>
</appender>
...
我确实看到了正确的头设施原始值<174>和wireshark数据包中的解析值LOCAL5.INFO
Syslog message: LOCAL5.INFO: Mar 22 12:31:03 sbmelmac-06390.local INFO App au.com.App Started App in 11.292 seconds (JVM running for 29.336)
syslog头是必需的(在syslog conf中),以便将日志消息路由到正确的文件,因此如果没有它,我就无法根据设施过滤器过滤掉日志条目。
我使用的是SpringBoot(1.2.7)(使用logback 1.1.3)、apachecamel(2.16.1)和logstash logback编码器(4.6)
当我在调试中运行时,看起来从未调用SyslogStartConverter.convert方法。
干杯Roy
经过一番挠头之后,这个例子的包装LayoutWrappingEncoder部分似乎出现了问题。
当我使用最新的SpringBoot版本时,一个logback配置异常会阻止应用程序启动(即使出现异常,以前的版本也不会阻止应用程序的启动)。
Application startup failed
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.util.PropertySetter@10d307f1 - A "ch.qos.logback.core.encoder.LayoutWrappingEncoder" object is not assignable to a "ch.qos.logback.core.Layout" variable.
ERROR in ch.qos.logback.core.joran.util.PropertySetter@10d307f1 - The class "ch.qos.logback.core.Layout" was loaded by
ERROR in ch.qos.logback.core.joran.util.PropertySetter@10d307f1 - [sun.misc.Launcher$AppClassLoader@73d16e93] whereas object of type
ERROR in ch.qos.logback.core.joran.util.PropertySetter@10d307f1 - "ch.qos.logback.core.encoder.LayoutWrappingEncoder" was loaded by [sun.misc.Launcher$AppClassLoader@73d16e93].
因此,如果我删除包装LayoutWrappingEncoder并在前缀中使用Pattern实现,代码就会工作,并且我会在syslog标头中看到正确的Facility详细信息。
<conversionRule conversionWord="syslogStart" converterClass="ch.qos.logback.classic.pattern.SyslogStartConverter"/>
<appender name="JSON_SYSLOG" class="net.logstash.logback.appender.LogstashSocketAppender">
<host>127.0.0.1</host>
<port>514</port>
<prefix class="ch.qos.logback.classic.PatternLayout">
<pattern>%syslogStart{LOCAL5}</pattern>
</prefix>
</appender>
并且得到的wireshark分组具有设施信息(LOCAL5.INFO|ERROR)
[truncated]Syslog message: LOCAL5.INFO: Mar 22 14:12:18 sbmelmac-06390.local {"@timestamp":"2016-03-22T14:12:18.494+11:00","@version":1,"message":"Started App in 4.597 seconds (JVM running for 5.18)","logger":"au.com.myapp.