问题描述
我需要使Java应用程序将日志记录写入Graylog2服务器.该应用程序使用log4j配置.我试图将日志记录写入Graylog2服务器的几件事,而我正在工作的事情是直接向服务器发送测试消息,如此处(第一个示例).
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).
但是,当我编写一个附加程序并将其附加到root记录器时,我总是在第一次触发日志事件时收到此错误消息:
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:
log4j:ERROR无法发送GELF消息
然后在Graylog2服务器端什么也没发生.
Nothing then happens on the Graylog2 server side.
我尝试开始使用的附加器:
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!
推荐答案
这项工作对我有用:
将此依赖项添加到您的maven pom文件中
add this dependency in your maven pom file
<dependency>
<groupId>org.graylog2</groupId>
<artifactId>gelfj</artifactId>
<version>1.1.13</version>
</dependency>
以及您的log4j.properties中的这些行
and these lines in your 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
这篇关于如何在log4j中获得GELFJ追加器的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!