Skip to content

Commit

Permalink
Encapsulate logging (open-telemetry#6543)
Browse files Browse the repository at this point in the history
* Encapsulate actual logging implementation better

* Apply suggestions from code review

Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>

* code review comments

* revert to the old slf4j package name

* spotless

Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
  • Loading branch information
2 people authored and LironKS committed Dec 4, 2022
1 parent 9bac9b4 commit 72ae9d3
Show file tree
Hide file tree
Showing 21 changed files with 584 additions and 539 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ tasks.withType<ShadowJar>().configureEach {

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

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

Expand Down
2 changes: 0 additions & 2 deletions examples/distro/gradle/shadow.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
ext.relocatePackages = { shadowJar ->
// Prevents conflict with other SLF4J instances. Important for premain.
shadowJar.relocate 'org.slf4j', 'io.opentelemetry.javaagent.slf4j'
// rewrite dependencies calling Logger.getLogger
shadowJar.relocate 'java.util.logging.Logger', 'io.opentelemetry.javaagent.bootstrap.PatchLogger'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ tasks.withType<ShadowJar>().configureEach {

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

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

Expand Down
4 changes: 0 additions & 4 deletions javaagent-bootstrap/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ plugins {

group = "io.opentelemetry.javaagent"

val agentSlf4jVersion = "2.0.0"

dependencies {
implementation(project(":instrumentation-api"))
implementation(project(":instrumentation-appender-api-internal"))
implementation("org.slf4j:slf4j-api:$agentSlf4jVersion")
implementation("org.slf4j:slf4j-simple:$agentSlf4jVersion")

testImplementation(project(":testing-common"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@
*/
public class AgentClassLoader extends URLClassLoader {

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

static {
ClassLoader.registerAsParallelCapable();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.bootstrap;

import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;

public abstract class InternalLogger {

private static final AtomicReference<Factory> loggerFactory =
new AtomicReference<>(NoopLoggerFactory.INSTANCE);

public static void initialize(Factory factory) {
if (!loggerFactory.compareAndSet(NoopLoggerFactory.INSTANCE, factory)) {
factory
.create(InternalLogger.class.getName())
.log(
Level.WARN,
"Developer error: logging system has already been initialized once",
null);
}
}

static InternalLogger getLogger(String name) {
return loggerFactory.get().create(name);
}

protected abstract boolean isLoggable(Level level);

protected abstract void log(Level level, String message, @Nullable Throwable error);

protected abstract String name();

public enum Level {
ERROR,
WARN,
INFO,
DEBUG,
TRACE
}

@FunctionalInterface
public interface Factory {

InternalLogger create(String name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.bootstrap;

import javax.annotation.Nullable;

final class NoopLoggerFactory implements InternalLogger.Factory {

static final InternalLogger.Factory INSTANCE = new NoopLoggerFactory();

private NoopLoggerFactory() {}

@Override
public InternalLogger create(String name) {
return new NoopLogger(name);
}

private static final class NoopLogger extends InternalLogger {

private final String name;

private NoopLogger(String name) {
this.name = name;
}

@Override
public boolean isLoggable(Level level) {
return false;
}

@Override
public void log(Level level, String message, @Nullable Throwable error) {}

@Override
protected String name() {
return name;
}
}
}
Loading

0 comments on commit 72ae9d3

Please sign in to comment.