• 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;
        }
    }
  • 相关阅读:
    机器学习笔记(四)---- 逻辑回归的多分类
    在modelarts上部署backend为TensorFlow的keras模型
    深度学习在其他领域的应用1:密码破解
    Reactive(2) 响应式流与制奶厂业务
    如何把图片变得炫酷多彩,Python教你这样实现!
    漫谈边缘计算(三):5G的好拍档
    机器学习笔记(三)---- 逻辑回归(二分类)
    华为云数据库携新品惊艳亮相2019华为全联接大会
    100 个网络基础知识普及,看完成半个网络高手
    最大流
  • 原文地址:https://www.cnblogs.com/jwentest/p/7680754.html
Copyright © 2020-2023  润新知