如何确保在文本文件中可打印的内容

时间:2013-01-23 09:18:58

标签: log4j rest-assured

我正在尝试使用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()总是打印一个void字符串(旧的实现工作正常)。也许我做错了什么,但是什么? :(

我需要获得可以通过log4j以这种或其他方式管理的可打印内容。

任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分: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;
}

我希望它可以帮助将来的某个人。

答案 1 :(得分:2)

也可能有用:这里有一个重定向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;
}

答案 2 :(得分:0)

PrintStream fileOutPutStream = new PrintStream(new File("somefile.txt"));
RestAssured.config = config().logConfig(new LogConfig().defaultStream(fileOutPutStream)); 

使用printStream指向文件&amp;给出要打印日志的文件名。 将上面的代码放在测试的安装方法中。然后只需在RequestSpecification实例上调用log,如下所示

requestSpecification.log().all();

答案 3 :(得分:0)

对Heri的答案的一个可能的修改 - 而不是StringBuilder可以使用ByteArrayOutputStream - 这有助于处理多语言数据,例如。

2.jpeg