Skip to content

Commit

Permalink
Add support for key value pairs (#133)
Browse files Browse the repository at this point in the history
* Added key value pairs

* Added null safety

* Simplified import statement

* Corrected import statements
  • Loading branch information
Hakky54 authored Nov 19, 2024
1 parent 17d40ae commit 75116ea
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/main/java/nl/altindag/log/mapper/LogEventMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* <strong>NOTE:</strong>
Expand All @@ -50,6 +52,9 @@ public LogEvent apply(ILoggingEvent iLoggingEvent) {
String threadName = iLoggingEvent.getThreadName();
ZonedDateTime timeStamp = ZonedDateTime.ofInstant(Instant.ofEpochMilli(iLoggingEvent.getTimeStamp()), ZoneOffset.UTC);
Map<String, String> diagnosticContext = Collections.unmodifiableMap(iLoggingEvent.getMDCPropertyMap());
List<Map.Entry<String, Object>> keyValuePairs = iLoggingEvent.getKeyValuePairs() == null ? Collections.emptyList() : iLoggingEvent.getKeyValuePairs().stream()
.map(keyValuePair -> new SimpleImmutableEntry<>(keyValuePair.key, keyValuePair.value))
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));

List<Object> arguments = Optional.ofNullable(iLoggingEvent.getArgumentArray())
.map(Arrays::asList)
Expand All @@ -71,7 +76,8 @@ public LogEvent apply(ILoggingEvent iLoggingEvent) {
timeStamp,
arguments,
throwable,
diagnosticContext
diagnosticContext,
keyValuePairs
);
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/nl/altindag/log/model/LogEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public final class LogEvent {
private final List<Object> arguments;
private final Throwable throwable;
private final Map<String, String> diagnosticContext;
private final List<Map.Entry<String, Object>> keyValuePairs;

public LogEvent(String message,
String formattedMessage,
Expand All @@ -44,7 +45,8 @@ public LogEvent(String message,
ZonedDateTime timeStamp,
List<Object> arguments,
Throwable throwable,
Map<String, String> diagnosticContext) {
Map<String, String> diagnosticContext,
List<Map.Entry<String, Object>> keyValuePairs) {

this.message = Objects.requireNonNull(message);
this.formattedMessage = Objects.requireNonNull(formattedMessage);
Expand All @@ -55,6 +57,7 @@ public LogEvent(String message,
this.throwable = throwable;
this.arguments = arguments;
this.diagnosticContext = diagnosticContext;
this.keyValuePairs = keyValuePairs;
}

public String getMessage() {
Expand Down Expand Up @@ -93,6 +96,10 @@ public Map<String, String> getDiagnosticContext() {
return diagnosticContext;
}

public List<Map.Entry<String, Object>> getKeyValuePairs() {
return keyValuePairs;
}

@Override
public String toString() {
return "LogEvent{" +
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/nl/altindag/log/LogCaptorShould.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import nl.altindag.log.service.slfj4.ServiceWithSlf4j;
import nl.altindag.log.service.slfj4.ServiceWithSlf4jAndCustomException;
import nl.altindag.log.service.slfj4.ServiceWithSlf4jAndMdcHeaders;
import nl.altindag.log.service.slfj4.ServiceWithSlf4jWhileUsingKeyValuePairs;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -59,8 +60,10 @@
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Spliterator;
Expand Down Expand Up @@ -516,6 +519,26 @@ void captureMdcHeadersWhereSlf4jIsUsed() {
assertDiagnosticContext(logCaptor, "test-slf4j-mdc", "hello-slf4j");
}

@Test
void captureLoggingEventsContainingKeyValuePairs() {
logCaptor = LogCaptor.forClass(ServiceWithSlf4jWhileUsingKeyValuePairs.class);

Service service = new ServiceWithSlf4jWhileUsingKeyValuePairs();
service.sayHello();

List<LogEvent> logEvents = logCaptor.getLogEvents();
assertThat(logEvents).hasSize(1);

LogEvent logEvent = logEvents.get(0);
assertThat(logEvent.getMessage()).isEqualTo("My grocery list");

List<Map.Entry<String, Object>> keyValuePairs = logEvent.getKeyValuePairs();
assertThat(keyValuePairs)
.hasSize(2)
.contains(new SimpleImmutableEntry<>("fruit", "apple"))
.contains(new SimpleImmutableEntry<>("vegetable", "tomato"));
}

@Test
void detachAppenderWithCloseMethod() {
Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2019 Thunderberry.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.altindag.log.service.slfj4;

import nl.altindag.log.service.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Hakan Altindag
*/
public class ServiceWithSlf4jWhileUsingKeyValuePairs implements Service {

private static final Logger LOGGER = LoggerFactory.getLogger(ServiceWithSlf4jWhileUsingKeyValuePairs.class);

@Override
public void sayHello() {
LOGGER.atInfo()
.addKeyValue("fruit", "apple")
.addKeyValue("vegetable", "tomato")
.log("My grocery list");
}

}

0 comments on commit 75116ea

Please sign in to comment.