我正在研究一种方法,试图将默认的放心日志(进入控制台)更改为使用 log4j 的文件。
这是一个 JUnit 项目,其方法最终调用了一个 REST 外观,它具有类似这样的方法。
private ResponseSpecification responseSpecification(RequestSpecification requestSpecification, Matcher matcher, int statusCode) {
ResponseSpecification responseSpecification = requestSpecification.expect().statusCode(StatusCode).body(".", is(matcher));
if (log) {
responseSpecification = responseSpecification.log().all();
}
return responseSpecification;
}
按照官方文档,我改变了这样的方法:
private ResponseSpecification responseSpecification(RequestSpecification requestSpecification, Matcher matcher, int statusCode) {
final StringWriter writer = new StringWriter();
final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
ResponseSpecification responseSpecification = requestSpecification.filter(logResponseTo(captor)).expect().statusCode(statusCode).body(".", is(matcher));
System.out.println("writer = " + writer.toString() + " <-");
return responseSpecification;
}
但writer.toString()
总是打印一个空字符串(旧的实现工作正常)。也许我做错了什么,但是什么?:(
我需要以这种或其他方式获得可以由 log4j 管理的可打印的东西。
谁能帮我?