gpt4 book ai didi

log4j - 如何让放心登录到可打印在文本文件中的内容

转载 作者:行者123 更新时间:2023-12-04 12:10:09 24 4
gpt4 key购买 nike

我正在研究一种方法,尝试使用 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;
}

关注 official doc ,我已经改变了这样的方法:
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 管理的可打印内容。

谁能帮我?

最佳答案

我刚刚解决了将其写入 RestSuite.setUp() 的问题方法

RestAssured.config = config().logConfig(new LogConfig(defaultPrintStream));

并保持完整的旧代码。
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;
}

我希望它可以在 future 对某人有所帮助。

关于log4j - 如何让放心登录到可打印在文本文件中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14476112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com