Rest-assured 写日志到 log4j

Posted jwentest

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
}

 

以上是关于Rest-assured 写日志到 log4j的主要内容,如果未能解决你的问题,请参考以下文章

rest-assured的日志使用介绍

LOG4J 的日志写到了哪里

如何让log4j日志只输出错误信息

Java 标准日志工具 Log4j 的使用(附源代码)

log4j日志

对log4j产生的日志文件进行监控