Skip to content
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

Improve GJULE log message source detection #25046

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public String toString() {

private static class SourceDetector {

private static final Set<String> IGNORED_CLASSES = Set.of(
private static final Set<String> LOGGING_CLASSES = Set.of(
"org.glassfish.main.jul.GlassFishLogger",
"org.glassfish.main.jul.GlassFishLoggerWrapper",
// see LogDomains in GlassFish sources
Expand All @@ -308,8 +308,8 @@ private static class SourceDetector {
static void detectClassAndMethod(final LogRecord wrappedRecord) {
STACK_WALKER
.walk(stackFrames ->
stackFrames.dropWhile(frame -> !isIgnoredStackFrame(frame.getClassName()))
.filter(frame -> !isIgnoredStackFrame(frame.getClassName()))
stackFrames.dropWhile(SourceDetector::isSourceStackFrame)
.filter(SourceDetector::isSourceStackFrame)
.findFirst())
.ifPresent(frame -> {
wrappedRecord.setSourceClassName(frame.getClassName());
Expand All @@ -318,13 +318,33 @@ static void detectClassAndMethod(final LogRecord wrappedRecord) {
}

/**
* @param sourceClassName usually class which created this record
* @return if true the class will not be used as a source.
* @param stackFrame the stack frame
* @return {@code true} if the {@code stackFrame} will be used as a source
*/
private static boolean isIgnoredStackFrame(final String sourceClassName) {
return IGNORED_CLASSES.contains(sourceClassName)
private static boolean isSourceStackFrame(StackWalker.StackFrame stackFrame) {
return !isLoggingStackFrame(stackFrame)
&& !isReflectionStackFrame(stackFrame);
}

/**
* @param stackFrame the stack frame
* @return {@code true} if the {@code stackFrame} is logging frame
*/
private static boolean isLoggingStackFrame(final StackWalker.StackFrame stackFrame) {
final String sourceClassName = stackFrame.getClassName();
return LOGGING_CLASSES.contains(sourceClassName)
|| sourceClassName.startsWith("sun.util.logging.");
}

/**
* @param stackFrame the stack frame
* @return {@code true} if the {@code stackFrame} is reflection frame
*/
private static boolean isReflectionStackFrame(final StackWalker.StackFrame stackFrame) {
final String sourceClassName = stackFrame.getClassName();
return sourceClassName.startsWith("jdk.internal.reflect.")
|| sourceClassName.startsWith("java.lang.reflect.")
|| sourceClassName.startsWith("sun.util.logging.")
|| sourceClassName.startsWith("java.lang.invoke.")
|| sourceClassName.startsWith("sun.reflect.");
}
}
Expand Down
Loading