Skip to content

Commit

Permalink
Make InternalLogger an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Dec 12, 2022
1 parent c01fee6 commit 0fcbedc
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public InternalLogger create(String name) {
return new Slf4jApplicationLogger(name);
}

static final class Slf4jApplicationLogger extends InternalLogger {
static final class Slf4jApplicationLogger implements InternalLogger {

private final Logger slf4jLogger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,25 @@

package io.opentelemetry.javaagent.bootstrap;

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

public abstract class InternalLogger {
public interface 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 void initialize(Factory factory) {
InternalLoggerFactoryHolder.initialize(factory);
}

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

public abstract boolean isLoggable(Level level);
boolean isLoggable(Level level);

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

public abstract String name();
String name();

public enum Level {
enum Level {
ERROR,
WARN,
INFO,
Expand All @@ -43,7 +32,7 @@ public enum Level {
}

@FunctionalInterface
public interface Factory {
interface Factory {

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

package io.opentelemetry.javaagent.bootstrap;

import java.util.concurrent.atomic.AtomicReference;

final class InternalLoggerFactoryHolder {

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

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

static InternalLogger.Factory get() {
return loggerFactory.get();
}

private InternalLoggerFactoryHolder() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public InternalLogger create(String name) {
return new NoopLogger(name);
}

private static final class NoopLogger extends InternalLogger {
private static final class NoopLogger implements InternalLogger {

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.opentelemetry.javaagent.bootstrap.InternalLogger;
import javax.annotation.Nullable;

final class ApplicationLogger extends InternalLogger {
final class ApplicationLogger implements InternalLogger {

private final InMemoryLogStore inMemoryLogStore;
private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void shouldLogEverything() throws InterruptedException {
.hasSize(count.get());
}

private static final class TestLogger extends InternalLogger {
private static final class TestLogger implements InternalLogger {

private final String name;
private final List<Integer> logMessages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class Slf4jSimpleLogger extends InternalLogger {
final class Slf4jSimpleLogger implements InternalLogger {

static Slf4jSimpleLogger create(String name) {
return new Slf4jSimpleLogger(name);
Expand Down

0 comments on commit 0fcbedc

Please sign in to comment.