zoukankan      html  css  js  c++  java
  • RestAssured打印日志到文件中的方法

    参考https://stackoverflow.com/questions/14476112/how-to-get-rest-assured-log-into-something-printable-in-a-text-file

    结合Heriaskucins的回答,兼容中文乱码问题的printstream

    package tpf.common;
    
    import org.apache.log4j.Logger;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintStream;
    
    /**
     * 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;
    //}
        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);
                    }
    
                    @Override
                    public void flush() {
                        myLog.debug(this.baos.toString());
                        baos = new ByteArrayOutputStream();
                    }
                };
                myPrintStream = new PrintStream(output, true);  // true: autoflush must be set!
            }
            return myPrintStream;
        }
    
        /**
         * Constructor
         *
         * @param aLogger
         */
        public ToLoggerPrintStream(Logger aLogger) {
            super();
            myLog = aLogger;
        }
    }
  • 相关阅读:
    学习进度条64
    学习进度条63
    学习进度条62
    学习进度条61
    学习进度条60
    学习进度条59
    学习进度条58
    学习进度条57
    学习进度条56
    学习进度条55
  • 原文地址:https://www.cnblogs.com/zipon/p/7582832.html
Copyright © 2011-2022 走看看