-
Notifications
You must be signed in to change notification settings - Fork 0
Adds a connection id to an incoming request that is included in all log messages from code handling that request #97
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
8 commits
Select commit
Hold shift + click to select a range
f8c3809
Added LogContext and ServerLogFormatter, amended ServerLogging to use…
Tyreviel b8aa660
Minor change to ConnectionHandler to generate and set connection ID, …
Tyreviel 0863976
created a test, ConnectionIdLoggingTest, to verify that the connectio…
Tyreviel 2b4d97c
Updated LoggingFilterTest to check the java.util.logging.Logger inste…
Tyreviel be74d92
Updated LoggingFilterTest to check the java.util.logging.Logger inste…
Tyreviel 01ca9e9
Merge remote-tracking branch 'origin/connectionID' into connectionID
Tyreviel bf18e19
Apply Spotless formatting to resolve CI failure (#95)
Tyreviel 3e3dbc7
Include stack traces in log output as suggested by code review (#95)
Tyreviel 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
Some comments aren't visible on the classic Files Changed page.
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
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,17 @@ | ||
| package org.juv25d.logging; | ||
|
|
||
| public class LogContext { | ||
| private static final ThreadLocal<String> connectionId = new ThreadLocal<>(); | ||
|
|
||
| public static void setConnectionId(String id) { | ||
| connectionId.set(id); | ||
| } | ||
|
|
||
| public static String getConnectionId() { | ||
| return connectionId.get(); | ||
| } | ||
|
|
||
| public static void clear() { | ||
| connectionId.remove(); | ||
| } | ||
| } |
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,33 @@ | ||
| package org.juv25d.logging; | ||
|
|
||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
| import java.time.ZoneId; | ||
| import java.time.ZonedDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.logging.Formatter; | ||
| import java.util.logging.LogRecord; | ||
|
|
||
| public class ServerLogFormatter extends Formatter { | ||
| private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | ||
|
|
||
| @Override | ||
| public String format(LogRecord record) { | ||
| String connectionId = LogContext.getConnectionId(); | ||
| String idPart = (connectionId != null) ? " [" + connectionId + "]" : ""; | ||
|
|
||
| StringBuilder sb = new StringBuilder( | ||
| String.format("%s %s%s: %s%n", | ||
| ZonedDateTime.now(ZoneId.systemDefault()).format(dtf), | ||
| record.getLevel(), | ||
| idPart, | ||
| formatMessage(record))); | ||
|
|
||
| if (record.getThrown() != null) { | ||
| StringWriter sw = new StringWriter(); | ||
| record.getThrown().printStackTrace(new PrintWriter(sw)); | ||
| sb.append(sw); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
| } | ||
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
49 changes: 49 additions & 0 deletions
49
src/test/java/org/juv25d/logging/ConnectionIdLoggingTest.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,49 @@ | ||
| package org.juv25d.logging; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import java.util.logging.Handler; | ||
| import java.util.logging.LogRecord; | ||
| import java.util.logging.Logger; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class ConnectionIdLoggingTest { | ||
|
|
||
| @Test | ||
| void logMessageShouldIncludeConnectionId() { | ||
| // Arrange | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| Logger logger = Logger.getLogger("test.connectionid"); | ||
| logger.setUseParentHandlers(false); | ||
|
|
||
| List<String> formattedMessages = new ArrayList<>(); | ||
| ServerLogFormatter formatter = new ServerLogFormatter(); | ||
|
|
||
| Handler handler = new Handler() { | ||
| @Override | ||
| public void publish(LogRecord record) { | ||
| formattedMessages.add(formatter.format(record)); | ||
| } | ||
| @Override | ||
| public void flush() {} | ||
| @Override | ||
| public void close() throws SecurityException {} | ||
| }; | ||
| logger.addHandler(handler); | ||
|
|
||
| try { | ||
| String testId = "test-123"; | ||
| LogContext.setConnectionId(testId); | ||
|
|
||
| // Act | ||
| logger.info("This is a test message"); | ||
|
|
||
| // Assert | ||
| assertTrue(formattedMessages.get(0).contains("[" + testId + "]"), | ||
| "Log message should contain the connection ID. Found: " + formattedMessages.get(0)); | ||
| } finally { | ||
| LogContext.clear(); | ||
|
Tyreviel marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
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.