-
Notifications
You must be signed in to change notification settings - Fork 3
Add logging through slf4j (#6) #7
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
Open
xbt-a4224j
wants to merge
5
commits into
Premo-Cloud:main
Choose a base branch
from
xbt-a4224j:feat/logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
99f40cd
Log requests through slf4j, with credential headers masked
xbt-a4224j 9a3ce73
Keep the logging test's events out of the console
xbt-a4224j 28a5906
Log the request summary at DEBUG and the wire at TRACE
xbt-a4224j 5967278
Log directly through the slf4j logger; add request id to the summary …
xbt-a4224j a51f603
Drop the dependency list from the README; the build file is the record
xbt-a4224j 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
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
74 changes: 74 additions & 0 deletions
74
typesafe-sdk/src/main/java/io/github/premocloud/typesafe/Logging.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,74 @@ | ||
| package io.github.premocloud.typesafe; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.net.http.HttpHeaders; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * What the client logs, tiered as the official SDKs log it. | ||
| * | ||
| * <p>{@code DEBUG} is one line per request with its status, elapsed time and request id, plus a line | ||
| * for each retry and each connection failure. {@code TRACE} adds the wire in both directions: method, | ||
| * url, headers and body. Nothing is logged at {@code INFO} or above, because a failure is already | ||
| * thrown and the exception carries more than a log line would. | ||
| * | ||
| * <p>Set the level on the {@code io.github.premocloud.typesafe} logger, as for any library. There is | ||
| * no environment variable: slf4j has no library-side level setting, and a library should let the | ||
| * application configure its own logging. | ||
| * | ||
| * <p>Credential headers are masked. Bodies are not, so {@code TRACE} puts the state being classified | ||
| * into the log; both official SDKs document the same. | ||
| */ | ||
| final class Logging { | ||
|
|
||
| static final Logger LOG = LoggerFactory.getLogger("io.github.premocloud.typesafe"); | ||
|
|
||
| /** Masked in full. The union of what the official Python and JavaScript SDKs cover. */ | ||
| private static final Set<String> SECRET_HEADERS = Set.of( | ||
| "authorization", "proxy-authorization", "api-key", "x-api-key", "cookie", "set-cookie"); | ||
|
|
||
| private Logging() { | ||
| } | ||
|
|
||
| /** Guards the wire calls, so a redacted header string is never built when TRACE is off. */ | ||
| static boolean wireEnabled() { | ||
| return LOG.isTraceEnabled(); | ||
| } | ||
|
|
||
| /** | ||
| * One direction of the exchange. | ||
| * | ||
| * @param arrow {@code ->} for what was sent, {@code <-} for what came back | ||
| */ | ||
| static void wire(String tag, String arrow, String summary, HttpHeaders headers, @Nullable String body) { | ||
| LOG.trace("{} {} {} headers={} body={}", tag, arrow, summary, redact(headers), body == null ? "" : body); | ||
| } | ||
|
|
||
| /** | ||
| * Header names and values with credentials masked. The only place redaction happens, so no call | ||
| * site can leak one. | ||
| */ | ||
| private static String redact(HttpHeaders headers) { | ||
| return headers.map().entrySet().stream() | ||
| .map(header -> header.getKey() + "=" + value(header.getKey(), header.getValue())) | ||
| .collect(Collectors.joining(", ", "{", "}")); | ||
| } | ||
|
|
||
| private static String value(String name, List<String> values) { | ||
| String lower = name.toLowerCase(Locale.ROOT); | ||
|
|
||
| // The named set covers what is documented; the substring check catches a header this SDK | ||
| // never anticipated, which a caller can add through Builder.header. | ||
| if (SECRET_HEADERS.contains(lower) || lower.contains("token") || lower.contains("secret")) { | ||
| return "***"; | ||
| } | ||
|
|
||
| return values.isEmpty() ? "" : values.get(0); | ||
| } | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@garretpremo Should we keep or remove the REQUESTS counter? It's in this PR to help distinguish logs of interleaved async calls, but maybe not worth.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xbt-a4224j Adding an identifier to requests is super useful! Especially after adding async requests which could add interleaved logs that are indistinguishable from each other.
Suggested change: Generate the identifier as a short random hex id in the logging class rather than a JVM-global counter. This avoids collisions across restarts and replicas at a small cost to readability (which is worth it IMO for a debug line).
Calllines: