From 50a1897fe828243b6bb1c1f818a447bb4570bbf3 Mon Sep 17 00:00:00 2001 From: Jens Rieks Date: Tue, 10 Jan 2023 11:24:11 +0100 Subject: [PATCH] fixes #10 fixes internetitem/logback-elasticsearch-appender#63 fixes internetitem/logback-elasticsearch-appender#86 --- .../AbstractElasticsearchPublisher.java | 14 ++++++++------ .../ClassicElasticsearchPublisher.java | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/agido/logback/elasticsearch/AbstractElasticsearchPublisher.java b/src/main/java/com/agido/logback/elasticsearch/AbstractElasticsearchPublisher.java index 3d12ceb..edcab2d 100644 --- a/src/main/java/com/agido/logback/elasticsearch/AbstractElasticsearchPublisher.java +++ b/src/main/java/com/agido/logback/elasticsearch/AbstractElasticsearchPublisher.java @@ -206,15 +206,17 @@ private void serializeIndexString(JsonGenerator gen, T event) throws IOException } private void serializeEvent(JsonGenerator gen, T event, List> propertyList) throws IOException { - gen.writeStartObject(); + try { + gen.writeStartObject(); - serializeCommonFields(gen, event); + serializeCommonFields(gen, event); - for (AbstractPropertyAndEncoder pae : propertyList) { - propertySerializer.serializeProperty(gen, event, pae); + for (AbstractPropertyAndEncoder pae : propertyList) { + propertySerializer.serializeProperty(gen, event, pae); + } + } finally { + gen.writeEndObject(); } - - gen.writeEndObject(); } protected abstract void serializeCommonFields(JsonGenerator gen, T event) throws IOException; diff --git a/src/main/java/com/agido/logback/elasticsearch/ClassicElasticsearchPublisher.java b/src/main/java/com/agido/logback/elasticsearch/ClassicElasticsearchPublisher.java index 2c5a2a5..b329c1e 100644 --- a/src/main/java/com/agido/logback/elasticsearch/ClassicElasticsearchPublisher.java +++ b/src/main/java/com/agido/logback/elasticsearch/ClassicElasticsearchPublisher.java @@ -34,7 +34,7 @@ protected void serializeCommonFields(JsonGenerator gen, ILoggingEvent event) thr gen.writeRawValue(event.getFormattedMessage()); } else { String formattedMessage = event.getFormattedMessage(); - if (settings.getMaxMessageSize() > 0 && formattedMessage.length() > settings.getMaxMessageSize()) { + if (settings.getMaxMessageSize() > 0 && formattedMessage != null && formattedMessage.length() > settings.getMaxMessageSize()) { formattedMessage = formattedMessage.substring(0, settings.getMaxMessageSize()) + ".."; } gen.writeObjectField("message", formattedMessage);