Skip to content

Commit 8b2b328

Browse files
Mateusz Rzeszutektrask
andauthored
Encapsulate logging (open-telemetry#6543)
* Encapsulate actual logging implementation better * Apply suggestions from code review Co-authored-by: Trask Stalnaker <[email protected]> * code review comments * revert to the old slf4j package name * spotless Co-authored-by: Trask Stalnaker <[email protected]>
1 parent 7b139e9 commit 8b2b328

File tree

21 files changed

+584
-539
lines changed

21 files changed

+584
-539
lines changed

conventions/src/main/kotlin/io.opentelemetry.instrumentation.javaagent-shadowing.gradle.kts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ tasks.withType<ShadowJar>().configureEach {
1414

1515
exclude("**/module-info.class")
1616

17-
// Prevents conflict with other SLF4J instances. Important for premain.
18-
relocate("org.slf4j", "io.opentelemetry.javaagent.slf4j")
1917
// rewrite dependencies calling Logger.getLogger
2018
relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")
2119

examples/distro/gradle/shadow.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
ext.relocatePackages = { shadowJar ->
2-
// Prevents conflict with other SLF4J instances. Important for premain.
3-
shadowJar.relocate 'org.slf4j', 'io.opentelemetry.javaagent.slf4j'
42
// rewrite dependencies calling Logger.getLogger
53
shadowJar.relocate 'java.util.logging.Logger', 'io.opentelemetry.javaagent.bootstrap.PatchLogger'
64

gradle-plugins/src/main/kotlin/io.opentelemetry.instrumentation.muzzle-check.gradle.kts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ tasks.withType<ShadowJar>().configureEach {
8282

8383
exclude("**/module-info.class")
8484

85-
// Prevents conflict with other SLF4J instances. Important for premain.
86-
relocate("org.slf4j", "io.opentelemetry.javaagent.slf4j")
8785
// rewrite dependencies calling Logger.getLogger
8886
relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")
8987

javaagent-bootstrap/build.gradle.kts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ plugins {
55

66
group = "io.opentelemetry.javaagent"
77

8-
val agentSlf4jVersion = "2.0.0"
9-
108
dependencies {
119
implementation(project(":instrumentation-api"))
1210
implementation(project(":instrumentation-appender-api-internal"))
13-
implementation("org.slf4j:slf4j-api:$agentSlf4jVersion")
14-
implementation("org.slf4j:slf4j-simple:$agentSlf4jVersion")
1511

1612
testImplementation(project(":testing-common"))
1713
}

javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentClassLoader.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
*/
3434
public class AgentClassLoader extends URLClassLoader {
3535

36-
// NOTE it's important not to use slf4j in this class, because this class is used before slf4j is
37-
// configured, and so using slf4j here would initialize slf4j-simple before we have a chance to
38-
// configure the logging levels
36+
// NOTE it's important not to use logging in this class, because this class is used before logging
37+
// is initialized
3938

4039
static {
4140
ClassLoader.registerAsParallelCapable();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.bootstrap;
7+
8+
import java.util.concurrent.atomic.AtomicReference;
9+
import javax.annotation.Nullable;
10+
11+
public abstract class InternalLogger {
12+
13+
private static final AtomicReference<Factory> loggerFactory =
14+
new AtomicReference<>(NoopLoggerFactory.INSTANCE);
15+
16+
public static void initialize(Factory factory) {
17+
if (!loggerFactory.compareAndSet(NoopLoggerFactory.INSTANCE, factory)) {
18+
factory
19+
.create(InternalLogger.class.getName())
20+
.log(
21+
Level.WARN,
22+
"Developer error: logging system has already been initialized once",
23+
null);
24+
}
25+
}
26+
27+
static InternalLogger getLogger(String name) {
28+
return loggerFactory.get().create(name);
29+
}
30+
31+
protected abstract boolean isLoggable(Level level);
32+
33+
protected abstract void log(Level level, String message, @Nullable Throwable error);
34+
35+
protected abstract String name();
36+
37+
public enum Level {
38+
ERROR,
39+
WARN,
40+
INFO,
41+
DEBUG,
42+
TRACE
43+
}
44+
45+
@FunctionalInterface
46+
public interface Factory {
47+
48+
InternalLogger create(String name);
49+
}
50+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.bootstrap;
7+
8+
import javax.annotation.Nullable;
9+
10+
final class NoopLoggerFactory implements InternalLogger.Factory {
11+
12+
static final InternalLogger.Factory INSTANCE = new NoopLoggerFactory();
13+
14+
private NoopLoggerFactory() {}
15+
16+
@Override
17+
public InternalLogger create(String name) {
18+
return new NoopLogger(name);
19+
}
20+
21+
private static final class NoopLogger extends InternalLogger {
22+
23+
private final String name;
24+
25+
private NoopLogger(String name) {
26+
this.name = name;
27+
}
28+
29+
@Override
30+
public boolean isLoggable(Level level) {
31+
return false;
32+
}
33+
34+
@Override
35+
public void log(Level level, String message, @Nullable Throwable error) {}
36+
37+
@Override
38+
protected String name() {
39+
return name;
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)