t0xa / gelfj

Graylog Extended Log Format (GELF) implementation in Java and log4j appender without any dependencies.
https://github.com/t0xa/gelfj/wiki
Other
186 stars 116 forks source link

NullPointerException if only amqp is defined #126

Open medoctron opened 5 years ago

medoctron commented 5 years ago

If you configure for example the following:

handlers=org.graylog2.logging.GelfHandler
.handlers=org.graylog2.logging.GelfHandler
.level = ALL

org.graylog2.logging.GelfHandler.level = ALL

org.graylog2.logging.GelfHandler.amqpURI=amqp://just.a.test
org.graylog2.logging.GelfHandler.amqpExchangeName=messages
org.graylog2.logging.GelfHandler.amqpRoutingKey=gelfudp
org.graylog2.logging.GelfHandler.amqpMaxRetries=5

org.graylog2.logging.GelfHandler.extractStacktrace = true

then the code will throw a NullPointerException in org.graylog2.logging.GelfHandler.publish(LogRecord) at location

if (graylogHost.startsWith("tcp:")) {

graylogHost is null in the described case.

Here's a test case to reproduce the bug, using the configuration from above as file "logging-only-amqp-test.properties":

@Test
public void testConfigureOnlyAmqpLogging() throws SecurityException, IOException {
    // In versions <= 1.1.16 there's a bug that throws a NullPointerException if you
    // only configure amqp but leave graylogHost empty
    InputStream is = GelfHandlerTest.class.getResourceAsStream("logging-only-amqp-test.properties");
    LogManager.getLogManager().readConfiguration(is);

    GelfHandler gelfHandler = new GelfHandler();
    gelfHandler.publish(new LogRecord(Level.INFO, "testMessage"));
}