背景:

采用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的更多相关文章

  1. commons-logging和Log4j 日志管理/log4j.properties配置详解

    commons-logging和Log4j 日志管理 (zz) 什么要用日志(Log)? 这个……就不必说了吧. 为什么不用System.out.println()? 功能太弱:不易于控制.如果暂时不 ...

  2. java日志框架log4j详细配置及与slf4j联合使用教程

    最后更新于2017年02月09日 一.log4j基本用法 首先,配置log4j的jar,maven工程配置以下依赖,非maven工程从maven仓库下载jar添加到“build path” <d ...

  3. [转载]java日志框架log4j详细配置及与slf4j联合使用教程

    一.log4j基本用法 首先,配置log4j的jar,maven工程配置以下依赖,非maven工程从maven仓库下载jar添加到“build path” 1 2 3 4 5 <dependen ...

  4. java日志框架log4j详细配置及与slf4j使用教程

    一.log4j基本用法 首先,配置log4j的jar,maven工程配置以下依赖,非maven工程从maven仓库下载jar添加到“build path” 1 2 3 4 5 <dependen ...

  5. Flume学习应用:Java写日志数据到MongoDB

    概述 Windows平台:Java写日志到Flume,Flume最终把日志写到MongoDB. 系统环境 操作系统:win7 64 JDK:1.6.0_43 资源下载 Maven:3.3.3下载.安装 ...

  6. 日志管理-log4j与slf4j的使用

    一.概述 1.log4j: Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件,甚至是套接口服务器.NT的事件记录器.UNIX Sy ...

  7. SSM框架中添加写日志功能

    前提:要导入log4j的jar包 在web.xml中输入: <!--日志加载--> <context-param> <param-name>log4jConfigL ...

  8. Java Web项目实现写日志功能

    第一步:导入log4j-1.2.16的jar包 第二步:在servlet包里编写写日志的servlet,代码如下: public class InitServlet extends HttpServl ...

  9. 为何要打印日志?C++在高并发下如何写日志文件(附源码)?

    为何要打印日志?让程序裸奔不是一件很快乐的事么? 有些BUG就像薛定谔的猫,具有波粒二象性,当你试图去观察它时它就消失了,当你不去观察它时,它又会出现.当你在测试人员面前赌咒发誓,亲自路演把程序跑一遍 ...

随机推荐

  1. Oracle VM VirtualBox安裝Windows 2000失败

    问题: VirtualBox下安装Windows2000,设置网络后进入最后一步,复制组件……然后就是重启:再试还是重启!   解决: 在Oracle网站上查了一下资料:http://www.virt ...

  2. 图片采集器_PHP

    现在国内模仿“pinterest”的越来越多了,之前我做过一个基于chrome浏览器上的一个“图片采集工具”,类似于“花瓣网“那样的,初期我觉得挺简单,后来做起来发现还是挺复杂的,特别是整合到你自己的 ...

  3. django-连接mysql失败

    如果你的数据库连接报错,缺少组件,可以安装以下的包 安装依赖(如果使用虚拟环境,需要在虚拟环境下安装) 安装mysql开发依赖包   sudo apt-get install libmysqlclie ...

  4. ubantu环境下fidder安装

    转自:http://www.cnblogs.com/jcli/p/4474332.html Linux(Ubuntu)环境下使用Fiddler 自己的开发环境是Ubuntu, 对于很多优秀的软件但是又 ...

  5. epoll用法【整理】

    l  epoll是什么? epoll是当前在Linux下开发大规模并发网络程序的热门人选,epoll 在Linux2.6内核中正式引入,和select相似,都是I/O多路复用(IO multiplex ...

  6. Cannot find class [org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer]

    解决方案:添加spring-webmvc好多人都不知道org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer这个类到底 ...

  7. 【独家】阿里天池IJCAI17大赛第四名方案全解析(附代码)

    [独家]阿里天池IJCAI17大赛第四名方案全解析(附代码) https://mp.weixin.qq.com/s?__biz=MzAxMzA2MDYxMw==&mid=2651560625& ...

  8. 搭建https+nginx服务器

    搭建https+nginx的服务器,主要是安装ngnix和使用openssl生成自签证书,并在nginx中配置的过程   一.安装环境 1.安装opnssl(ssl支持) https://www.op ...

  9. nginx反向代理同一主机多个网站域名

    nginx反向代理同一ip多个域名,给header加上host就可以了 proxy_set_header   Host             $host; nginx.conf例子 upstream ...

  10. unity3d 移动与旋转 2

    这次的代码示例是配合动画系统使用的 4.3新的动画系统允许动画带有位置偏移,只需要在Animator组件中勾选Apply Root Motion我们就可以使用它了. using UnityEngine ...