-
Notifications
You must be signed in to change notification settings - Fork 35
Add java.util.logging backend #741
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
mihaimitrea-db
wants to merge
1
commit into
main
Choose a base branch
from
mihaimitrea-db/stack/logging-jul
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
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
208 changes: 208 additions & 0 deletions
208
databricks-sdk-java/src/main/java/com/databricks/sdk/core/logging/JulLogger.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,208 @@ | ||
| package com.databricks.sdk.core.logging; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.function.Supplier; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.LogRecord; | ||
|
|
||
| /** Delegates logging calls to a {@code java.util.logging.Logger}, translating SLF4J conventions. */ | ||
| class JulLogger extends Logger { | ||
|
|
||
| private static final String LOGGING_PACKAGE = "com.databricks.sdk.core.logging."; | ||
|
|
||
| private final java.util.logging.Logger delegate; | ||
|
|
||
| JulLogger(java.util.logging.Logger delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public void debug(String msg) { | ||
| log(Level.FINE, msg, null); | ||
| } | ||
|
|
||
| @Override | ||
| public void debug(String format, Object... args) { | ||
| log(Level.FINE, format, args); | ||
| } | ||
|
|
||
| @Override | ||
| public void debug(Supplier<String> msgSupplier) { | ||
| if (delegate.isLoggable(Level.FINE)) { | ||
| log(Level.FINE, msgSupplier.get(), null); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void info(String msg) { | ||
| log(Level.INFO, msg, null); | ||
| } | ||
|
|
||
| @Override | ||
| public void info(String format, Object... args) { | ||
| log(Level.INFO, format, args); | ||
| } | ||
|
|
||
| @Override | ||
| public void info(Supplier<String> msgSupplier) { | ||
| if (delegate.isLoggable(Level.INFO)) { | ||
| log(Level.INFO, msgSupplier.get(), null); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void warn(String msg) { | ||
| log(Level.WARNING, msg, null); | ||
| } | ||
|
|
||
| @Override | ||
| public void warn(String format, Object... args) { | ||
| log(Level.WARNING, format, args); | ||
| } | ||
|
|
||
| @Override | ||
| public void warn(Supplier<String> msgSupplier) { | ||
| if (delegate.isLoggable(Level.WARNING)) { | ||
| log(Level.WARNING, msgSupplier.get(), null); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void error(String msg) { | ||
| log(Level.SEVERE, msg, null); | ||
| } | ||
|
|
||
| @Override | ||
| public void error(String format, Object... args) { | ||
| log(Level.SEVERE, format, args); | ||
| } | ||
|
|
||
| @Override | ||
| public void error(Supplier<String> msgSupplier) { | ||
| if (delegate.isLoggable(Level.SEVERE)) { | ||
| log(Level.SEVERE, msgSupplier.get(), null); | ||
| } | ||
| } | ||
|
|
||
| private void log(Level level, String format, Object[] args) { | ||
| if (!delegate.isLoggable(level)) { | ||
| return; | ||
| } | ||
| Throwable thrown = (args != null) ? extractThrowable(format, args) : null; | ||
| String message = (args != null) ? formatMessage(format, args) : format; | ||
| LogRecord record = new LogRecord(level, message); | ||
| record.setLoggerName(delegate.getName()); | ||
| if (thrown != null) { | ||
| record.setThrown(thrown); | ||
| } | ||
| inferCaller(record); | ||
| delegate.log(record); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the source class and method on a {@link LogRecord} by walking the call stack to find the | ||
| * first frame outside this logging package. | ||
| * | ||
| * <p>JUL normally infers caller information automatically by scanning the stack for the first | ||
| * frame after its own {@code java.util.logging.Logger} methods. Because {@code JulLogger} wraps | ||
| * the JUL logger, that automatic inference stops at {@code JulLogger} or its helper methods | ||
| * instead of reaching the actual SDK class that initiated the log call. Without this correction, | ||
| * every log record would be attributed to {@code JulLogger}, making JUL output useless for | ||
| * identifying the real call site. | ||
| */ | ||
| private static void inferCaller(LogRecord record) { | ||
| StackTraceElement[] stack = new Throwable().getStackTrace(); | ||
| for (StackTraceElement frame : stack) { | ||
| if (!frame.getClassName().startsWith(LOGGING_PACKAGE)) { | ||
| record.setSourceClassName(frame.getClassName()); | ||
| record.setSourceMethodName(frame.getMethodName()); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Replaces SLF4J-style {@code {}} placeholders with argument values, matching the semantics of | ||
| * SLF4J's {@code MessageFormatter.arrayFormat}: | ||
| * | ||
| * <ul> | ||
| * <li>A trailing {@link Throwable} is unconditionally excluded from formatting. | ||
| * <li>A backslash before {@code {}} escapes it as a literal {@code {}}. | ||
| * <li>Array arguments are rendered with {@link Arrays#deepToString}. | ||
| * <li>A {@code null} format string returns {@code null}. | ||
| * </ul> | ||
| */ | ||
| static String formatMessage(String format, Object[] args) { | ||
| if (format == null) { | ||
| return null; | ||
| } | ||
| if (args == null || args.length == 0) { | ||
| return format; | ||
| } | ||
| int usableArgs = args.length; | ||
| if (args[usableArgs - 1] instanceof Throwable) { | ||
| usableArgs--; | ||
| } | ||
| StringBuilder sb = new StringBuilder(format.length() + 32); | ||
| int argIdx = 0; | ||
| int i = 0; | ||
| while (i < format.length()) { | ||
| if (i + 1 < format.length() && format.charAt(i) == '{' && format.charAt(i + 1) == '}') { | ||
| if (i > 0 && format.charAt(i - 1) == '\\') { | ||
| sb.setLength(sb.length() - 1); | ||
| sb.append("{}"); | ||
| } else if (argIdx < usableArgs) { | ||
| sb.append(renderArg(args[argIdx++])); | ||
| } else { | ||
| sb.append("{}"); | ||
| } | ||
| i += 2; | ||
| } else { | ||
| sb.append(format.charAt(i)); | ||
| i++; | ||
| } | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| private static String renderArg(Object arg) { | ||
| if (arg == null) { | ||
| return "null"; | ||
| } | ||
| if (arg instanceof Object[]) { | ||
| return Arrays.deepToString((Object[]) arg); | ||
| } | ||
| if (arg.getClass().isArray()) { | ||
| return primitiveArrayToString(arg); | ||
| } | ||
| return arg.toString(); | ||
| } | ||
|
|
||
| private static String primitiveArrayToString(Object array) { | ||
| if (array instanceof boolean[]) return Arrays.toString((boolean[]) array); | ||
| if (array instanceof byte[]) return Arrays.toString((byte[]) array); | ||
| if (array instanceof char[]) return Arrays.toString((char[]) array); | ||
| if (array instanceof short[]) return Arrays.toString((short[]) array); | ||
| if (array instanceof int[]) return Arrays.toString((int[]) array); | ||
| if (array instanceof long[]) return Arrays.toString((long[]) array); | ||
| if (array instanceof float[]) return Arrays.toString((float[]) array); | ||
| if (array instanceof double[]) return Arrays.toString((double[]) array); | ||
| return Arrays.deepToString(new Object[] {array}); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the last argument if it is a {@link Throwable}, unconditionally. This matches SLF4J's | ||
| * {@code NormalizedParameters.getThrowableCandidate}, which always extracts a trailing Throwable | ||
| * regardless of how many {@code {}} placeholders the format string contains. | ||
| */ | ||
| static Throwable extractThrowable(String format, Object[] args) { | ||
| if (args == null || args.length == 0) { | ||
| return null; | ||
| } | ||
| Object last = args[args.length - 1]; | ||
| if (last instanceof Throwable) { | ||
| return (Throwable) last; | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
25 changes: 25 additions & 0 deletions
25
databricks-sdk-java/src/main/java/com/databricks/sdk/core/logging/JulLoggerFactory.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,25 @@ | ||
| package com.databricks.sdk.core.logging; | ||
|
|
||
| /** | ||
| * An {@link ILoggerFactory} backed by {@code java.util.logging}. Always available on any JRE. | ||
| * | ||
| * <p>Use this when SLF4J is not desirable: | ||
| * | ||
| * <pre>{@code | ||
| * LoggerFactory.setDefault(JulLoggerFactory.INSTANCE); | ||
| * }</pre> | ||
| */ | ||
| public class JulLoggerFactory implements ILoggerFactory { | ||
|
|
||
| public static final JulLoggerFactory INSTANCE = new JulLoggerFactory(); | ||
|
|
||
| @Override | ||
| public Logger getLogger(Class<?> type) { | ||
| return new JulLogger(java.util.logging.Logger.getLogger(type.getName())); | ||
| } | ||
|
|
||
| @Override | ||
| public Logger getLogger(String name) { | ||
| return new JulLogger(java.util.logging.Logger.getLogger(name)); | ||
| } | ||
| } |
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.
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.
Could this drop the escape incorrectly for
\\{}?For format
a\\{}bwith arg"x", SLF4J producesa\xb; the first\escapes the second, and{}substitutes. Though, this code producesa\{}b; the\before{}is treated as an escape and{}stays literal.The one-character lookback can't distinguish
\{}from\\{}. SLF4J counts the run of backslashes before{}and uses the odd/even count to decide.I don't think this is a blocker as log calls are controlled by the SDK. Let's just make sure this is documented for posterity.