• rest-assured 将log()中的信息打印到log日志中去的方法


    rest-assured 将log()中的信息打印到log日志中去的方法:

     ============方法1==============

    PrintStream fileOutPutStream = new PrintStream(new File("log/test.log"));

    RestAssured.config
    = RestAssured.config().logConfig(new LogConfig(fileOutPutStream, true));

     ============方法2==============

    RestAssured.config = RestAssured.config().logConfig(new LogConfig(new ToLoggerPrintStream(logger).getPrintStream(), true));
    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;
    }
  • 相关阅读:
    夯实JavaScript基础之prototype, __proto__, instanceof
    入住博客园
    基础知识盲点——2
    Vuecli开发笔记三引入外部插件
    转让阿里云服务器(CPU2G+内存2G+带宽5M+硬盘150G)
    ubuntu 1
    ftp命令
    mysql数据库备份及恢复命令mysqldump,source的用法
    wordpress option的操作
    wordpress作者角色添加不了视频代码
  • 原文地址:https://www.cnblogs.com/zipon/p/6802842.html
Copyright © 2020-2023  润新知