|
@@ -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));
|
|
|
+ }
|
|
|
+
|
|
|
}
|