Rest-assured 写日志到 log4j

背景:

采用Rest-assured,日志采用log4j,发现Rest-assured本身只支持打印日志到控制台,但期望打印到文件中以便排查问题

请求打印的语句只能输出到控制台

given().log().all()

(Rest-assured的官方文档:https://github.com/rest-assured/rest-assured

 

 解决方法:

1.翻阅资料,可以通过RestAssured.config来改变日志方面的配置,因此尝试从这里入手

RestAssured.config = RestAssured.config().logConfig(new LogConfig());

 

2.发现一种解决方法,PrintStream支持 字符串路径/File对象/outputstream,可以通过新建file来可以将日志输出到file中,但这种不能append,只能保存最新的一次记录,而且没有log4j格式

PrintStream ps = new PrintStream(new File("test.txt"));
RestAssured.config = config().logConfig(new LogConfig(ps));

 

3.继续google,发现了通过重写方法来解决该问题(http://stackoverflow.com/questions/14476112/how-to-get-rest-assured-log-into-something-printable-in-a-text-file),需要新建一个类来将logger转为outputstream

ToLoggerPrintStream loggerPrintStream = new ToLoggerPrintStream(logger);
RestAssured.config = RestAssured.config().logConfig(new LogConfig(loggerPrintStream.getPrintStream(), true));

ToLoggerPrintStream类源码:

 折叠原码
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
 
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.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:
 * <p>
 * <pre>
 * ToLoggerPrintStream loggerPrintStream = new ToLoggerPrintStream(myLog);
 * RestAssured.config = RestAssured.config().logConfig(new LogConfig(loggerPrintStream.getPrintStream(), true));
 * </pre>
 * <p>
 * will redirect all log outputs of a ValidatableResponse to the supplied
 * logger:
 * <p>
 * <pre>
 * resp.then().log().all(true);
 * </pre>
 *
 * @author Heri Bender
 * @version 1.0 (28.10.2015)
 */
public class ToLoggerPrintStream {
    /**
     * Logger for this class
     */
    private Logger myLog;
    private PrintStream myPrintStream;
 
    /**
     * @return printStream
     * @throws UnsupportedEncodingException
     */
    public PrintStream getPrintStream() {
        if (myPrintStream == null) {
            OutputStream output = new OutputStream() {
 
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
                @Override
                public void write(int b) throws IOException {
 
                    baos.write(b);
 
                }
 
                /**
                 * @see java.io.OutputStream#flush()
                 */
                @Override
                public void flush() {
 
                    String log = this.baos.toString().trim();
 
                    if (!StringUtils.isBlank(log)) {
                        myLog.info(log);
                        baos = new ByteArrayOutputStream();
                    }
                }
            };
            // true: autoflush
            // must be set!
            myPrintStream = new PrintStream(output, true);
 
        }
        return myPrintStream;
    }
 
    /**
     * Constructor
     *
     * @param logger
     */
    public ToLoggerPrintStream(Logger logger) {
        super();
        myLog = logger;
    }
}

 

posted @ 2017-10-17 11:30  whendream  阅读(700)  评论(0编辑  收藏  举报