Skip to content

Commit

Permalink
use Optional in mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
osiegmar committed Jan 17, 2021
1 parent aa162e3 commit dca8603
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package de.siegmar.logbackgelf.mappers;

import java.util.Optional;
import java.util.function.BiConsumer;

import ch.qos.logback.classic.spi.ILoggingEvent;
Expand All @@ -34,12 +35,9 @@ protected AbstractFixedNameFieldMapper(final String fieldName) {

@Override
public void mapField(final ILoggingEvent event, final BiConsumer<String, T> valueHandler) {
final T value = getValue(event);
if (value != null) {
valueHandler.accept(fieldName, value);
}
getValue(event).ifPresent(v -> valueHandler.accept(fieldName, v));
}

protected abstract T getValue(ILoggingEvent event);
protected abstract Optional<T> getValue(ILoggingEvent event);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package de.siegmar.logbackgelf.mappers;

import java.util.Optional;
import java.util.function.BiConsumer;

import ch.qos.logback.classic.spi.ILoggingEvent;
Expand All @@ -28,17 +29,15 @@ public class CallerDataFieldMapper implements GelfFieldMapper<Object> {

@Override
public void mapField(final ILoggingEvent event, final BiConsumer<String, Object> valueHandler) {
final StackTraceElement[] callerData = event.getCallerData();
if (callerData == null || callerData.length == 0) {
return;
}

final StackTraceElement first = callerData[0];

valueHandler.accept("source_file_name", first.getFileName());
valueHandler.accept("source_method_name", first.getMethodName());
valueHandler.accept("source_class_name", first.getClassName());
valueHandler.accept("source_line_number", first.getLineNumber());
Optional.ofNullable(event.getCallerData())
.filter(s -> s.length > 0)
.map(s -> s[0])
.ifPresent(first -> {
valueHandler.accept("source_file_name", first.getFileName());
valueHandler.accept("source_method_name", first.getMethodName());
valueHandler.accept("source_class_name", first.getClassName());
valueHandler.accept("source_line_number", first.getLineNumber());
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package de.siegmar.logbackgelf.mappers;

import java.util.Iterator;
import java.util.Optional;

import org.slf4j.Marker;

Expand All @@ -32,9 +33,9 @@ public MarkerFieldMapper(final String fieldName) {
}

@Override
protected String getValue(final ILoggingEvent event) {
final Marker marker = event.getMarker();
return marker != null ? buildMarkerStr(marker) : null;
protected Optional<String> getValue(final ILoggingEvent event) {
return Optional.ofNullable(event.getMarker())
.map(MarkerFieldMapper::buildMarkerStr);
}

private static String buildMarkerStr(final Marker marker) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package de.siegmar.logbackgelf.mappers;

import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;

import ch.qos.logback.classic.spi.ILoggingEvent;
Expand All @@ -36,16 +37,14 @@ public MdcDataFieldMapper(final GelfFieldHelper fieldHelper) {

@Override
public void mapField(final ILoggingEvent event, final BiConsumer<String, Object> valueHandler) {
final Map<String, String> mdcProperties = event.getMDCPropertyMap();
if (mdcProperties == null || mdcProperties.isEmpty()) {
return;
}

for (final Map.Entry<String, String> entry : mdcProperties.entrySet()) {
if (fieldHelper.isValidFieldName(entry.getKey())) {
valueHandler.accept(entry.getKey(), fieldHelper.convertToNumberIfNeeded(entry.getValue()));
}
}
Optional.ofNullable(event.getMDCPropertyMap())
.ifPresent(p -> {
for (final Map.Entry<String, String> entry : p.entrySet()) {
if (fieldHelper.isValidFieldName(entry.getKey())) {
valueHandler.accept(entry.getKey(), fieldHelper.convertToNumberIfNeeded(entry.getValue()));
}
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package de.siegmar.logbackgelf.mappers;

import java.util.Optional;
import java.util.function.BiConsumer;

import ch.qos.logback.classic.spi.ILoggingEvent;
Expand All @@ -29,15 +30,11 @@ public class RootExceptionDataFieldMapper implements GelfFieldMapper<Object> {

@Override
public void mapField(final ILoggingEvent event, final BiConsumer<String, Object> valueHandler) {
final IThrowableProxy throwableProxy = event.getThrowableProxy();
final IThrowableProxy rootException = getRootException(throwableProxy);
if (rootException == null) {
return;
}

valueHandler.accept("root_cause_class_name", rootException.getClassName());
valueHandler.accept("root_cause_message", rootException.getMessage());

Optional.ofNullable(getRootException(event.getThrowableProxy()))
.ifPresent(rootException -> {
valueHandler.accept("root_cause_class_name", rootException.getClassName());
valueHandler.accept("root_cause_message", rootException.getMessage());
});
}

private IThrowableProxy getRootException(final IThrowableProxy throwableProxy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package de.siegmar.logbackgelf.mappers;

import java.util.Optional;
import java.util.function.Function;

import ch.qos.logback.classic.spi.ILoggingEvent;
Expand All @@ -33,8 +34,8 @@ public SimpleFieldMapper(final String fieldName, final Function<ILoggingEvent, T
}

@Override
protected T getValue(final ILoggingEvent event) {
return valueGetter.apply(event);
protected Optional<T> getValue(final ILoggingEvent event) {
return Optional.ofNullable(valueGetter.apply(event));
}

}

0 comments on commit dca8603

Please sign in to comment.