-
Notifications
You must be signed in to change notification settings - Fork 867
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulate actual logging implementation better
- Loading branch information
Mateusz Rzeszutek
committed
Sep 5, 2022
1 parent
78b3fae
commit 00b2fa7
Showing
25 changed files
with
597 additions
and
550 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/InternalLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...agent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/NoopLoggerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.