How to get GELFJ appender work in log4j?

14,615

Solution 1

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

Solution 2

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();
Share:
14,615
Jochen
Author by

Jochen

Updated on July 01, 2022

Comments

  • Jochen
    Jochen almost 2 years

    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).

    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 Could not send GELF message

    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!

    • mp911de
      mp911de over 9 years
      This can have two causes: Either your log message is empty, your machine or there is a firewall/GELF service not running. The code you've posted uses UDP. Check using netstat -an whether your GELF port is open.
    • Clarice Bouwer
      Clarice Bouwer over 5 years
      What happens if both criteria are satisfied. I can send UDP packets using the timbre library but org.graylog2.log.GelfAppender doesn't send anything. My configuration is similar to the above and the root level is set to INFO. I am using Clojure and a logback.xml file in env/dev/resources. I have no idea why there is no traffic on that port for GELF but there is for the timbre library. It is also failing silently. I don't know where to find the logs. I am using IntelliJ. Any ideas?