Skip to content

Commit 581eb61

Browse files
committed
Add Class and Package Level support for TestLogging
1 parent 9d8361a commit 581eb61

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

src/test/java/org/elasticsearch/test/junit/annotations/TestLogging.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
package org.elasticsearch.test.junit.annotations;
2020

21-
import java.lang.annotation.ElementType;
2221
import java.lang.annotation.Retention;
2322
import java.lang.annotation.RetentionPolicy;
2423
import java.lang.annotation.Target;
2524

25+
import static java.lang.annotation.ElementType.METHOD;
26+
import static java.lang.annotation.ElementType.PACKAGE;
27+
import static java.lang.annotation.ElementType.TYPE;
28+
2629
/**
2730
* Annotation used to set a custom log level for a specific test method.
2831
*
@@ -32,7 +35,7 @@
3235
* or just @TestLogging("_root:DEBUG,cluster.metadata:TRACE") since we start the test with -Des.logger.prefix=
3336
*/
3437
@Retention(RetentionPolicy.RUNTIME)
35-
@Target(ElementType.METHOD)
38+
@Target({PACKAGE, TYPE, METHOD})
3639
public @interface TestLogging {
3740
String value();
3841
}

src/test/java/org/elasticsearch/test/junit/listeners/LoggingListener.java

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.common.logging.Loggers;
2424
import org.elasticsearch.test.junit.annotations.TestLogging;
2525
import org.junit.runner.Description;
26+
import org.junit.runner.Result;
2627
import org.junit.runner.notification.RunListener;
2728

2829
import java.util.HashMap;
@@ -41,35 +42,30 @@
4142
public class LoggingListener extends RunListener {
4243

4344
private Map<String, String> previousLoggingMap;
45+
private Map<String, String> previousClassLoggingMap;
46+
private Map<String, String> previousPackageLoggingMap;
47+
48+
@Override
49+
public void testRunStarted(Description description) throws Exception {
50+
previousPackageLoggingMap = processTestLogging( description.getTestClass().getPackage().getAnnotation(TestLogging.class));
51+
previousClassLoggingMap = processTestLogging(description.getAnnotation(TestLogging.class));
52+
}
53+
54+
@Override
55+
public void testRunFinished(Result result) throws Exception {
56+
previousClassLoggingMap = reset(previousClassLoggingMap);
57+
previousPackageLoggingMap = reset(previousPackageLoggingMap);
58+
}
4459

4560
@Override
4661
public void testStarted(Description description) throws Exception {
47-
TestLogging testLogging = description.getAnnotation(TestLogging.class);
48-
if (testLogging != null) {
49-
this.previousLoggingMap = new HashMap<String, String>();
50-
String[] loggersAndLevels = testLogging.value().split(",");
51-
for (String loggerAndLevel : loggersAndLevels) {
52-
String[] loggerAndLevelArray = loggerAndLevel.split(":");
53-
if (loggerAndLevelArray.length >=2) {
54-
String loggerName = loggerAndLevelArray[0];
55-
String level = loggerAndLevelArray[1];
56-
ESLogger esLogger = resolveLogger(loggerName);
57-
this.previousLoggingMap.put(loggerName, esLogger.getLevel());
58-
esLogger.setLevel(level);
59-
}
60-
}
61-
}
62+
final TestLogging testLogging = description.getAnnotation(TestLogging.class);
63+
previousLoggingMap = processTestLogging(testLogging);
6264
}
6365

6466
@Override
6567
public void testFinished(Description description) throws Exception {
66-
if (this.previousLoggingMap != null) {
67-
for (Map.Entry<String, String> previousLogger : previousLoggingMap.entrySet()) {
68-
ESLogger esLogger = resolveLogger(previousLogger.getKey());
69-
esLogger.setLevel(previousLogger.getValue());
70-
}
71-
this.previousLoggingMap = null;
72-
}
68+
previousLoggingMap = reset(previousLoggingMap);
7369
}
7470

7571
private static ESLogger resolveLogger(String loggerName) {
@@ -78,4 +74,33 @@ private static ESLogger resolveLogger(String loggerName) {
7874
}
7975
return Loggers.getLogger(loggerName);
8076
}
77+
78+
private Map<String, String> processTestLogging(TestLogging testLogging) {
79+
if (testLogging == null) {
80+
return null;
81+
}
82+
Map<String, String> map = new HashMap<String, String>();
83+
final String[] loggersAndLevels = testLogging.value().split(",");
84+
for (String loggerAndLevel : loggersAndLevels) {
85+
String[] loggerAndLevelArray = loggerAndLevel.split(":");
86+
if (loggerAndLevelArray.length >=2) {
87+
String loggerName = loggerAndLevelArray[0];
88+
String level = loggerAndLevelArray[1];
89+
ESLogger esLogger = resolveLogger(loggerName);
90+
map.put(loggerName, esLogger.getLevel());
91+
esLogger.setLevel(level);
92+
}
93+
}
94+
return map;
95+
}
96+
97+
private Map<String, String> reset(Map<String, String> map) {
98+
if (map != null) {
99+
for (Map.Entry<String, String> previousLogger : map.entrySet()) {
100+
ESLogger esLogger = resolveLogger(previousLogger.getKey());
101+
esLogger.setLevel(previousLogger.getValue());
102+
}
103+
}
104+
return null;
105+
}
81106
}

0 commit comments

Comments
 (0)