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的更多相关文章
- commons-logging和Log4j 日志管理/log4j.properties配置详解
commons-logging和Log4j 日志管理 (zz) 什么要用日志(Log)? 这个……就不必说了吧. 为什么不用System.out.println()? 功能太弱:不易于控制.如果暂时不 ...
- java日志框架log4j详细配置及与slf4j联合使用教程
最后更新于2017年02月09日 一.log4j基本用法 首先,配置log4j的jar,maven工程配置以下依赖,非maven工程从maven仓库下载jar添加到“build path” <d ...
- [转载]java日志框架log4j详细配置及与slf4j联合使用教程
一.log4j基本用法 首先,配置log4j的jar,maven工程配置以下依赖,非maven工程从maven仓库下载jar添加到“build path” 1 2 3 4 5 <dependen ...
- java日志框架log4j详细配置及与slf4j使用教程
一.log4j基本用法 首先,配置log4j的jar,maven工程配置以下依赖,非maven工程从maven仓库下载jar添加到“build path” 1 2 3 4 5 <dependen ...
- Flume学习应用:Java写日志数据到MongoDB
概述 Windows平台:Java写日志到Flume,Flume最终把日志写到MongoDB. 系统环境 操作系统:win7 64 JDK:1.6.0_43 资源下载 Maven:3.3.3下载.安装 ...
- 日志管理-log4j与slf4j的使用
一.概述 1.log4j: Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件,甚至是套接口服务器.NT的事件记录器.UNIX Sy ...
- SSM框架中添加写日志功能
前提:要导入log4j的jar包 在web.xml中输入: <!--日志加载--> <context-param> <param-name>log4jConfigL ...
- Java Web项目实现写日志功能
第一步:导入log4j-1.2.16的jar包 第二步:在servlet包里编写写日志的servlet,代码如下: public class InitServlet extends HttpServl ...
- 为何要打印日志?C++在高并发下如何写日志文件(附源码)?
为何要打印日志?让程序裸奔不是一件很快乐的事么? 有些BUG就像薛定谔的猫,具有波粒二象性,当你试图去观察它时它就消失了,当你不去观察它时,它又会出现.当你在测试人员面前赌咒发誓,亲自路演把程序跑一遍 ...
随机推荐
- rpc简易实现-zookeeper
一.RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的存在,如TCP或UDP, ...
- 根据插件Swipe,结合jQuery封装成的新的jQuery插件
swipe支持电脑上的自动滑动,也支持手机端的滑动效果.但是每次调用只能支持一个效果或者说一个页面出现n个这样的效果,我们就得调用n次这个插件. 我使用swipe+jQuery使得swip变得方便使用 ...
- iis应用程序池假死问题
“Comprehensive orientate 16:05:43 查看原文 IIS貌似问题不少 问:IIS 网站 并发连接线不多,但是运行一段时间后 就非常慢,系统资源占用都正常,一回收应用 ...
- AWS安装
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" sudo python get-pip. ...
- 将com口2号引脚、3号引脚引出来,两个引脚对接会出发中断
思路:让程序一直不停的发数据,再设一个及接收事件,接通(短路)后触发. 灵感文章: 方案一: 去AQ解决话务台问题,下了车就牛不停蹄的去买了串口挡板,db9 female接口,根据2-3.3-2.5 ...
- python的接口
写法一: class Payment: def pay(self, money): raise NotImplementedError class Alipay(Payment): def pay(s ...
- sql日期查询
select getdate() ,getdate()) ,getdate()) ) ,getdate())) ,getdate())) Select datename(weekday, getdat ...
- CentOS上安装 jdk
先下载最新的jdk版本 文件名:jdk-8u5-linux-x64.rpm 将文件通过winscp上传到/usr/local目录中 rpm -ivh jdk-8u5-linux-x64.rpm 系统会 ...
- k8s实战
wget https://github.com/coreos/etcd/releases/download/v2.2.0/etcd-v2.2.0-linux-amd64.tar.gz etcd -na ...
- 2015年传智播客JavaEE 第168期就业班视频教程day45-ERP项目-0107-其他子系统
一套ERP系统中一定会有CRM,不可能说我所有数据都是散着放的,你想用就随便写一个.你出去和人聊,一定得说我这里有什么有什么,然后你就可以和人说你做的是进销存.人家要问CRM或者说财务系统你就说那不是 ...