【问题标题】:How do I print rest-assured logs and log4j logs and/or system.out.println statements to a log4j file or text file?如何将可靠日志和 log4j 日志和/或 system.out.println 语句打印到 log4j 文件或文本文件?
【发布时间】:2018-08-28 17:32:41
【问题描述】:

如何打印放心日志和 log4j 日志 log.info 和/或 system.out.println 语句是否也在 log4j 文件中?当前代码 下面只打印出放心的日志,但很难阅读 它属于哪个测试用例以及来自哪个类。我希望 有人可以告诉我如何添加该信息并打印出来 我要添加的文本 cmets 将放心日志划分为 测试和上课。感谢您的帮助。

package com.students.loggingexamples;

import com.student.base.TestBase;
import static io.restassured.RestAssured.given;
import java.io.FileNotFoundException;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.junit.Test;


public class LoggingResponseValues extends TestBase{

private static Logger log =        

LogManager.getLogger(LoggingResponseValues.class.getName());


 /*
 * This test will print out the response body.
 */

@Test
public void test003() {

    log.info("---Printing Response Body---");
    given()
        .param("programme", "Computer Science")
        .param("limit", 1)
        .when()
        .get("/list")
        .then()
        .log()
        .body()
        .statusCode(200);

}


/*
 * This test will print out the response in case of an error.
 */

@Test
public void test004() throws FileNotFoundException {
    log.info("---Printing Response Body In Case of An Error---");

    given()
        .param("programme", "Computer Science")
        .param("limit", -1)
        .when()
        .get("/list")
        .then()
        .log()
        .ifError();

}

}

public class TestBase {

    @BeforeClass
    public static void init() throws FileNotFoundException {
    RestAssured.baseURI="http://localhost";
    RestAssured.port=8080;
    RestAssured.basePath="/student";

    //Prints out the rest-assured logs into file
    PrintStream fileOutPutStream = new PrintStream(new 

    File("C:\\EclipseProjects\\students-application\\logs\\main.log"));
    RestAssured.config = RestAssured.config().logConfig(new 
    LogConfig().defaultStream(fileOutPutStream));

    }
    }

【问题讨论】:

    标签: log4j rest-assured


    【解决方案1】:

    您可以使用 log4j-iostreams 库来构建 PrintOutputStream。将该 printStream 添加到 RestAssured logConfig。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 2010-09-10
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多