如何将可靠保证的日志和log4j日志和/或system.out.println语句打印到log4j文件或文本文件?

时间:2018-03-20 01:46:15

标签: log4j rest-assured

如何打印有保证的日志和log4j日志log.info和/或 system.out.println语句也适用于log4j文件?目前的代码 下面只打印出可靠的日志,但很难阅读 它属于哪个测试用例以及从哪个类开始。我希望 有人可以告诉我如何添加该信息并将其打印出来 我要添加的文本注释,以便将剩余的日志分成几部分 测试和课程。谢谢你的帮助。

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));

    }
    }

1 个答案:

答案 0 :(得分:0)

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