Skip to content

Commit d504057

Browse files
authored
Merge pull request #210 from DataDog/ark/fix_dependencies_loggers
Patch getLogger calls to safe logger.
2 parents 9816eca + d703095 commit d504057

4 files changed

Lines changed: 131 additions & 0 deletions

File tree

dd-java-agent-ittests/dd-java-agent-ittests.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ if (JavaVersion.current() != JavaVersion.VERSION_1_8) {
1111
groovy {
1212
// These classes use Ratpack which requires Java 8. (Currently also incompatible with Java 9.)
1313
exclude '**/TestHttpServer.groovy', '**/ApacheHttpClientTest.groovy'
14+
15+
// log rewrite incompatible with Java9 test classpath
16+
exclude '**/TestLoggerRewrite.groovy'
1417
}
1518
}
1619
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package datadog.trace.agent.integration
2+
3+
import io.opentracing.util.GlobalTracer
4+
import java.lang.reflect.Field
5+
import spock.lang.Specification
6+
7+
class TestLoggerRewrite extends Specification {
8+
def "java getLogger rewritten to safe logger"() {
9+
setup:
10+
Field logField = GlobalTracer.getDeclaredField("LOGGER")
11+
logField.setAccessible(true)
12+
Object logger = logField.get(null)
13+
14+
expect:
15+
!logger.getClass().getName().startsWith("java.util.logging")
16+
17+
cleanup:
18+
logField?.setAccessible(false)
19+
}
20+
}

dd-java-agent/dd-java-agent.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ shadowJar {
8383
// This is used in the Cassandra Cluster.connectAsync signature so we can't relocate it. :fingers_crossed:
8484
exclude 'com.google.common.util.concurrent.ListenableFuture'
8585
}
86+
87+
// rewrite dependencies calling Logger.getLogger
88+
relocate 'java.util.logging.Logger', 'datadog.trace.agent.PatchLogger'
8689
}
8790

8891
dependencies {
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package datadog.trace.agent;
2+
3+
import java.util.ResourceBundle;
4+
import java.util.logging.Level;
5+
import java.util.logging.LogRecord;
6+
7+
/**
8+
* Dependencies of the agent somtimes call Logger.getLogger This can have the effect of initializing
9+
* the global logger incompatibly with the user's app.
10+
*
11+
* <p>Shadow rewrites will redirect those calls to this class, which will return a safe logger.
12+
*/
13+
public class PatchLogger {
14+
private static final PatchLogger SAFE_LOGGER = new PatchLogger("datadogSafeLogger", "bundle");
15+
16+
public static PatchLogger getLogger(String name) {
17+
return SAFE_LOGGER;
18+
}
19+
20+
public static PatchLogger getLogger(String name, String resourceBundleName) {
21+
return SAFE_LOGGER;
22+
}
23+
24+
public static PatchLogger getAnonymousLogger() {
25+
return SAFE_LOGGER;
26+
}
27+
28+
public static PatchLogger getAnonymousLogger(String resourceBundleName) {
29+
return SAFE_LOGGER;
30+
}
31+
32+
protected PatchLogger(String name, String resourceBundleName) {
33+
// super(name, resourceBundleName);
34+
}
35+
36+
// providing a bunch of empty log methods
37+
38+
public void log(LogRecord record) {}
39+
40+
public void log(Level level, String msg) {}
41+
42+
public void log(Level level, String msg, Object param1) {}
43+
44+
public void log(Level level, String msg, Object params[]) {}
45+
46+
public void log(Level level, String msg, Throwable thrown) {}
47+
48+
public void logp(Level level, String sourceClass, String sourceMethod, String msg) {}
49+
50+
public void logp(
51+
Level level, String sourceClass, String sourceMethod, String msg, Object param1) {}
52+
53+
public void logp(
54+
Level level, String sourceClass, String sourceMethod, String msg, Object params[]) {}
55+
56+
public void logp(
57+
Level level, String sourceClass, String sourceMethod, String msg, Throwable thrown) {}
58+
59+
public void logrb(
60+
Level level, String sourceClass, String sourceMethod, String bundleName, String msg) {}
61+
62+
public void logrb(
63+
Level level,
64+
String sourceClass,
65+
String sourceMethod,
66+
String bundleName,
67+
String msg,
68+
Object param1) {}
69+
70+
public void logrb(
71+
Level level,
72+
String sourceClass,
73+
String sourceMethod,
74+
String bundleName,
75+
String msg,
76+
Object params[]) {}
77+
78+
public void logrb(
79+
Level level,
80+
String sourceClass,
81+
String sourceMethod,
82+
ResourceBundle bundle,
83+
String msg,
84+
Object... params) {}
85+
86+
public void logrb(
87+
Level level,
88+
String sourceClass,
89+
String sourceMethod,
90+
String bundleName,
91+
String msg,
92+
Throwable thrown) {}
93+
94+
public void logrb(
95+
Level level,
96+
String sourceClass,
97+
String sourceMethod,
98+
ResourceBundle bundle,
99+
String msg,
100+
Throwable thrown) {}
101+
102+
public void throwing(String sourceClass, String sourceMethod, Throwable thrown) {}
103+
104+
public void setLevel(Level newLevel) throws SecurityException {}
105+
}

0 commit comments

Comments
 (0)