-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to output all keys and values in thread context (#718)
- Loading branch information
Showing
8 changed files
with
187 additions
and
66 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...rc/main/java/org/tinylog/impl/format/pattern/placeholders/AbstractContextPlaceholder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.tinylog.impl.format.pattern.placeholders; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Set; | ||
|
||
import org.tinylog.impl.LogEntryValue; | ||
import org.tinylog.impl.format.pattern.ValueType; | ||
|
||
/** | ||
* Placeholder implementation for resolving thread context values for a log entry. | ||
*/ | ||
public abstract class AbstractContextPlaceholder implements Placeholder { | ||
|
||
@Override | ||
public Set<LogEntryValue> getRequiredLogEntryValues() { | ||
return EnumSet.of(LogEntryValue.CONTEXT); | ||
} | ||
|
||
@Override | ||
public ValueType getType() { | ||
return ValueType.STRING; | ||
} | ||
} |
45 changes: 0 additions & 45 deletions
45
...g-impl/src/main/java/org/tinylog/impl/format/pattern/placeholders/ContextPlaceholder.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
.../main/java/org/tinylog/impl/format/pattern/placeholders/MultiValueContextPlaceholder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.tinylog.impl.format.pattern.placeholders; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.tinylog.impl.LogEntry; | ||
|
||
/** | ||
* Placeholder implementation for resolving all thread context keys and values for a log entry. | ||
*/ | ||
public class MultiValueContextPlaceholder extends AbstractContextPlaceholder { | ||
|
||
/** | ||
* Constructs a MultiValueContextPlaceholder. | ||
*/ | ||
public MultiValueContextPlaceholder() { | ||
} | ||
|
||
@Override | ||
public String getValue(LogEntry entry) { | ||
return contextKeysAndValues(entry); | ||
} | ||
|
||
@Override | ||
public void render(StringBuilder builder, LogEntry entry) { | ||
builder.append(contextKeysAndValues(entry)); | ||
} | ||
|
||
/** | ||
* Creates a string representation of all keys and values in thread context. | ||
* | ||
* @param logEntry The log entry whose thread context is to be represented as a string | ||
* @return A string representation of all keys and values in entry's thread context | ||
*/ | ||
private static String contextKeysAndValues(LogEntry logEntry) { | ||
Set<Map.Entry<String, String>> contextEntries = logEntry.getContext().entrySet(); | ||
if (contextEntries.isEmpty()) { | ||
return ""; | ||
} | ||
|
||
return contextEntries.stream() | ||
.sorted(Map.Entry.comparingByKey()) | ||
.map(mapEntry -> new StringBuilder(mapEntry.getKey()).append("=").append(mapEntry.getValue())) | ||
.collect(Collectors.joining(", ")); | ||
} | ||
|
||
} | ||
|
29 changes: 29 additions & 0 deletions
29
...main/java/org/tinylog/impl/format/pattern/placeholders/SingleValueContextPlaceholder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.tinylog.impl.format.pattern.placeholders; | ||
|
||
import org.tinylog.impl.LogEntry; | ||
|
||
/** | ||
* Placeholder implementation for resolving one thread context value for a log entry. | ||
*/ | ||
public class SingleValueContextPlaceholder extends AbstractContextPlaceholder { | ||
|
||
private final String key; | ||
|
||
/** | ||
* @param key The key of the thread context value to output | ||
*/ | ||
public SingleValueContextPlaceholder(String key) { | ||
this.key = key; | ||
} | ||
|
||
@Override | ||
public String getValue(LogEntry entry) { | ||
return entry.getContext().get(key); | ||
} | ||
|
||
@Override | ||
public void render(StringBuilder builder, LogEntry entry) { | ||
String value = entry.getContext().getOrDefault(key, ""); | ||
builder.append(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...t/java/org/tinylog/impl/format/pattern/placeholders/MultiValueContextPlaceholderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.tinylog.impl.format.pattern.placeholders; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.tinylog.impl.LogEntry; | ||
import org.tinylog.impl.LogEntryValue; | ||
import org.tinylog.impl.format.pattern.ValueType; | ||
import org.tinylog.impl.test.FormatOutputRenderer; | ||
import org.tinylog.impl.test.LogEntryBuilder; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for MultiValueContextPlaceholder. | ||
*/ | ||
public class MultiValueContextPlaceholderTest { | ||
|
||
/** | ||
* Verifies that the log entry value {@link LogEntryValue#CONTEXT} is defined as required by the context | ||
* placeholder. | ||
*/ | ||
@Test | ||
void requiredLogEntryValues() { | ||
MultiValueContextPlaceholder placeholder = new MultiValueContextPlaceholder(); | ||
assertThat(placeholder.getRequiredLogEntryValues()).containsExactly(LogEntryValue.CONTEXT); | ||
} | ||
|
||
/** | ||
* Verifies that all thread context keys and values of a log entry will be resolved. | ||
*/ | ||
@Test | ||
void resolveWithContextValues() { | ||
MultiValueContextPlaceholder placeholder = new MultiValueContextPlaceholder(); | ||
LogEntry logEntry = new LogEntryBuilder().context("foo", "bar").context("baz", "quk").create(); | ||
assertThat(placeholder.getType()).isEqualTo(ValueType.STRING); | ||
assertThat(placeholder.getValue(logEntry)).isEqualTo("baz=quk, foo=bar"); | ||
} | ||
|
||
/** | ||
* Verifies that empty string will be resolved, if a thread context value is not present. | ||
*/ | ||
@Test | ||
void resolveWithoutContextValues() { | ||
MultiValueContextPlaceholder placeholder = new MultiValueContextPlaceholder(); | ||
LogEntry logEntry = new LogEntryBuilder().create(); | ||
assertThat(placeholder.getType()).isEqualTo(ValueType.STRING); | ||
assertThat(placeholder.getValue(logEntry)).isEqualTo(""); | ||
} | ||
|
||
/** | ||
* Verifies that all thread context keys and values of a log entry will be output. | ||
*/ | ||
@Test | ||
void renderWithContextValues() { | ||
MultiValueContextPlaceholder placeholder = new MultiValueContextPlaceholder(); | ||
FormatOutputRenderer renderer = new FormatOutputRenderer(placeholder); | ||
LogEntry logEntry = new LogEntryBuilder().context("foo", "bar").context("baz", "quk").create(); | ||
assertThat(renderer.render(logEntry)).isEqualTo("baz=quk, foo=bar"); | ||
} | ||
|
||
/** | ||
* Verifies that an empty string will be output, if no thread context values are | ||
* present. | ||
*/ | ||
@Test | ||
void renderWithoutContextValues() { | ||
MultiValueContextPlaceholder placeholder = new MultiValueContextPlaceholder(); | ||
FormatOutputRenderer renderer = new FormatOutputRenderer(placeholder); | ||
LogEntry logEntry = new LogEntryBuilder().create(); | ||
assertThat(renderer.render(logEntry)).isEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters