How to get GELFJ appender work in log4j?

[亡魂溺海] 提交于 2019-12-05 08:08:26

This work work me:

add this dependency in your maven pom file

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

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
Girish kumar

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

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

private GelfSender gelfSender;

Once the gelfSender is set to sumthing the message can be sent to GELF.

here is the code how I used to get the 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();

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!