-
Notifications
You must be signed in to change notification settings - Fork 867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encapsulate logging #6543
Merged
trask
merged 5 commits into
open-telemetry:main
from
mateuszrzeszutek:encapsulate-logging
Sep 12, 2022
Merged
Encapsulate logging #6543
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,12 +92,12 @@ class JavaagentTestArgumentsProvider( | |
"-Dotel.metrics.exporter=otlp", | ||
// suppress repeated logging of "No metric data to export - skipping export." | ||
// since PeriodicMetricReader is configured with a short interval | ||
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.opentelemetry.sdk.metrics.export.PeriodicMetricReader=INFO", | ||
"-Dio.opentelemetry.javaagent.logging.simple.slf4j.simpleLogger.log.io.opentelemetry.sdk.metrics.export.PeriodicMetricReader=INFO", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wow thats quite a property name 😂 |
||
// suppress a couple of verbose ClassNotFoundException stack traces logged at debug level | ||
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.internal.ServerImplBuilder=INFO", | ||
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.internal.ManagedChannelImplBuilder=INFO", | ||
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.perfmark.PerfMark=INFO", | ||
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.Context=INFO" | ||
"-Dio.opentelemetry.javaagent.logging.simple.slf4j.simpleLogger.log.io.grpc.internal.ServerImplBuilder=INFO", | ||
"-Dio.opentelemetry.javaagent.logging.simple.slf4j.simpleLogger.log.io.grpc.internal.ManagedChannelImplBuilder=INFO", | ||
"-Dio.opentelemetry.javaagent.logging.simple.slf4j.simpleLogger.log.io.perfmark.PerfMark=INFO", | ||
"-Dio.opentelemetry.javaagent.logging.simple.slf4j.simpleLogger.log.io.grpc.Context=INFO" | ||
) | ||
} | ||
|
||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍