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

[Backport 2.x] Add calling class name to mock log appender #12017

Merged
merged 1 commit into from
Jan 25, 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 @@ -35,6 +35,7 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.filter.RegexFilter;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.regex.Regex;
Expand Down Expand Up @@ -68,11 +69,19 @@ public class MockLogAppender extends AbstractAppender implements AutoCloseable {
* write to a closed MockLogAppender instance.
*/
public static MockLogAppender createForLoggers(Logger... loggers) throws IllegalAccessException {
return createForLoggers(".*(\n.*)*", loggers);
final String callingClass = Thread.currentThread().getStackTrace()[2].getClassName();
return createForLoggersInternal(callingClass, ".*(\n.*)*", loggers);
}

public static MockLogAppender createForLoggers(String filter, Logger... loggers) throws IllegalAccessException {
final String callingClass = Thread.currentThread().getStackTrace()[2].getClassName();
return createForLoggersInternal(callingClass, filter, loggers);
}

private static MockLogAppender createForLoggersInternal(String callingClass, String filter, Logger... loggers)
throws IllegalAccessException {
final MockLogAppender appender = new MockLogAppender(
callingClass + "-mock-log-appender",
RegexFilter.createFilter(filter, new String[0], false, null, null),
Collections.unmodifiableList(Arrays.asList(loggers))
);
Expand All @@ -83,8 +92,8 @@ public static MockLogAppender createForLoggers(String filter, Logger... loggers)
return appender;
}

private MockLogAppender(RegexFilter filter, List<Logger> loggers) {
super("mock", filter, null);
private MockLogAppender(String name, RegexFilter filter, List<Logger> loggers) {
super(name, filter, null, true, Property.EMPTY_ARRAY);
/*
* We use a copy-on-write array list since log messages could be appended while we are setting up expectations. When that occurs,
* we would run into a concurrent modification exception from the iteration over the expectations in #append, concurrent with a
Expand Down Expand Up @@ -116,7 +125,14 @@ public void close() {
for (Logger logger : loggers) {
Loggers.removeAppender(logger, this);
}
this.stop();
super.stop();
}

@Override
public void stop() {
// MockLogAppender should be used with try-with-resources to ensure
// proper clean up ordering and should never be stopped directly.
throw new UnsupportedOperationException("Use close() to ensure proper clean up ordering");
}

public interface LoggingExpectation {
Expand Down
Loading