8

我正在研究一种方法,试图将默认的放心日志(进入控制台)更改为使用 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 管理的可打印的东西。

谁能帮我?

4

4 回答 4

3

我刚刚解决了将其写入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;
}

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

于 2013-01-25T15:26:57.577 回答
3

也可能有用:这里有一个类将 restAssured log() 调用重定向到提供的记录器:

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import org.slf4j.Logger;

/**
 * A wrapper class which takes a logger as constructor argument and offers a PrintStream whose flush
 * method writes the written content to the supplied logger (debug level).
 * <p>
 * Usage:<br> 
 * initializing in @BeforeClass of the unit test:
 * <pre>
 *          ToLoggerPrintStream loggerPrintStream = new ToLoggerPrintStream( myLog );
 *          RestAssured.config = RestAssured.config().logConfig(
 *                                 new LogConfig( loggerPrintStream.getPrintStream(), true ) );
 * </pre>
 * will redirect all log outputs of a ValidatableResponse to the supplied logger:
 * <pre>
 *             resp.then().log().all( true );
 * </pre>
 *
 * @version 1.0 (28.10.2015)
 * @author  Heri Bender
 */
public class ToLoggerPrintStream
{
    /** Logger for this class */
    private Logger myLog;
    private PrintStream myPrintStream;

/**
 * @return printStream
 */
public PrintStream getPrintStream()
{
    if ( myPrintStream == null )
    {
        OutputStream output = new OutputStream()
        {
            private StringBuilder myStringBuilder = new StringBuilder();

            @Override
            public void write(int b) throws IOException 
            {
                this.myStringBuilder.append((char) b );
            }

            /**
             * @see java.io.OutputStream#flush()
             */
            @Override
            public void flush()
            {
                myLog.debug( this.myStringBuilder.toString() );
                myStringBuilder = new StringBuilder();
            }
        };

        myPrintStream = new PrintStream( output, true );  // true: autoflush must be set!
    }

    return myPrintStream;
}

/**
 * Constructor
 *
 * @param aLogger
 */
public ToLoggerPrintStream( Logger aLogger )
{
    super();
    myLog = aLogger;
}
于 2015-10-28T10:24:57.750 回答
1
PrintStream fileOutPutStream = new PrintStream(new File("somefile.txt"));
RestAssured.config = config().logConfig(new LogConfig().defaultStream(fileOutPutStream)); 

使用 printStream 指向文件并给出要打印日志的文件名。将上面的代码放在您的设置方法中进行测试,然后只需在您的 RequestSpecification 实例上调用日志,如下所示

requestSpecification.log().all();
于 2017-05-10T06:27:16.427 回答
0

对 Heri 的答案的一种可能的修改 - 可以使用 ByteArrayOutputStream 而不是 StringBuilder - 如果一个人处理多语言数据,例如

// [...]
PrintStream getPrintStream() {
    if (printStream == null) {
        OutputStream output = new OutputStream() {
            ByteArrayOutputStream baos = new ByteArrayOutputStream()

            @Override
            public void write(int b) throws IOException {
                baos.write(b)
            }

            @Override
            public void flush() {
                logger.debug(this.baos.toString())
                baos = new ByteArrayOutputStream()
            }
        }
        printStream = new PrintStream(output, true)  // true: autoflush must be set!
    }
    return printStream
}
// [...]
于 2017-08-22T19:51:59.163 回答