-
Notifications
You must be signed in to change notification settings - Fork 50
Introduce SPI Loading for Log4j ContextFieldSuppliers #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
773728f
Support SPI Loading of ContextFieldSuppliers for Log4j2
KarstenSchnitter 7474318
Refactor Logback ContextFieldSupplier Loading
KarstenSchnitter 5b1a957
Delete cf-java-logging-support-opentelemetry-agent-extension/dependen…
KarstenSchnitter 3a9f016
Add comment on order constants for Field Suppliers
KarstenSchnitter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
...n/java/com/sap/hcp/cf/logging/common/serialization/ContextFieldSupplierServiceLoader.java
This file contains hidden or 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,18 @@ | ||
| package com.sap.hcp.cf.logging.common.serialization; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.ServiceLoader; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class ContextFieldSupplierServiceLoader<T> { | ||
|
|
||
| private ContextFieldSupplierServiceLoader() { | ||
| } | ||
|
|
||
| public static <T extends ContextFieldSupplier> ArrayList<T> addFieldSuppliers(Stream<T> original, Class<T> clazz) { | ||
| Stream<T> spiSuppliers = ServiceLoader.load(clazz).stream().map(ServiceLoader.Provider::get).sorted(); | ||
| return Stream.concat(original, spiSuppliers).sorted().collect(Collectors.toCollection(ArrayList::new)); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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
This file contains hidden or 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
5 changes: 5 additions & 0 deletions
5
...va-logging-support-log4j2/src/main/java/com/sap/hcp/cf/log4j2/layout/ElementSupplier.java
This file contains hidden or 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,5 @@ | ||
| package com.sap.hcp.cf.log4j2.layout; | ||
|
|
||
| public interface ElementSupplier { | ||
| String getSupplierClass(); | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
3 changes: 3 additions & 0 deletions
3
...resources/META-INF/services/com.sap.hcp.cf.log4j2.converter.api.Log4jContextFieldSupplier
This file contains hidden or 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,3 @@ | ||
| com.sap.hcp.cf.log4j2.layout.supppliers.BaseFieldSupplier | ||
| com.sap.hcp.cf.log4j2.layout.supppliers.EventContextFieldSupplier | ||
| com.sap.hcp.cf.log4j2.layout.supppliers.RequestRecordFieldSupplier |
80 changes: 80 additions & 0 deletions
80
...ging-support-log4j2/src/test/java/com/sap/hcp/cf/log4j2/layout/JsonPatternLayoutTest.java
This file contains hidden or 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,80 @@ | ||
| package com.sap.hcp.cf.log4j2.layout; | ||
|
|
||
| import com.sap.hcp.cf.log4j2.converter.api.Log4jContextFieldSupplier; | ||
| import com.sap.hcp.cf.log4j2.layout.supppliers.BaseFieldSupplier; | ||
| import com.sap.hcp.cf.log4j2.layout.supppliers.EventContextFieldSupplier; | ||
| import com.sap.hcp.cf.log4j2.layout.supppliers.RequestRecordFieldSupplier; | ||
| import com.sap.hcp.cf.logging.common.serialization.ContextFieldSupplier; | ||
| import org.apache.logging.log4j.core.LogEvent; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class JsonPatternLayoutTest { | ||
|
|
||
| private final JsonPatternLayout LAYOUT = | ||
| JsonPatternLayout.createLayout(StandardCharsets.UTF_8, false, 10, null, new CustomFieldElement[0], | ||
| new Log4jContextFieldSupplierElement[] { | ||
| Log4jContextFieldSupplierElement.newBuilder().setClazz( | ||
| SampleLog4jContextFieldSupplier.class.getName()).build() }, | ||
| new ContextFieldSupplierElement[] { ContextFieldSupplierElement.newBuilder() | ||
| .setClazz( | ||
| SampleContextFieldSupplier.class.getName()).build() }, | ||
| null); | ||
|
|
||
| @Test | ||
| void loadsContextFieldSuppliers() { | ||
| assertThat(LAYOUT.getContextFieldSuppliers()).map(Object::getClass).map(Class::getName) | ||
| .containsExactly(SampleContextFieldSupplier.class.getName(), | ||
| SpiContextFieldSupplier.class.getName()); | ||
| } | ||
|
|
||
| @Test | ||
| void getLog4jContextFieldSuppliers() { | ||
| assertThat(LAYOUT.getLog4jContextFieldSuppliers()).map(Object::getClass).map(Class::getName) | ||
| .containsExactly(BaseFieldSupplier.class.getName(), | ||
| EventContextFieldSupplier.class.getName(), | ||
| RequestRecordFieldSupplier.class.getName(), | ||
| SampleLog4jContextFieldSupplier.class.getName(), | ||
| SpiLog4jContextFieldSupplier.class.getName()); | ||
| } | ||
|
|
||
| static class SampleContextFieldSupplier implements ContextFieldSupplier { | ||
| @Override | ||
| public Map<String, Object> get() { | ||
| return Collections.emptyMap(); | ||
| } | ||
| } | ||
|
|
||
| public static class SpiContextFieldSupplier implements ContextFieldSupplier { | ||
|
|
||
| public SpiContextFieldSupplier() { | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Object> get() { | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| static class SampleLog4jContextFieldSupplier implements Log4jContextFieldSupplier { | ||
|
|
||
| @Override | ||
| public Map<String, Object> map(LogEvent event) { | ||
| return Collections.emptyMap(); | ||
| } | ||
| } | ||
|
|
||
| public static class SpiLog4jContextFieldSupplier implements Log4jContextFieldSupplier { | ||
|
|
||
| @Override | ||
| public Map<String, Object> map(LogEvent event) { | ||
| return Collections.emptyMap(); | ||
| } | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
...resources/META-INF/services/com.sap.hcp.cf.log4j2.converter.api.Log4jContextFieldSupplier
This file contains hidden or 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 @@ | ||
| com.sap.hcp.cf.log4j2.layout.JsonPatternLayoutTest$SpiLog4jContextFieldSupplier |
1 change: 1 addition & 0 deletions
1
...ources/META-INF/services/com.sap.hcp.cf.logging.common.serialization.ContextFieldSupplier
This file contains hidden or 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 @@ | ||
| com.sap.hcp.cf.log4j2.layout.JsonPatternLayoutTest$SpiContextFieldSupplier |
This file contains hidden or 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
31 changes: 0 additions & 31 deletions
31
...back/src/main/java/com/sap/hcp/cf/logback/encoder/ContextFieldSuppliersServiceLoader.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.