java 如何让 GELFJ appender 在 log4j 中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26847569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:45:07  来源:igfitidea点击:

How to get GELFJ appender work in log4j?

javalogginglog4jgraylog2

提问by Jochen

I need to get my Java application writing logging to a Graylog2 server. The application uses a log4j configuration. Several things I have tried to get the logging writing to the Graylog2 server, the things I got working was sending a test message directly to the server, as shown here(first example).

我需要让我的 Java 应用程序将日志记录到 Graylog2 服务器。该应用程序使用 log4j 配置。有几件事情我试图记录写入到该服务器Graylog2,我得到了工作直接发送测试信息到服务器,如图所示的东西在这里(第一个例子)。

Yet, when I write an appender and attach it to the root logger, I always get this error message the first time a log event is to be fired:

然而,当我编写一个 appender 并将其附加到根记录器时,我总是在第一次触发日志事件时收到此错误消息:

log4j:ERROR Could not send GELF message

log4j:ERROR 无法发送 GELF 消息

Nothing then happens on the Graylog2 server side.

Graylog2 服务器端没有任何反应。

The appender I try to get working:

我尝试开始工作的附加程序:

<appender name="graylog2" class="org.graylog2.log.GelfAppender">
    <param name="graylogHost" value="127.0.0.1"/>
    <param name="originHost" value="my.machine.example.com"/>
    <param name="extractStacktrace" value="true"/>
    <param name="addExtendedInformation" value="true"/>
    <param name="facility" value="gelf-java"/>
    <param name="Threshold" value="INFO"/>
    <param name="additionalFields" value="{'environment': 'DEV', 'application': 'MyAPP'}"/>
</appender>

Does anyone have an idea how to get this running?
Any help is highly appreciated!

有谁知道如何让它运行?
任何帮助表示高度赞赏!

回答by adramazany

This work work me:

这项工作对我有用:

add this dependency in your maven pom file

在您的 maven pom 文件中添加此依赖项

       <dependency>
            <groupId>org.graylog2</groupId>
            <artifactId>gelfj</artifactId>
            <version>1.1.13</version>
        </dependency>

and these lines in your log4j.properties

以及 log4j.properties 中的这些行

# Define the graylog2 destination
log4j.appender.graylog2=org.graylog2.log.GelfAppender
log4j.appender.graylog2.graylogHost=192.168.243.23 
log4j.appender.graylog2.port=12201
log4j.appender.graylog2.originHost=loggenerator-server-ip
log4j.appender.graylog2.layout=org.apache.log4j.PatternLayout
log4j.appender.graylog2.additionalFields={'environment': 'DEV', 'application': 'MyAPP'}
log4j.appender.graylog2.extractStacktrace=true
log4j.appender.graylog2.addExtendedInformation=true
log4j.appender.graylog2.facility=gelfappender-test

回答by Girish kumar

Through java code I appended the GelfAppenderand even I was getting the same error as:

通过Java代码,我附加了GelfAppender,甚至我也遇到了与以下相同的错误:

log4j:ERROR Could not send GELF message

The reason I found for the error is that I was not calling the

我发现错误的原因是我没有调用

activateOptions();

This function will set the gelfSender which we had intialized using

此函数将设置我们已使用初始化的 gelfSender

private GelfSender gelfSender;

Once the gelfSenderis set to sumthing the message can be sent to GELF.

一旦gelfSender设置为 sumthing,消息就可以发送到GELF

here is the code how I used to get the GelfAppender

这是我用来获取的代码 GelfAppender

GelfAppender appender = new GelfAppender();
    appender.setName("GrayLogAppender");
    appender.setGraylogHost("localhost");
    appender.setGraylogPort(12201);
    appender.setFacility("gelf-java");
    appender.setOriginHost("localhost");
    appender.setLayout(lyt);
    appender.setExtractStacktrace(true);
    appender.setAddExtendedInformation(true);
    appender.setAdditionalFields("{'environment': 'DEV', 'application':'MyAPP'}");
    appender.activateOptions();

回答by Alex Milman

Look in catalina.out(usually located under tomcat/logs) for error messages related to Gelf

查看catalina.out(通常位于 下tomcat/logs)以获取与 Gelf 相关的错误消息