参考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;
}
}

RestAssured打印日志到文件中的方法的更多相关文章

  1. 如何优雅地将printf的打印保存在文件中?

    我们都知道,一般使用printf的打印都会直接打印在终端,如果想要保存在文件里呢?我想你可能想到的是重定向.例如: $ program > result.txt 这样printf的输出就存储在r ...

  2. JNI系列——C文件中的方法调用Java中方法

    1.创建xxx.jni包并在该包下实现一些Java的方法,和要调用的本地方法 2.实现MainActivity中的按钮点击事件-即点击按钮调用本地的方法 3.在C文件中的方法中回调Java的方法 3. ...

  3. python提取文件中的方法名称

    #提取文件中的方法名称 # -*- coding:utf-8 -*- def Query_Method(filepath): file = open(filepath,'r',encoding= 'U ...

  4. vim 单文件中查找方法

    1.vim 单文件中查找方法 正常模式下使用 / 或 ? 命令执行向后搜索或向前搜索 /love   从光标位置向前搜索关键词 love ?love   从光标位置向后搜索关键词 love 正常模式下 ...

  5. 将数组打印到txt文件中

    用print_r 将数组打印到txt文件中.     1.function save_log($content='', $file='app') { $logDir = './logs'; $now ...

  6. 《Java虚拟机原理图解》1.5、 class文件中的方法表集合--method方法在class文件中是怎样组织的

    0. 前言 了解JVM虚拟机原理是每一个Java程序员修炼的必经之路.但是由于JVM虚拟机中有很多的东西讲述的比较宽泛,在当前接触到的关于JVM虚拟机原理的教程或者博客中,绝大部分都是充斥的文字性的描 ...

  7. MySQL导出数据到文件中的方法

    MySQL导出数据到文件中的方法 1.导出数据到txt文件中实例:把数据表studscoreinfo中所有数据导出到指定的位置方法:select * from 表名 into outfile 指定导出 ...

  8. Linux下执行的java命令重定向到文件中的方法

    在Linux下通常会执行如:java -version 的命令, 但是,命令只是打印到了屏幕上不能重定向到文件中或标准输出流中. 此时需要将错误输出流重定向到标准输出流中就可以得到了. 比如:java ...

  9. IntelliJ IDEA中怎么查看文件中所有方法?

    可以使用快捷键ALT + 7打开左侧的Structure查看当前文件中的所有方法.   来自为知笔记(Wiz)

随机推荐

  1. kubernetes-核心概念及创建应用(六)

    kubernetes是什么: •Kubernetes是Google在2014年开源的一个容器集群管理系统,Kubernetes简称K8S.•K8S用于容器化应用程序的部署,扩展和管理.•K8S提供了容 ...

  2. CV做直方图的比较说明图形越相似性

    #include "opencv/cv.hpp" #include "opencv2/objdetect/objdetect.hpp" #include &qu ...

  3. Oracle11g 数据库的导入导出

    导出: 全部: exp imagesys/imagesys@orcl file=/icms/20170116.dmp full=y 用户: exp imagesys/imagesys @orcl fi ...

  4. 2d游戏中求出一个向量的两个垂直向量

    function cc.exports.VerticalVector(vec)--求出两个垂直向量 local result = {} result[1] = cc.p(vec.y/vec.x,-1) ...

  5. Oracle 数据库常用SQL语句(1)

    一.数据定义语句 CREATE:创建表或其它对象 create database test; //创建test数据库 ),sex )); //创建表 ALTER:修改表或其它对象的结构 )); //为 ...

  6. python 爬取知乎图片

    先上完整代码 import requests import time import datetime import os import json import uuid from pyquery im ...

  7. linux关于任务计划

    1.一次性任务计划:at 1)添加 在18:16时候重启服务器 at 18:16 >at init 6 >at ctrl+d2)查看 atq 1 Mon Aug 20 21:09:00 2 ...

  8. python-for循环与while循环

    while 循环 格式: while 条件 为 True: 代码块 while True: rayn_age = 18 age = input('请输入你的年龄:') age = int(age) i ...

  9. eclipse使用技巧的网站收集——转载(一)

    Eclipse工具使用技巧总结(转载) 首先推荐一篇非常好的How to use eclipse文章 ,讲的是eclipse使用的方方面面,非常实用,推荐给大家! 一.常用快捷键:Ctrl+F11 运 ...

  10. spring+struts2+mybatis框架依赖pom.xml

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...