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

问题描述 投票:2回答:1

如何打印有保证的日志和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));

    }
    }
log4j rest-assured
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.