diff --git a/src/main/java/biz/paluch/logging/gelf/intern/MessagePostprocessingErrorReporter.java b/src/main/java/biz/paluch/logging/gelf/intern/MessagePostprocessingErrorReporter.java new file mode 100644 index 000000000..84dac78a7 --- /dev/null +++ b/src/main/java/biz/paluch/logging/gelf/intern/MessagePostprocessingErrorReporter.java @@ -0,0 +1,39 @@ +package biz.paluch.logging.gelf.intern; + +/** + * {@link ErrorReporter} that post-processes the error message if it is {@code null} by using the exception class name as + * fallback. + * + * @author Mark Paluch + * @since 1.11.2 + */ +public class MessagePostprocessingErrorReporter implements ErrorReporter { + + private final ErrorReporter delegate; + + public MessagePostprocessingErrorReporter(ErrorReporter delegate) { + this.delegate = delegate; + } + + @Override + public void reportError(String message, Exception e) { + + String messageToUse = postProcessMessage(message, e); + + delegate.reportError(messageToUse, e); + } + + private static String postProcessMessage(String message, Exception e) { + + if ((message == null || "null".equalsIgnoreCase(message)) && e != null) { + + if (e.getMessage() != null) { + return e.getMessage(); + } + + return e.getClass().getSimpleName(); + } + + return message; + } +} diff --git a/src/main/java/biz/paluch/logging/gelf/jul/GelfLogHandler.java b/src/main/java/biz/paluch/logging/gelf/jul/GelfLogHandler.java index f8f4a72b3..c2a4bcb1a 100644 --- a/src/main/java/biz/paluch/logging/gelf/jul/GelfLogHandler.java +++ b/src/main/java/biz/paluch/logging/gelf/jul/GelfLogHandler.java @@ -45,11 +45,12 @@ public class GelfLogHandler extends Handler implements ErrorReporter { protected volatile GelfSender gelfSender; protected GelfMessageAssembler gelfMessageAssembler; + private final ErrorReporter errorReporter = new MessagePostprocessingErrorReporter(this); public GelfLogHandler() { super(); - RuntimeContainer.initialize(this); + RuntimeContainer.initialize(errorReporter); gelfMessageAssembler = createGelfMessageAssembler(); initializeDefaultFields(); @@ -133,7 +134,7 @@ public void publish(final LogRecord record) { } protected GelfSender createGelfSender() { - return GelfSenderFactory.createSender(gelfMessageAssembler, this, Collections.EMPTY_MAP); + return GelfSenderFactory.createSender(gelfMessageAssembler, errorReporter, Collections.EMPTY_MAP); } @Override diff --git a/src/main/java/biz/paluch/logging/gelf/log4j/GelfLogAppender.java b/src/main/java/biz/paluch/logging/gelf/log4j/GelfLogAppender.java index bc1f69276..3cb1c0c51 100644 --- a/src/main/java/biz/paluch/logging/gelf/log4j/GelfLogAppender.java +++ b/src/main/java/biz/paluch/logging/gelf/log4j/GelfLogAppender.java @@ -65,11 +65,12 @@ public class GelfLogAppender extends AppenderSkeleton implements ErrorReporter { protected GelfSender gelfSender; protected MdcGelfMessageAssembler gelfMessageAssembler; + private final ErrorReporter errorReporter = new MessagePostprocessingErrorReporter(this); public GelfLogAppender() { super(); - RuntimeContainer.initialize(this); + RuntimeContainer.initialize(errorReporter); gelfMessageAssembler = new MdcGelfMessageAssembler(); gelfMessageAssembler.addFields(LogMessageField.getDefaultMapping(Time, Severity, ThreadName, SourceClassName, SourceMethodName, SourceLineNumber, SourceSimpleClassName, LoggerName, NDC, Server)); @@ -98,9 +99,10 @@ protected void append(LoggingEvent event) { } protected GelfSender createGelfSender() { - return GelfSenderFactory.createSender(gelfMessageAssembler, this, Collections.EMPTY_MAP); + return GelfSenderFactory.createSender(gelfMessageAssembler, errorReporter, Collections.EMPTY_MAP); } + @Override public void reportError(String message, Exception exception) { errorHandler.error(message, exception, 0); } diff --git a/src/main/java/biz/paluch/logging/gelf/log4j2/GelfLogAppender.java b/src/main/java/biz/paluch/logging/gelf/log4j2/GelfLogAppender.java index 3869e3b80..af9bbb9f6 100644 --- a/src/main/java/biz/paluch/logging/gelf/log4j2/GelfLogAppender.java +++ b/src/main/java/biz/paluch/logging/gelf/log4j2/GelfLogAppender.java @@ -191,11 +191,13 @@ public GelfLogAppender(String name, Filter filter, MdcGelfMessageAssembler gelfM super(name, filter, null, ignoreExceptions); this.gelfMessageAssembler = gelfMessageAssembler; - if (ignoreExceptions) { - errorReporter = ERROR_REPORTER; - } else { - errorReporter = PROPAGATING_ERROR_REPORTER; - } + ErrorReporter errorReporter = getErrorReporter(ignoreExceptions); + + this.errorReporter = new MessagePostprocessingErrorReporter(errorReporter); + } + + private ErrorReporter getErrorReporter(boolean ignoreExceptions) { + return ignoreExceptions ? ERROR_REPORTER : PROPAGATING_ERROR_REPORTER; } @PluginFactory diff --git a/src/main/java/biz/paluch/logging/gelf/logback/GelfLogbackAppender.java b/src/main/java/biz/paluch/logging/gelf/logback/GelfLogbackAppender.java index 767a81dbc..00917016d 100644 --- a/src/main/java/biz/paluch/logging/gelf/logback/GelfLogbackAppender.java +++ b/src/main/java/biz/paluch/logging/gelf/logback/GelfLogbackAppender.java @@ -1,26 +1,13 @@ package biz.paluch.logging.gelf.logback; -import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.LoggerName; -import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.Marker; -import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.Severity; -import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.SourceClassName; -import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.SourceLineNumber; -import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.SourceMethodName; -import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.SourceSimpleClassName; -import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.ThreadName; -import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.Time; +import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.*; import java.util.Collections; import biz.paluch.logging.RuntimeContainer; import biz.paluch.logging.gelf.LogMessageField; import biz.paluch.logging.gelf.MdcGelfMessageAssembler; -import biz.paluch.logging.gelf.intern.Closer; -import biz.paluch.logging.gelf.intern.ConfigurationSupport; -import biz.paluch.logging.gelf.intern.ErrorReporter; -import biz.paluch.logging.gelf.intern.GelfMessage; -import biz.paluch.logging.gelf.intern.GelfSender; -import biz.paluch.logging.gelf.intern.GelfSenderFactory; +import biz.paluch.logging.gelf.intern.*; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.AppenderBase; @@ -77,6 +64,7 @@ public class GelfLogbackAppender extends AppenderBase implements protected GelfSender gelfSender; protected MdcGelfMessageAssembler gelfMessageAssembler; + private final ErrorReporter errorReporter = new MessagePostprocessingErrorReporter(this); public GelfLogbackAppender() { super(); @@ -111,7 +99,7 @@ protected void append(ILoggingEvent event) { public void start() { if (null == gelfSender) { - RuntimeContainer.initialize(this); + RuntimeContainer.initialize(errorReporter); gelfSender = createGelfSender(); } @@ -130,9 +118,10 @@ public void stop() { } protected GelfSender createGelfSender() { - return GelfSenderFactory.createSender(gelfMessageAssembler, this, Collections.EMPTY_MAP); + return GelfSenderFactory.createSender(gelfMessageAssembler, errorReporter, Collections.EMPTY_MAP); } + @Override public void reportError(String message, Exception exception) { addError(message, exception); } diff --git a/src/main/java/biz/paluch/logging/gelf/standalone/DefaultGelfSenderConfiguration.java b/src/main/java/biz/paluch/logging/gelf/standalone/DefaultGelfSenderConfiguration.java index b38e99614..3ca755b28 100644 --- a/src/main/java/biz/paluch/logging/gelf/standalone/DefaultGelfSenderConfiguration.java +++ b/src/main/java/biz/paluch/logging/gelf/standalone/DefaultGelfSenderConfiguration.java @@ -5,10 +5,11 @@ import biz.paluch.logging.gelf.intern.ErrorReporter; import biz.paluch.logging.gelf.intern.GelfSenderConfiguration; +import biz.paluch.logging.gelf.intern.MessagePostprocessingErrorReporter; /** * Default Gelf sender configuration for standalone use. - * + * * @author Mark Paluch * @since 21.07.14 17:34 */ @@ -20,7 +21,7 @@ public class DefaultGelfSenderConfiguration implements GelfSenderConfiguration { protected Map specificConfigurations = new HashMap(); public DefaultGelfSenderConfiguration() { - errorReporter = new Slf4jErrorReporter(); + errorReporter = new MessagePostprocessingErrorReporter(new Slf4jErrorReporter()); } public DefaultGelfSenderConfiguration(ErrorReporter errorReporter) { diff --git a/src/test/java/biz/paluch/logging/gelf/intern/MessagePostprocessingErrorReporterTests.java b/src/test/java/biz/paluch/logging/gelf/intern/MessagePostprocessingErrorReporterTests.java new file mode 100644 index 000000000..cf5373cf2 --- /dev/null +++ b/src/test/java/biz/paluch/logging/gelf/intern/MessagePostprocessingErrorReporterTests.java @@ -0,0 +1,48 @@ +package biz.paluch.logging.gelf.intern; + +import static org.mockito.Mockito.verify; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; + +import external.MockitoExtension; + +/** + * @author Mark Paluch + */ +@ExtendWith(MockitoExtension.class) +class MessagePostprocessingErrorReporterTests { + + @Mock + private ErrorReporter target; + + @Test + void shouldRetainOriginalMessage() { + + IOException e = new IOException(); + new MessagePostprocessingErrorReporter(target).reportError("foo", e); + + verify(target).reportError("foo", e); + } + + @Test + void shouldReplaceNullMessageWithExceptionClassName() { + + IOException e = new IOException(); + new MessagePostprocessingErrorReporter(target).reportError(null, e); + + verify(target).reportError("IOException", e); + } + + @Test + void shouldReplaceNullMessageWithExceptionMessage() { + + IOException e = new IOException("foo"); + new MessagePostprocessingErrorReporter(target).reportError(null, e); + + verify(target).reportError("foo", e); + } +}