forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordingSimpleLogger.java
More file actions
83 lines (75 loc) · 2.72 KB
/
RecordingSimpleLogger.java
File metadata and controls
83 lines (75 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Copyright 2012-2019 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign.slf4j;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.SimpleLogger;
import org.slf4j.impl.SimpleLoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals;
import static org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY;
import static org.slf4j.impl.SimpleLogger.SHOW_THREAD_NAME_KEY;
/**
* A testing utility to allow control over {@link org.slf4j.impl.SimpleLogger}. In some cases,
* reflection is used to bypass access restrictions.
*/
final class RecordingSimpleLogger implements TestRule {
private String expectedMessages = "";
/**
* Resets {@link org.slf4j.impl.SimpleLogger} to the new log level.
*/
RecordingSimpleLogger logLevel(String logLevel) throws Exception {
System.setProperty(SHOW_THREAD_NAME_KEY, "false");
System.setProperty(DEFAULT_LOG_LEVEL_KEY, logLevel);
Field field = SimpleLogger.class.getDeclaredField("INITIALIZED");
field.setAccessible(true);
field.set(null, false);
Method method = SimpleLoggerFactory.class.getDeclaredMethod("reset");
method.setAccessible(true);
method.invoke(LoggerFactory.getILoggerFactory());
return this;
}
/**
* Newline delimited output that would be sent to stderr.
*/
RecordingSimpleLogger expectMessages(String expectedMessages) {
this.expectedMessages = expectedMessages;
return this;
}
/**
* Steals the output of stderr as that's where the log events go.
*/
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
ByteArrayOutputStream buff = new ByteArrayOutputStream();
PrintStream stderr = System.err;
try {
System.setErr(new PrintStream(buff));
base.evaluate();
assertEquals(expectedMessages, buff.toString());
} finally {
System.setErr(stderr);
}
}
};
}
}