From 5e5a5f034076b9177f4ab0d14364737350553d84 Mon Sep 17 00:00:00 2001 From: Oliver Siegmar Date: Fri, 30 Aug 2024 08:16:27 +0200 Subject: [PATCH] add support for external encoders --- CHANGELOG.md | 4 ++++ build.gradle.kts | 2 +- .../de/siegmar/logbackgelf/AbstractGelfAppender.java | 9 +++++---- .../java/de/siegmar/logbackgelf/GelfHttpAppender.java | 9 +++++---- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40c85c5..ee95d35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] +### Added +- Support for external encoders like Spring's StructuredLogEncoder + ## [6.0.2] - 2024-08-24 ### Changed - Improve performance of number conversion in GelfEncoder (#108); Thanks to [@deathy](https://github.com/deathy) diff --git a/build.gradle.kts b/build.gradle.kts index 8671da3..2dc2df0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { } group = "de.siegmar" -version = "6.0.2" +version = "6.1.0-SNAPSHOT" java { toolchain { diff --git a/src/main/java/de/siegmar/logbackgelf/AbstractGelfAppender.java b/src/main/java/de/siegmar/logbackgelf/AbstractGelfAppender.java index 9359d9c..9d034a3 100644 --- a/src/main/java/de/siegmar/logbackgelf/AbstractGelfAppender.java +++ b/src/main/java/de/siegmar/logbackgelf/AbstractGelfAppender.java @@ -23,6 +23,7 @@ import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.UnsynchronizedAppenderBase; +import ch.qos.logback.core.encoder.Encoder; public abstract class AbstractGelfAppender extends UnsynchronizedAppenderBase { @@ -38,7 +39,7 @@ public abstract class AbstractGelfAppender extends UnsynchronizedAppenderBase encoder; public String getGraylogHost() { return graylogHost; @@ -56,11 +57,11 @@ public void setGraylogPort(final int graylogPort) { this.graylogPort = graylogPort; } - public GelfEncoder getEncoder() { + public Encoder getEncoder() { return encoder; } - public void setEncoder(final GelfEncoder encoder) { + public void setEncoder(final Encoder encoder) { this.encoder = encoder; } @@ -76,7 +77,7 @@ public final void start() { encoder = new GelfEncoder(); encoder.setContext(getContext()); encoder.start(); - } else if (encoder.isAppendNewline()) { + } else if (encoder instanceof GelfEncoder && ((GelfEncoder) encoder).isAppendNewline()) { addError("Newline separator must not be enabled in layout"); return; } diff --git a/src/main/java/de/siegmar/logbackgelf/GelfHttpAppender.java b/src/main/java/de/siegmar/logbackgelf/GelfHttpAppender.java index 8a3f5b1..1e06a0a 100644 --- a/src/main/java/de/siegmar/logbackgelf/GelfHttpAppender.java +++ b/src/main/java/de/siegmar/logbackgelf/GelfHttpAppender.java @@ -38,6 +38,7 @@ import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.UnsynchronizedAppenderBase; +import ch.qos.logback.core.encoder.Encoder; import de.siegmar.logbackgelf.compressor.Compressor; @SuppressWarnings("checkstyle:ClassFanOutComplexity") @@ -90,7 +91,7 @@ public class GelfHttpAppender extends UnsynchronizedAppenderBase /** * The encoder to use for encoding log messages. */ - private GelfEncoder encoder; + private Encoder encoder; private Compressor compressor; @@ -158,11 +159,11 @@ public void setHttpClient(final HttpClient httpClient) { this.httpClient = httpClient; } - public GelfEncoder getEncoder() { + public Encoder getEncoder() { return encoder; } - public void setEncoder(final GelfEncoder encoder) { + public void setEncoder(final Encoder encoder) { this.encoder = encoder; } @@ -177,7 +178,7 @@ public void start() { encoder = new GelfEncoder(); encoder.setContext(getContext()); encoder.start(); - } else if (encoder.isAppendNewline()) { + } else if (encoder instanceof GelfEncoder && ((GelfEncoder) encoder).isAppendNewline()) { addError("Newline separator must not be enabled in layout"); return; }