internetitem / logback-elasticsearch-appender

Logback Elasticsearch Appender
Other
233 stars 137 forks source link

logger.info(null) throws NPE when publishing #86

Open meganalnico opened 2 years ago

meganalnico commented 2 years ago

logger.info(null) throws NPE when publishing with version 1.6

java.lang.NullPointerException: null at com.internetitem.logback.elasticsearch.ClassicElasticsearchPublisher
.serializeCommonFields(ClassicElasticsearchPublisher.java:37)

Also es-logger reports this:

2022-05-10 17:48:27,075 WARN  [es-writer-6] es-error-logger: 
Failed to send events to Elasticsearch: Got response code [400] from server with data 
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\\n]"}],"type":"illegal_argument_exception","reason":"
The bulk request must be terminated by a newline [\\n]"},"status":400}

ClassicElasticsearchPublisher line 37

String formattedMessage = event.getFormattedMessage();
if (this.settings.getMaxMessageSize() > 0 && formattedMessage.length() > this.settings.getMaxMessageSize()) {
    formattedMessage = formattedMessage.substring(0, this.settings.getMaxMessageSize()) + "..";
} 

It's very possible that a value is unexpectedly null. One might fix it thus:

String formattedMessage = String.valueOf(event.getFormattedMessage());

This way in the case of a null value the string "null" is placed in the message.

meganalnico commented 2 years ago

I have an even better fix. This way, no matter what happens you at least have valid JSON.

private void serializeEvent(JsonGenerator gen, T event, List<AbstractPropertyAndEncoder<T>> propertyList) throws IOException {
    try {
        gen.writeStartObject();

        serializeCommonFields(gen, event);
        for (AbstractPropertyAndEncoder<T> pae : propertyList) {
            propertySerializer.serializeProperty(gen, event, pae);
    }
    } finally {
    gen.writeEndObject();
    }
}
gavenkoa commented 1 year ago

Probably the same: https://github.com/internetitem/logback-elasticsearch-appender/issues/63