本文介绍了无法让Log4J SocketAppender工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java项目使用Log4J2。目前,它已成功将日志写入文件。现在,我正在尝试通过Socket Appender将日志推送到LogStash。不幸的是,我对这些努力没有任何成功。这时,我正在看两件:我的log4j2.xml文件和我的logstash配置文件。我在这里提供了两个,希望有人可以帮我识别我的问题。

My Java project uses Log4J2. Currently, it is successfully writing logs to a file. Now, I'm trying to push the logs to LogStash via a Socket Appender. Unfortunately, I am not having any success with these efforts. At this time, I'm looking at two pieces: my log4j2.xml file and my logstash config file. I've provided both here in hopes that someone can help me identify my problem.

log4j2.xml

<Configuration status="WARN" monitorInterval="30">
  <Appenders>
    <Socket name="A1" host="0.0.0.0" port="4560">
      <SerializedLayout/>
    </Socket>

    <RollingRandomAccessFile name="RollingFile" fileName="/logs/server.log"
                             filePattern="/logs/$${date:yyyy-MM}/server-%d{yyyy-MM-dd-HH}-%i.log.gz">
      <JSONLayout complete="false"></JSONLayout>
      <Policies>
        <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
        <SizeBasedTriggeringPolicy size="100 MB"/>
      </Policies>
    </RollingRandomAccessFile>
    <Async name="AsyncFile">
      <AppenderRef ref="RollingFile"/>
    </Async>
  </Appenders>
  <Loggers>
    <Logger name="com.company" level="trace" additivity="false">
      <AppenderRef ref="AsyncFile"/>
    </Logger>
    <Root level="trace">
      <AppenderRef ref="AsyncFile"/>
    </Root>
  </Loggers>
</Configuration>

这是我的log4j2配置。这是我的logstash配置:

That was my log4j2 configuration. Here is my logstash configuration:

logstash.conf

input {
  log4j {
    mode => "server"
    type => "log4j"
    host => "0.0.0.0"
    port => "4560"
  }
}
output {
  stdout {
    codec => "json"
  }
}

我从命令行启动logstash使用 logstash-1.4.0 / bin / logstash --config /etc/logstash/logstash.conf --debug 。我没有看到任何错误。同时,我没有看到任何日志写入控制台窗口。我知道日志应该出现,因为它们出现在我的滚动server.log文件中,该文件被配置为log4j中的第二个appender。

I start logstash from the command-line using logstash-1.4.0/bin/logstash --config /etc/logstash/logstash.conf --debug. I do not see any errors. At the same time, I do not see any logs written to the console window. I know that logs should appear because they are appearing in my rolling server.log file that was configured as the second appender in log4j.

我做错了什么?我一直在修补它3天。

What am I doing wrong? I've been tinkering with this for 3 days.

推荐答案

Logstash中的log4j输入与Log4j2不兼容。 Log4j2使用新格式来序列化日志数据。

The log4j input in Logstash is not compatible with Log4j2. Log4j2 uses a new format to serialize the log data.

但是有一个插件可以解析Log4j2套接字:

There is however a plugin which enables parsing of Log4j2 sockets:https://github.com/jurmous/logstash-log4j2

In logstash 1.5+可以从以下位置安装:

In logstash 1.5+ it can be installed from:https://rubygems.org/gems/logstash-input-log4j2

这篇关于无法让Log4J SocketAppender工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:42