Skip to content

Commit 5156924

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 5156924

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed
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+
* Contract for creating {@link Logger} instances.
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+
/** Creates a logger for the given class. */
12+
Logger createLogger(Class<?> type);
13+
14+
/** Creates a logger with the given name. */
15+
Logger createLogger(String name);
16+
}

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

Lines changed: 10 additions & 20 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,11 +13,13 @@
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) {
@@ -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
}
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 createLogger(Class<?> type) {
1010
return new Slf4jLogger(org.slf4j.LoggerFactory.getLogger(type));
1111
}
1212

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

0 commit comments

Comments
 (0)