Add logging abstraction with SLF4J backend#740
Conversation
9ac10fc to
99b473a
Compare
Range-diff: main (9ac10fc -> 99b473a)
Reproduce locally: |
| */ | ||
| public abstract class Logger { | ||
|
|
||
| public abstract boolean isDebugEnabled(); |
There was a problem hiding this comment.
How and why is this function used?
There was a problem hiding this comment.
As of right now this function is only used in ApiClient.java:L256. It's used to guard against a call to LOG.debug where constructing the message is relatively expensive and runs at every request:
response = httpClient.execute(in);
if (LOG.isDebugEnabled()) {
LOG.debug(makeLogRecord(in, response));
}
Users can customize their logger's level of detail in the runtime config. If they are not interested in debug-level details it makes sense to not slow down every request with constructing the log. This behaviour is not new to this PR and I think it makes sense to keep it.
This check could be made as a guard in the debug function itself to abstract over this, but both JUL (.isLoggable(Level.FINE)) and SLF4J (.isDebugEnabled()) implement some sort of way of checking if you want to log for debugs.
Would you prefer something else?
|
|
||
| /** Returns a logger for the given class, using the current default factory. */ | ||
| public static Logger getLogger(Class<?> type) { | ||
| return getDefault().newInstance(type); |
There was a problem hiding this comment.
Does newInstance always create a new logger? What will happen when a logger exists for a given class?
There was a problem hiding this comment.
With the wrapper we are introducing you will get a new instance of the desired logger wrapper. However behind the scenes for SLF4J and JUL keep track of existing loggers and see if you have already instantiated the requested logger before. If you have the .getLogger() function will provide you with the same instance.
As per the SLF4J FAQ:
More specifically, each time a logger is retrieved by invoking LoggerFactory.getLogger() method, the underlying logging system will return an instance appropriate for the current application. Please note that within the same application retrieving a logger by a given name will always return the same logger. For a given name, a different logger will be returned only for different applications.
The only difference in our case is that we are creating a new wrapper instance. In practice this should NOT happen because every class except for CallbackResponseHandler and NonIdempotentRequestRetryStrategy use the static keyword when initializing their logger. These two classes follow a different method of initializing their loggers, namely: .getLogger(getClass().getName()).
databricks-sdk-java/src/main/java/com/databricks/sdk/core/logging/Slf4jLoggerFactory.java
Outdated
Show resolved
Hide resolved
databricks-sdk-java/src/test/java/com/databricks/sdk/core/logging/Slf4jLoggerTest.java
Outdated
Show resolved
Hide resolved
99b473a to
d550d5e
Compare
Range-diff: main (99b473a -> d550d5e)
Reproduce locally: |
d550d5e to
9597522
Compare
Range-diff: main (d550d5e -> 9597522)
Reproduce locally: |
|
If integration tests don't run automatically, an authorized user can run them manually by following the instructions below: Trigger: Inputs:
Checks will be approved automatically on success. |
🥞 Stacked PR
Use this link to review incremental changes.
Summary
Introduces a logging abstraction layer (
com.databricks.sdk.core.logging) that decouples the SDK's internal logging from any specific backend. SLF4J remains the default. This PR contains only the abstraction and the SLF4J backend; the JUL backend and the call-site migration follow in stacked PRs.Why
The SDK currently imports
org.slf4j.Loggerandorg.slf4j.LoggerFactorydirectly in every class that logs. This hard coupling means users who embed the SDK in environments where SLF4J is impractical (e.g. BI tools with constrained classpaths) have no way to switch to an alternative logging backend likejava.util.logging.We need an indirection layer that lets users swap the logging backend programmatically while keeping SLF4J as the zero-configuration default so that existing users don't have to change anything.
The design follows the pattern established by Netty's
InternalLoggerFactory: an abstractLoggerFactoryclass with concrete subclasses for each backend, asetDefaultmethod to override the backend, and a separateLoggerabstract class that serves as a clean extension point for custom implementations.What changed
Interface changes
Logger— new abstract class incom.databricks.sdk.core.logging. Defines the logging contract:debug,info,warn,error(each with plain-string and varargs overloads), andisDebugEnabled. Users extend this to build custom loggers.LoggerFactory— new abstract class. Static methodsgetLogger(Class<?>)andgetLogger(String)return loggers from the current default factory.setDefault(LoggerFactory)overrides the backend — must be called before creating any SDK client.Slf4jLoggerFactory— public concrete factory with a singletonINSTANCE. This is the default.Behavioral changes
None. SLF4J is the default backend and all logging calls pass through to
org.slf4j.Loggerexactly as before. Existing users see no difference.Internal changes
Slf4jLogger— package-private class that delegates all calls to anorg.slf4j.Logger. Fully qualifiesorg.slf4j.LoggerFactoryreferences to avoid collision with the SDK'sLoggerFactory.com.databricks.sdk.core.logging.How is this tested?
LoggerFactoryTest— verifies the default factory is SLF4J, thatsetDefault(null)is rejected, and thatgetLogger(String)works.Slf4jLoggerTest— verifiesSlf4jLogger.createreturns the correct type, and exercises all logging methods including varargs and trailing Throwable.