Separate configs for logging tests
The evil logger tests rely on external configuration. This configuration is shared between these tests which means that changing the configuration for one test can cause an unrelated test to fail. In particular, removing the appenders on the root logger so that inherited loggers in one test do not have a console and file appender by default breaks tests that were expecting the root logger to have these appenders. This commit separates these configs so that these tests are not subject to this problem.
This commit is contained in:
parent
9f1f5fdedc
commit
d547b79224
|
@ -49,26 +49,6 @@ import static org.hamcrest.Matchers.equalTo;
|
|||
|
||||
public class EvilLoggerTests extends ESTestCase {
|
||||
|
||||
private Logger testLogger;
|
||||
private DeprecationLogger deprecationLogger;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
final Path configDir = getDataPath("config");
|
||||
// need to set custom path.conf so we can use a custom log4j2.properties file for the test
|
||||
final Settings settings = Settings.builder()
|
||||
.put(Environment.PATH_CONF_SETTING.getKey(), configDir.toAbsolutePath())
|
||||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
|
||||
.build();
|
||||
final Environment environment = new Environment(settings);
|
||||
LogConfigurator.configure(environment, true);
|
||||
|
||||
testLogger = ESLoggerFactory.getLogger("test");
|
||||
deprecationLogger = ESLoggerFactory.getDeprecationLogger("test");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tearDown() throws Exception {
|
||||
LoggerContext context = (LoggerContext) LogManager.getContext(false);
|
||||
|
@ -77,6 +57,10 @@ public class EvilLoggerTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testLocationInfoTest() throws IOException {
|
||||
setupLogging("location_info");
|
||||
|
||||
final Logger testLogger = ESLoggerFactory.getLogger("test");
|
||||
|
||||
testLogger.error("This is an error message");
|
||||
testLogger.warn("This is a warning message");
|
||||
testLogger.info("This is an info message");
|
||||
|
@ -94,15 +78,11 @@ public class EvilLoggerTests extends ESTestCase {
|
|||
assertLogLine(events.get(4), Level.TRACE, location, "This is a trace message");
|
||||
}
|
||||
|
||||
private void assertLogLine(final String logLine, final Level level, final String location, final String message) {
|
||||
final Matcher matcher = Pattern.compile("\\[(.*)\\]\\[(.*)\\(.*\\)\\] (.*)").matcher(logLine);
|
||||
assertTrue(logLine, matcher.matches());
|
||||
assertThat(matcher.group(1), equalTo(level.toString()));
|
||||
assertThat(matcher.group(2), RegexMatcher.matches(location));
|
||||
assertThat(matcher.group(3), RegexMatcher.matches(message));
|
||||
}
|
||||
|
||||
public void testDeprecationLogger() throws IOException {
|
||||
setupLogging("deprecation");
|
||||
|
||||
final DeprecationLogger deprecationLogger = new DeprecationLogger(ESLoggerFactory.getLogger("deprecation"));
|
||||
|
||||
deprecationLogger.deprecated("This is a deprecation message");
|
||||
final String deprecationPath = System.getProperty("es.logs") + "_deprecation.log";
|
||||
final List<String> deprecationEvents = Files.readAllLines(PathUtils.get(deprecationPath));
|
||||
|
@ -114,13 +94,17 @@ public class EvilLoggerTests extends ESTestCase {
|
|||
"This is a deprecation message");
|
||||
}
|
||||
|
||||
public void testFindAppender() {
|
||||
final Appender testLoggerConsoleAppender = Loggers.findAppender(testLogger, ConsoleAppender.class);
|
||||
public void testFindAppender() throws IOException {
|
||||
setupLogging("find_appender");
|
||||
|
||||
final Logger hasConsoleAppender = ESLoggerFactory.getLogger("has_console_appender");
|
||||
|
||||
final Appender testLoggerConsoleAppender = Loggers.findAppender(hasConsoleAppender, ConsoleAppender.class);
|
||||
assertNotNull(testLoggerConsoleAppender);
|
||||
assertThat(testLoggerConsoleAppender.getName(), equalTo("console"));
|
||||
final Logger countingNoOpLogger = ESLoggerFactory.getLogger("counting_no_op");
|
||||
assertNull(Loggers.findAppender(countingNoOpLogger, ConsoleAppender.class));
|
||||
final Appender countingNoOpAppender = Loggers.findAppender(countingNoOpLogger, CountingNoOpAppender.class);
|
||||
final Logger hasCountingNoOpAppender = ESLoggerFactory.getLogger("has_counting_no_op_appender");
|
||||
assertNull(Loggers.findAppender(hasCountingNoOpAppender, ConsoleAppender.class));
|
||||
final Appender countingNoOpAppender = Loggers.findAppender(hasCountingNoOpAppender, CountingNoOpAppender.class);
|
||||
assertThat(countingNoOpAppender.getName(), equalTo("counting_no_op"));
|
||||
}
|
||||
|
||||
|
@ -165,4 +149,23 @@ public class EvilLoggerTests extends ESTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
private void setupLogging(final String config) throws IOException {
|
||||
final Path configDir = getDataPath(config);
|
||||
// need to set custom path.conf so we can use a custom log4j2.properties file for the test
|
||||
final Settings settings = Settings.builder()
|
||||
.put(Environment.PATH_CONF_SETTING.getKey(), configDir.toAbsolutePath())
|
||||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
|
||||
.build();
|
||||
final Environment environment = new Environment(settings);
|
||||
LogConfigurator.configure(environment, true);
|
||||
}
|
||||
|
||||
private void assertLogLine(final String logLine, final Level level, final String location, final String message) {
|
||||
final Matcher matcher = Pattern.compile("\\[(.*)\\]\\[(.*)\\(.*\\)\\] (.*)").matcher(logLine);
|
||||
assertTrue(logLine, matcher.matches());
|
||||
assertThat(matcher.group(1), equalTo(level.toString()));
|
||||
assertThat(matcher.group(2), RegexMatcher.matches(location));
|
||||
assertThat(matcher.group(3), RegexMatcher.matches(message));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,6 +11,10 @@ appender.file.fileName = ${sys:es.logs}.log
|
|||
appender.file.layout.type = PatternLayout
|
||||
appender.file.layout.pattern = [%p][%l] %m%n
|
||||
|
||||
rootLogger.level = info
|
||||
rootLogger.appenderRef.console.ref = console
|
||||
rootLogger.appenderRef.file.ref = file
|
||||
|
||||
logger.test.name = test
|
||||
logger.test.level = trace
|
||||
logger.test.appenderRef.console.ref = console
|
||||
|
@ -28,8 +32,3 @@ logger.deprecation.level = warn
|
|||
logger.deprecation.appenderRef.deprecation_file.ref = deprecation_file
|
||||
logger.deprecation.additivity = false
|
||||
|
||||
appender.counting_no_op.type = CountingNoOp
|
||||
appender.counting_no_op.name = counting_no_op
|
||||
|
||||
logger.counting_no_op.name = counting_no_op
|
||||
logger.counting_no_op.appenderRef.counting_no_op.ref = counting_no_op
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
logger.level: INFO
|
||||
rootLogger: ${logger.level}, terminal
|
|
@ -0,0 +1,27 @@
|
|||
status = error
|
||||
|
||||
appender.console.type = Console
|
||||
appender.console.name = console
|
||||
appender.console.layout.type = PatternLayout
|
||||
appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c] %m%n
|
||||
|
||||
appender.file.type = File
|
||||
appender.file.name = file
|
||||
appender.file.fileName = ${sys:es.logs}.log
|
||||
appender.file.layout.type = PatternLayout
|
||||
appender.file.layout.pattern = [%p][%l] %m%n
|
||||
|
||||
rootLogger.level = info
|
||||
rootLogger.appenderRef.console.ref = console
|
||||
rootLogger.appenderRef.file.ref = file
|
||||
|
||||
appender.deprecation_file.type = File
|
||||
appender.deprecation_file.name = deprecation_file
|
||||
appender.deprecation_file.fileName = ${sys:es.logs}_deprecation.log
|
||||
appender.deprecation_file.layout.type = PatternLayout
|
||||
appender.deprecation_file.layout.pattern = [%p][%l] %m%n
|
||||
|
||||
logger.deprecation.name = deprecation
|
||||
logger.deprecation.level = warn
|
||||
logger.deprecation.appenderRef.deprecation_file.ref = deprecation_file
|
||||
logger.deprecation.additivity = false
|
|
@ -0,0 +1,17 @@
|
|||
status = error
|
||||
|
||||
appender.console.type = Console
|
||||
appender.console.name = console
|
||||
appender.console.layout.type = PatternLayout
|
||||
appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c] %m%n
|
||||
|
||||
logger.has_console_appender.name = has_console_appender
|
||||
logger.has_console_appender.level = trace
|
||||
logger.has_console_appender.appenderRef.console.ref = console
|
||||
logger.has_console_appender.additivity = false
|
||||
|
||||
appender.counting_no_op.type = CountingNoOp
|
||||
appender.counting_no_op.name = counting_no_op
|
||||
|
||||
logger.has_counting_no_op_appender.name = has_counting_no_op_appender
|
||||
logger.has_counting_no_op_appender.appenderRef.counting_no_op.ref = counting_no_op
|
|
@ -0,0 +1,22 @@
|
|||
status = error
|
||||
|
||||
appender.console.type = Console
|
||||
appender.console.name = console
|
||||
appender.console.layout.type = PatternLayout
|
||||
appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c] %m%n
|
||||
|
||||
appender.file.type = File
|
||||
appender.file.name = file
|
||||
appender.file.fileName = ${sys:es.logs}.log
|
||||
appender.file.layout.type = PatternLayout
|
||||
appender.file.layout.pattern = [%p][%l] %m%n
|
||||
|
||||
rootLogger.level = info
|
||||
rootLogger.appenderRef.console.ref = console
|
||||
rootLogger.appenderRef.file.ref = file
|
||||
|
||||
logger.test.name = test
|
||||
logger.test.level = trace
|
||||
logger.test.appenderRef.console.ref = console
|
||||
logger.test.appenderRef.file.ref = file
|
||||
logger.test.additivity = false
|
Loading…
Reference in New Issue