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就像薛定谔的猫,具有波粒二象性,当你试图去观察它时它就消失了,当你不去观察它时,它又会出现.当你在测试人员面前赌咒发誓,亲自路演把程序跑一遍 ...
随机推荐
- python渗透测试工具包
网络 Scapy, Scapy3k: 发送,嗅探,分析和伪造网络数据包.可用作交互式包处理程序或单独作为一个库.pypcap, Pcapy, pylibpcap: 几个不同 libpcap 捆绑的py ...
- LLMNR欺骗工具Responder
LLMNR(Link-Local Multicast Name Resolution,链路本地多播名称解析)协议是一种基于DNS包格式的协议.它可以将主机名解析为IPv4和IPv6的IP地址.这样用户 ...
- 030:Cetus中间件和MHA读写分离
030:Cetus中间件和MHA读写分离 line:V1.1 mail: gczheng@139.com date: 2018-08-30 一.主机环境 虚拟机配置 CPU 内存 硬盘 OS版本 My ...
- kernel_task high cpu usage
# Find the model $ system_profiler -detailLevel mini | grep "Model Identifier:" Model Iden ...
- Python写一个目录检索器
前言: 昨天看了Demon哥发的干货,有了次篇博文 干货链接: https://www.soffensive.com/2018/06/exploiting-blind-file-reads-path. ...
- spring data jpa 的各种查询总结
参考哦:https://blog.csdn.net/weixin_36667844/article/details/79945156
- JDK常用命令
转自:https://www.cnblogs.com/saiQsai/p/10353044.html 1.jps 查看java进程,得到进程ID:7854 作用等同于:ps -ef | grep ja ...
- C++builder XML XSL 代码生成
void __fastcall TFrmGenCode::XSLTxml1Click(TObject *Sender) { // XSLT转换xml文件格式 _di_IXMLDocument xml; ...
- 利用TortoiseGit从Github上下载代码
1.首先确保安装好了Git和TortoiseGit并在Github上有存放资源 2.将git上博客源文件克隆到本地,在本地创建好要存放资源的文件夹,之后在此文件内右键单击,可以看到下拉菜单中增加了To ...
- Linux Makefile 教程(转)
原文地址:http://blog.csdn.net/liang13664759/article/details/1771246 ------------------------------------ ...