Skip to content

Commit e9ea3da

Browse files
Extract ILoggerFactory interface from LoggerFactory
LoggerFactory was an abstract class holding a static reference to itself. Replace with the SLF4J-style pattern: ILoggerFactory is the interface that backends implement, and LoggerFactory is a final utility class that delegates to the current ILoggerFactory.
1 parent 31ca46e commit e9ea3da

5 files changed

Lines changed: 35 additions & 33 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.databricks.sdk.core.logging;
2+
3+
/**
4+
* Provides {@link Logger} instances for a specific logging backend.
5+
*
6+
* <p>Implement this interface to provide a custom logging backend, then register it via {@link
7+
* LoggerFactory#setDefault(ILoggerFactory)}.
8+
*/
9+
public interface ILoggerFactory {
10+
11+
/** Returns a logger for the given class. */
12+
Logger getLogger(Class<?> type);
13+
14+
/** Returns a logger with the given name. */
15+
Logger getLogger(String name);
16+
}

databricks-sdk-java/src/main/java/com/databricks/sdk/core/logging/Logger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Logging contract used throughout the SDK.
77
*
88
* <p>Extend this class to provide a custom logging implementation, then register it via a custom
9-
* {@link LoggerFactory} subclass and {@link LoggerFactory#setDefault}.
9+
* {@link ILoggerFactory} and {@link LoggerFactory#setDefault}.
1010
*/
1111
public abstract class Logger {
1212

databricks-sdk-java/src/main/java/com/databricks/sdk/core/logging/LoggerFactory.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.concurrent.atomic.AtomicReference;
44

55
/**
6-
* Creates and configures {@link Logger} instances for the SDK.
6+
* Static entry point for obtaining {@link Logger} instances.
77
*
88
* <p>By default, logging goes through SLF4J. Users can override the backend programmatically before
99
* creating any SDK client:
@@ -13,20 +13,22 @@
1313
* WorkspaceClient ws = new WorkspaceClient();
1414
* }</pre>
1515
*
16-
* <p>Extend this class to provide a fully custom logging backend.
16+
* <p>Implement {@link ILoggerFactory} to provide a fully custom logging backend.
1717
*/
18-
public abstract class LoggerFactory {
18+
public final class LoggerFactory {
1919

20-
private static final AtomicReference<LoggerFactory> defaultFactory = new AtomicReference<>();
20+
private static final AtomicReference<ILoggerFactory> defaultFactory = new AtomicReference<>();
21+
22+
private LoggerFactory() {}
2123

2224
/** Returns a logger for the given class, using the current default factory. */
2325
public static Logger getLogger(Class<?> type) {
24-
return getDefault().createLogger(type);
26+
return getDefault().getLogger(type);
2527
}
2628

2729
/** Returns a logger with the given name, using the current default factory. */
2830
public static Logger getLogger(String name) {
29-
return getDefault().createLogger(name);
31+
return getDefault().getLogger(name);
3032
}
3133

3234
/**
@@ -35,31 +37,19 @@ public static Logger getLogger(String name) {
3537
* <p>Must be called before creating any SDK client or calling {@link #getLogger}. Loggers already
3638
* obtained will not be affected by subsequent calls.
3739
*/
38-
public static void setDefault(LoggerFactory factory) {
40+
public static void setDefault(ILoggerFactory factory) {
3941
if (factory == null) {
40-
throw new IllegalArgumentException("LoggerFactory must not be null");
42+
throw new IllegalArgumentException("ILoggerFactory must not be null");
4143
}
4244
defaultFactory.set(factory);
4345
}
4446

45-
static LoggerFactory getDefault() {
46-
LoggerFactory f = defaultFactory.get();
47+
static ILoggerFactory getDefault() {
48+
ILoggerFactory f = defaultFactory.get();
4749
if (f != null) {
4850
return f;
4951
}
5052
defaultFactory.compareAndSet(null, Slf4jLoggerFactory.INSTANCE);
5153
return defaultFactory.get();
5254
}
53-
54-
/**
55-
* Creates a {@link Logger} for the given class. Subclasses obtain the backend logger (e.g. SLF4J)
56-
* and return an adapter.
57-
*/
58-
protected abstract Logger createLogger(Class<?> type);
59-
60-
/**
61-
* Creates a {@link Logger} for the given name. Subclasses obtain the backend logger and return an
62-
* adapter.
63-
*/
64-
protected abstract Logger createLogger(String name);
6555
}

databricks-sdk-java/src/main/java/com/databricks/sdk/core/logging/Slf4jLogger.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ class Slf4jLogger extends Logger {
1313

1414
@Override
1515
public void debug(String msg) {
16-
if (delegate.isDebugEnabled()) {
17-
delegate.debug(msg);
18-
}
16+
delegate.debug(msg);
1917
}
2018

2119
@Override
2220
public void debug(String format, Object... args) {
23-
if (delegate.isDebugEnabled()) {
24-
delegate.debug(format, args);
25-
}
21+
delegate.debug(format, args);
2622
}
2723

2824
@Override
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.databricks.sdk.core.logging;
22

3-
/** A {@link LoggerFactory} backed by SLF4J. */
4-
public class Slf4jLoggerFactory extends LoggerFactory {
3+
/** An {@link ILoggerFactory} backed by SLF4J. */
4+
public class Slf4jLoggerFactory implements ILoggerFactory {
55

66
public static final Slf4jLoggerFactory INSTANCE = new Slf4jLoggerFactory();
77

88
@Override
9-
protected Logger createLogger(Class<?> type) {
9+
public Logger getLogger(Class<?> type) {
1010
return new Slf4jLogger(org.slf4j.LoggerFactory.getLogger(type));
1111
}
1212

1313
@Override
14-
protected Logger createLogger(String name) {
14+
public Logger getLogger(String name) {
1515
return new Slf4jLogger(org.slf4j.LoggerFactory.getLogger(name));
1616
}
1717
}

0 commit comments

Comments
 (0)