Logback和Graylog在Mac上使用syslog无法通信。

huangapple go评论69阅读模式
英文:

Logback and Graylog cannot communicate on a Mac using syslog

问题

以下是您提供的内容的翻译:

我想从Java应用程序将日志消息发送到Graylog,使用slf4j在logback之上,一侧使用logback GELF-appender,另一侧使用Graylog GELF-input。为了测试它,我在Docker容器中运行Graylog(使用Docker for Mac),然后在本地运行我的Java应用程序。我故事的要点是,Graylog GELF-input未能从Java应用程序接收任何内容。不知何故,Java应用程序和Graylog似乎无法进行通信。当我切换到不同的appender/input组合(基于syslog记录的组合)时也是如此。然而,当我从命令行向不同的Graylog输入发送消息,即侦听端口5555的RAW输入时,该消息会被正确接收。

有什么想法是什么问题吗?
以下是我使用GELF的设置:

Java应用程序:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogDemo {

    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(LogDemo.class);
        logger.error("Hello World 2");
    }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>logdemo</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>de.appelgriepsch.logback</groupId>
      <artifactId>logback-gelf-appender</artifactId>
      <version>1.5</version>
    </dependency>
  </dependencies>
</project>

logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="GELF" class="de.appelgriepsch.logback.GelfAppender">
    <server>localhost</server>
    <port>12201</port>
    <protocol>TCP</protocol>
  </appender>

  <root level="error">
    <appender-ref ref="GELF"/>
  </root>
</configuration>

Graylog的Docker启动:

$ docker run --name mongo -d mongo:3
$ docker run --name elasticsearch \
    -e "http.host=0.0.0.0" \
    -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
    -d docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.10
$ docker run --link mongo --link elasticsearch \
    -p 9000:9000 -p 12201:12201 -p 1514:1514 -p 5555:5555 \
    -e GRAYLOG_HTTP_EXTERNAL_URI="http://127.0.0.1:9000/" \
    -d graylog/graylog:3.3

Graylog GELF tcp input(正在运行):

bind_address: 0.0.0.0
decompress_size_limit: 8388608
max_message_size: 2097152
number_worker_threads: 4
override_source: <empty>
port: 12201
recv_buffer_size: 1048576
tcp_keepalive: false
tls_cert_file: <empty>
tls_client_auth: disabled
tls_client_auth_cert_file: <empty>
tls_enable: false
tls_key_file: <empty>
tls_key_password: ********
use_null_delimiter: true

正如所述,当我运行Java应用程序且Graylog作为Docker容器在后台运行时,Graylog未能接收我发送的日志消息。然而,当我在命令行中键入以下内容(在Mac上使用终端时),消息会被Graylog的RAW输入接收:

$ echo "Testmessage" | nc localhost 5555

是否有人知道我做错了什么?

英文:

I want to send logmessages from a Java application to Graylog, using slf4j on top of logback with a logback GELF-appender on one side and a Graylog GELF-input on the other. To test it, i'm running Graylog in a Docker container (using Docker for Mac) and run my Java application locally. The gist of my story is that the Graylog GELF-input does not receive anything from the Java application. Somehow the Java application and Graylog don't seem to be able to communicate. The same applies when i switch to a different appender/input combination (one based on syslog records). However, when echoing a message from the commandline to a different Graylog input, namely the RAW input that's listening to port 5555, that message is received fine.

Any idea what the problem is?
This is my setup using GELF:

Java app:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogDemo {

    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(LogDemo.class);
        logger.error(&quot;Hello World 2&quot;);
    }
}

pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;groupId&gt;org.example&lt;/groupId&gt;
  &lt;artifactId&gt;logdemo&lt;/artifactId&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;de.appelgriepsch.logback&lt;/groupId&gt;
      &lt;artifactId&gt;logback-gelf-appender&lt;/artifactId&gt;
      &lt;version&gt;1.5&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
&lt;/project&gt;

logback.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
  &lt;appender name=&quot;GELF&quot; class=&quot;de.appelgriepsch.logback.GelfAppender&quot;&gt;
    &lt;server&gt;localhost&lt;/server&gt;
    &lt;port&gt;12201&lt;/port&gt;
    &lt;protocol&gt;TCP&lt;/protocol&gt;
  &lt;/appender&gt;

  &lt;root level=&quot;error&quot;&gt;
    &lt;appender-ref ref=&quot;GELF&quot;/&gt;
  &lt;/root&gt;
&lt;/configuration&gt;

Graylog docker startup:

$ docker run --name mongo -d mongo:3
$ docker run --name elasticsearch \
    -e &quot;http.host=0.0.0.0&quot; \
    -e &quot;ES_JAVA_OPTS=-Xms512m -Xmx512m&quot; \
    -d docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.10
$ docker run --link mongo --link elasticsearch \
    -p 9000:9000 -p 12201:12201 -p 1514:1514 -p 5555:5555 \
    -e GRAYLOG_HTTP_EXTERNAL_URI=&quot;http://127.0.0.1:9000/&quot; \
    -d graylog/graylog:3.3

Graylog GELF tcp input (running):

bind_address: 0.0.0.0
decompress_size_limit: 8388608
max_message_size: 2097152
number_worker_threads: 4
override_source: &lt;empty&gt;
port: 12201
recv_buffer_size: 1048576
tcp_keepalive: false
tls_cert_file: &lt;empty&gt;
tls_client_auth: disabled
tls_client_auth_cert_file: &lt;empty&gt;
tls_enable: false
tls_key_file: &lt;empty&gt;
tls_key_password:********
use_null_delimiter: true

As stated, when i run the java app and Graylog is running as a Docker container in the background, Graylog does not receive the logmessage i sent. However, when i type the following on my commandline (using Terminal on Mac), the message IS received by the Graylog RAW input:

$ echo &quot;Testmessage&quot; | nc localhost 5555

Does somebody got a clue what i'm doing wrong?

答案1

得分: 1

我找到了一个解决方案,尽管我不确定问题的确切原因是什么。解决方案是使用不同的 Gelf appender。而不是我上面提到的那个,我现在使用的是以下这个:

<dependency>
  <groupId>de.siegmar</groupId>
  <artifactId>logback-gelf</artifactId>
  <version>2.2.0</version>
</dependency>

这样就解决了问题,但正如我所说的,我不确定为什么我之前使用的那个方法不起作用。

英文:

I found a solution, though i'm not sure what the exact cause of the problem was. The solution was to use a different Gelf appender. Instead of the one i mentioned above, i'm now using the following one:

&lt;dependency&gt;
  &lt;groupId&gt;de.siegmar&lt;/groupId&gt;
  &lt;artifactId&gt;logback-gelf&lt;/artifactId&gt;
  &lt;version&gt;2.2.0&lt;/version&gt;
&lt;/dependency&gt;

That did the trick, but as i said, i'm unsure why the one i used earlier did not work.

huangapple
  • 本文由 发表于 2020年10月12日 19:39:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64317174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定