Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
Report exceptions without message with their exception class name #126
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Jan 29, 2018
1 parent 786a6d1 commit e5df6f7
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 3 additions & 2 deletions src/main/java/biz/paluch/logging/gelf/jul/GelfLogHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -77,6 +64,7 @@ public class GelfLogbackAppender extends AppenderBase<ILoggingEvent> implements

protected GelfSender gelfSender;
protected MdcGelfMessageAssembler gelfMessageAssembler;
private final ErrorReporter errorReporter = new MessagePostprocessingErrorReporter(this);

public GelfLogbackAppender() {
super();
Expand Down Expand Up @@ -111,7 +99,7 @@ protected void append(ILoggingEvent event) {
public void start() {

if (null == gelfSender) {
RuntimeContainer.initialize(this);
RuntimeContainer.initialize(errorReporter);
gelfSender = createGelfSender();
}

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -20,7 +21,7 @@ public class DefaultGelfSenderConfiguration implements GelfSenderConfiguration {
protected Map<String, Object> specificConfigurations = new HashMap<String, Object>();

public DefaultGelfSenderConfiguration() {
errorReporter = new Slf4jErrorReporter();
errorReporter = new MessagePostprocessingErrorReporter(new Slf4jErrorReporter());
}

public DefaultGelfSenderConfiguration(ErrorReporter errorReporter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit e5df6f7

Please sign in to comment.