-
Notifications
You must be signed in to change notification settings - Fork 3
Fix #453: Add logging-support module with StructuredArgumentsConverter #454
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
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
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| # Changelog | ||
|
|
||
| ## 2.2.0 | ||
| - [#453](https://github.com/wultra/java-core/issues/453) - Added `logging-support` module with `StructuredArgumentsConverter` for rendering `kv()` structured arguments in plain-text log patterns. | ||
| - [#451](https://github.com/wultra/java-core/issues/451) - Migrated to Spring Boot 4 and Jackson 3. | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>com.wultra.core</groupId> | ||
| <artifactId>wultra-java-core-parent</artifactId> | ||
| <version>2.2.0-SNAPSHOT</version> | ||
| </parent> | ||
|
|
||
| <artifactId>logging-support</artifactId> | ||
| <name>wultra-core-logging-support</name> | ||
| <description>Logging support utilities for Wultra services</description> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>ch.qos.logback</groupId> | ||
| <artifactId>logback-classic</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>net.logstash.logback</groupId> | ||
| <artifactId>logstash-logback-encoder</artifactId> | ||
| <version>${logstash.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-test</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
|
vita-kotacka marked this conversation as resolved.
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> | ||
64 changes: 64 additions & 0 deletions
64
...g-support/src/main/java/com/wultra/core/logging/logback/StructuredArgumentsConverter.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,64 @@ | ||
| /* | ||
| * Copyright 2026 Wultra s.r.o. | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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 com.wultra.core.logging.logback; | ||
|
vita-kotacka marked this conversation as resolved.
|
||
|
|
||
| import ch.qos.logback.classic.pattern.ClassicConverter; | ||
| import ch.qos.logback.classic.spi.ILoggingEvent; | ||
| import net.logstash.logback.argument.StructuredArgument; | ||
|
|
||
| /** | ||
| * Logback {@link ClassicConverter} that renders {@link StructuredArgument} instances | ||
| * (e.g. from {@code StructuredArguments.kv()}) in plain-text log output. | ||
| * | ||
| * <p>Usage in {@code logback.xml}: | ||
| * <pre>{@code | ||
| * <conversionRule conversionWord="sa" | ||
| * converterClass="com.wultra.core.logging.logback.StructuredArgumentsConverter"/> | ||
| * <!-- Pattern example (no space before %sa — the converter prepends its own space) --> | ||
| * <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%sa%n</pattern> | ||
| * }</pre> | ||
| * | ||
| * <p>Output example: | ||
| * <pre> | ||
| * 10:35:22.123 [main] INFO c.w.EnrollmentService - action: createIdentity, state: succeeded {processId=abc-123, identityVerificationId=def-456} | ||
| * </pre> | ||
| * | ||
| * <p>When no structured arguments are present, an empty string is returned so no trailing | ||
| * whitespace appears in the log line. | ||
| * | ||
| * @author Vit Kotacka, vit.kotacka@wultra.com | ||
| */ | ||
|
vita-kotacka marked this conversation as resolved.
|
||
| public class StructuredArgumentsConverter extends ClassicConverter { | ||
|
|
||
| @Override | ||
| public String convert(final ILoggingEvent event) { | ||
| final Object[] args = event.getArgumentArray(); | ||
| if (args == null || args.length == 0) { | ||
| return ""; | ||
| } | ||
| final StringBuilder sb = new StringBuilder(); | ||
| for (final Object arg : args) { | ||
| if (arg instanceof StructuredArgument) { | ||
| if (sb.length() > 0) { | ||
| sb.append(", "); | ||
| } | ||
| sb.append(arg); | ||
| } | ||
| } | ||
| return sb.length() == 0 ? "" : " {" + sb + "}"; | ||
| } | ||
|
|
||
| } | ||
70 changes: 70 additions & 0 deletions
70
...pport/src/test/java/com/wultra/core/logging/logback/StructuredArgumentsConverterTest.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,70 @@ | ||
| /* | ||
| * Copyright 2026 Wultra s.r.o. | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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 com.wultra.core.logging.logback; | ||
|
|
||
| import ch.qos.logback.classic.Level; | ||
| import ch.qos.logback.classic.Logger; | ||
| import ch.qos.logback.classic.LoggerContext; | ||
| import ch.qos.logback.classic.spi.LoggingEvent; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static net.logstash.logback.argument.StructuredArguments.kv; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * Tests for {@link StructuredArgumentsConverter}. | ||
| * | ||
| * @author Vit Kotacka, vit.kotacka@wultra.com | ||
| */ | ||
| class StructuredArgumentsConverterTest { | ||
|
vita-kotacka marked this conversation as resolved.
|
||
|
|
||
| private final StructuredArgumentsConverter converter = new StructuredArgumentsConverter(); | ||
|
|
||
| @Test | ||
| void convert_withStructuredArguments_rendersKeyValuePairs() { | ||
| final LoggingEvent event = buildEvent("action: test, state: succeeded", | ||
| kv("processId", "abc-123"), kv("userId", "user-42")); | ||
|
|
||
| assertEquals(" {processId=abc-123, userId=user-42}", converter.convert(event)); | ||
| } | ||
|
|
||
| @Test | ||
| void convert_withNoArguments_returnsEmpty() { | ||
| final LoggingEvent event = buildEvent("plain message"); | ||
| assertEquals("", converter.convert(event)); | ||
| } | ||
|
|
||
| @Test | ||
| void convert_withNullArgumentArray_returnsEmpty() { | ||
| final LoggingEvent event = buildEvent("plain message", (Object[]) null); | ||
| assertEquals("", converter.convert(event)); | ||
| } | ||
|
|
||
| @Test | ||
| void convert_withMixedArguments_rendersOnlyStructuredOnes() { | ||
| final LoggingEvent event = buildEvent("msg {}", "plainValue", kv("id", "xyz")); | ||
| assertEquals(" {id=xyz}", converter.convert(event)); | ||
| } | ||
|
|
||
| private static LoggingEvent buildEvent(final String message, final Object... args) { | ||
| final LoggerContext ctx = new LoggerContext(); | ||
| final Logger logger = ctx.getLogger(StructuredArgumentsConverterTest.class); | ||
| return new LoggingEvent( | ||
| StructuredArgumentsConverterTest.class.getName(), | ||
| logger, Level.INFO, message, null, args); | ||
| } | ||
|
|
||
| } | ||
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
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.