Skip to content

Commit 3fdd545

Browse files
Improve logging when jdbc is shaded (#1093)
## Description <!-- Provide a brief summary of the changes made and the issue they aim to address.--> Improve logging when jdbc is shaded ## Testing <!-- Describe how the changes have been tested--> Unit tests + manually in benchmarking ## Additional Notes to the Reviewer <!-- Share any additional context or insights that may help the reviewer understand the changes better. This could include challenges faced, limitations, or compromises made during the development process. Also, mention any areas of the code that you would like the reviewer to focus on specifically. --> Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
1 parent 61e996b commit 3fdd545

3 files changed

Lines changed: 95 additions & 1 deletion

File tree

NEXT_CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
### Updated
88

99
### Fixed
10-
10+
- Fixed logging to respect params when the driver is shaded.
1111
---
1212
*Note: When making changes, please add your change under the appropriate section with a brief description.*

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ private static String getPackagePrefix() {
233233
if (prefix != null && !prefix.isEmpty()) {
234234
return prefix;
235235
}
236+
// Auto-detect the actual package prefix by using the current class
237+
// This handles shaded JARs where the package might be relocated
238+
String actualPackageName = JulLogger.class.getPackage().getName();
239+
// Extract the root prefix (e.g., "jdbc.shaded.v1.0.12.OSS.com.databricks.jdbc"
240+
// or "com.databricks.jdbc")
241+
if (actualPackageName.contains(DEFAULT_PACKAGE_PREFIX)) {
242+
// Find the actual prefix including any shading prefix
243+
int defaultPrefixIndex = actualPackageName.indexOf(DEFAULT_PACKAGE_PREFIX);
244+
if (defaultPrefixIndex > 0) {
245+
// Include everything up to and including the default prefix
246+
return actualPackageName.substring(0, defaultPrefixIndex + DEFAULT_PACKAGE_PREFIX.length());
247+
}
248+
}
236249
return DEFAULT_PACKAGE_PREFIX;
237250
}
238251

@@ -241,6 +254,17 @@ private static String getDriverPackagePrefix() {
241254
if (StringUtils.isNotEmpty(prefix)) {
242255
return prefix;
243256
}
257+
// Auto-detect the actual driver package prefix
258+
// Try to determine if the driver package is also shaded
259+
String mainPackagePrefix = getPackagePrefix();
260+
// Check if we have a shaded prefix
261+
if (!mainPackagePrefix.equals(DEFAULT_PACKAGE_PREFIX)
262+
&& mainPackagePrefix.contains(DEFAULT_PACKAGE_PREFIX)) {
263+
// Extract the shading prefix and apply it to the driver package
264+
String shadingPrefix =
265+
mainPackagePrefix.substring(0, mainPackagePrefix.indexOf(DEFAULT_PACKAGE_PREFIX));
266+
return shadingPrefix + DEFAULT_DRIVER_PACKAGE_PREFIX;
267+
}
244268
return DEFAULT_DRIVER_PACKAGE_PREFIX;
245269
}
246270

src/test/java/com/databricks/jdbc/log/JulLoggerTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,74 @@ private String[] methodCallingLogger() {
183183
private String[] info() {
184184
return JulLogger.getCaller();
185185
}
186+
187+
@Test
188+
void testPackagePrefixDetectionForUnshadedJar() {
189+
// Test that package prefix detection works for unshaded JARs
190+
String packagePrefix = JulLogger.PARENT_CLASS_PREFIX;
191+
// For unshaded JARs, it should be the default prefix
192+
assertTrue(
193+
packagePrefix.endsWith("com.databricks.jdbc"),
194+
"Package prefix should end with com.databricks.jdbc, got: " + packagePrefix);
195+
}
196+
197+
@Test
198+
void testPackagePrefixDetectionForShadedJar() {
199+
// Test that the package prefix includes any shading prefix
200+
// The JulLogger should be able to detect its own package correctly
201+
String actualPackageName = JulLogger.class.getPackage().getName();
202+
String packagePrefix = JulLogger.PARENT_CLASS_PREFIX;
203+
204+
// Verify that the package prefix matches the actual runtime package structure
205+
assertTrue(
206+
actualPackageName.startsWith(packagePrefix),
207+
String.format(
208+
"Actual package '%s' should start with detected prefix '%s'",
209+
actualPackageName, packagePrefix));
210+
211+
// If the jar is shaded, the prefix should include the shading prefix
212+
if (actualPackageName.contains(".") && !actualPackageName.startsWith("com.databricks.jdbc")) {
213+
// This means we have a shaded jar
214+
assertTrue(
215+
packagePrefix.contains("com.databricks.jdbc"),
216+
"Shaded package prefix should still contain com.databricks.jdbc");
217+
}
218+
}
219+
220+
@Test
221+
void testInitLoggerConfiguresCorrectPackageForShadedJar() throws IOException {
222+
// This test verifies that when initLogger is called, it configures loggers
223+
// for the correct package prefix (whether shaded or unshaded)
224+
JulLogger.initLogger(Level.OFF, JulLogger.STDOUT, 1024, 1);
225+
226+
// Get the configured logger
227+
Logger jdbcLogger = Logger.getLogger(JulLogger.PARENT_CLASS_PREFIX);
228+
229+
// Verify it was configured with the correct level
230+
assertEquals(
231+
Level.OFF,
232+
jdbcLogger.getLevel(),
233+
"Logger should be configured with Level.OFF to suppress all logs");
234+
235+
// Verify that a logger created with the actual package name inherits the settings
236+
String testLoggerName =
237+
JulLogger.class.getPackage().getName() + ".api.impl.ExecutionResultFactory";
238+
Logger testLogger = Logger.getLogger(testLoggerName);
239+
240+
// The effective level should be OFF (inherited from parent)
241+
Level effectiveLevel = testLogger.getLevel();
242+
// If the local level is null, it inherits from parent
243+
if (effectiveLevel == null) {
244+
Logger parent = testLogger.getParent();
245+
while (parent != null && effectiveLevel == null) {
246+
effectiveLevel = parent.getLevel();
247+
parent = parent.getParent();
248+
}
249+
}
250+
251+
assertEquals(
252+
Level.OFF,
253+
effectiveLevel,
254+
"Child loggers should inherit Level.OFF from properly configured parent logger");
255+
}
186256
}

0 commit comments

Comments
 (0)