Skip to content

Commit 5c985c8

Browse files
authored
JDBC Client Driver Logger not being configured correctly (#967)
## Description JDBC client driver is present in a different namespace than the general codebase and due to this when we configure loglevel we only configure the root logger in the `com.databricks.jdbc` chain but the Driver falls in the `com.databricks.client.jdbc` chain and hence was not configured properly ## Issue #965 ## Testing Manual testing to view logs in file and console settings `NO_CHANGELOG=true`
1 parent a2b5fd0 commit 5c985c8

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/main/java/com/databricks/jdbc/common/util/LoggingUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public static void setupLogger(String logDir, int logFileSizeMB, int logFileCoun
1919
throws IOException {
2020
if (LOGGER instanceof JulLogger && System.getProperty(JAVA_UTIL_LOGGING_CONFIG_FILE) == null) {
2121
// Only configure JUL logger if it's not already configured via external properties file
22-
LOGGER.info("Setting up JUL logger");
2322
JulLogger.initLogger(toJulLevel(level), logDir, logFileSizeMB * 1024 * 1024, logFileCount);
23+
LOGGER.info("Setting up JUL logger");
2424
}
2525
}
2626

src/main/java/com/databricks/jdbc/log/JulLogger.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Set;
88
import java.util.logging.*;
99
import java.util.stream.Stream;
10+
import org.apache.commons.lang3.StringUtils;
1011

1112
/**
1213
* The {@code JulLogger} class provides an implementation of the {@link JdbcLogger} interface using
@@ -27,10 +28,14 @@ public class JulLogger implements JdbcLogger {
2728

2829
private static final String DEFAULT_PACKAGE_PREFIX = "com.databricks.jdbc";
2930

31+
private static final String DEFAULT_DRIVER_PACKAGE_PREFIX = "com.databricks.client.jdbc";
32+
3033
public static final String STDOUT = "STDOUT";
3134

3235
public static final String PARENT_CLASS_PREFIX = getPackagePrefix();
3336

37+
public static final String DRIVER_CLASS_PREFIX = getDriverPackagePrefix();
38+
3439
public static final String DATABRICKS_LOG_FILE = "databricks_jdbc.log";
3540

3641
public static final String JAVA_UTIL_LOGGING_CONFIG_FILE = "java.util.logging.config.file";
@@ -130,10 +135,15 @@ public static synchronized void initLogger(
130135
// java.util.logging uses hierarchical loggers, so we just need to set the log level on the
131136
// parent package logger
132137
Logger jdbcJulLogger = Logger.getLogger(PARENT_CLASS_PREFIX);
133-
134138
jdbcJulLogger.setLevel(level);
135139
jdbcJulLogger.setUseParentHandlers(false);
136140

141+
// Jdbc client driver is present in a different namespace and hence need to configure its
142+
// logger separately
143+
Logger jdbcDriverJulLogger = Logger.getLogger(DRIVER_CLASS_PREFIX);
144+
jdbcDriverJulLogger.setLevel(level);
145+
jdbcDriverJulLogger.setUseParentHandlers(false);
146+
137147
String logPattern = getLogPattern(logDir);
138148
Handler handler;
139149
if (logPattern.equalsIgnoreCase(STDOUT)) {
@@ -152,6 +162,7 @@ public void publish(LogRecord record) {
152162
handler.setLevel(level);
153163
handler.setFormatter(new Slf4jFormatter());
154164
jdbcJulLogger.addHandler(handler);
165+
jdbcDriverJulLogger.addHandler(handler);
155166
}
156167
}
157168

@@ -225,6 +236,14 @@ private static String getPackagePrefix() {
225236
return DEFAULT_PACKAGE_PREFIX;
226237
}
227238

239+
private static String getDriverPackagePrefix() {
240+
String prefix = System.getenv("JDBC_DRIVER_PACKAGE_PREFIX");
241+
if (StringUtils.isNotEmpty(prefix)) {
242+
return prefix;
243+
}
244+
return DEFAULT_DRIVER_PACKAGE_PREFIX;
245+
}
246+
228247
private String slf4jToJavaFormat(String format) {
229248
if (format == null) {
230249
return null;

0 commit comments

Comments
 (0)