如何确保输入日志到文本文件中可打印的内容 [英] How to get rest-assured log into something printable in a text file
问题描述
我正在研究一种方法,尝试使用log4j将默认的保证日志(转到控制台)更改为文件.
I'm working into a method trying to change the default rest-assured log (which goes to the console) to a file using log4j.
这是一个JUnit项目,方法最终调用一个REST门面,该门面具有类似这样的方法.
It's a JUnit project which methods finally call to a REST facade, which have methods like this one.
private ResponseSpecification responseSpecification(RequestSpecification requestSpecification, Matcher matcher, int statusCode) {
ResponseSpecification responseSpecification = requestSpecification.expect().statusCode(StatusCode).body(".", is(matcher));
if (log) {
responseSpecification = responseSpecification.log().all();
}
return responseSpecification;
}
Following the official doc, I've changed the method like this:
private ResponseSpecification responseSpecification(RequestSpecification requestSpecification, Matcher matcher, int statusCode) {
final StringWriter writer = new StringWriter();
final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
ResponseSpecification responseSpecification = requestSpecification.filter(logResponseTo(captor)).expect().statusCode(statusCode).body(".", is(matcher));
System.out.println("writer = " + writer.toString() + " <-");
return responseSpecification;
}
但是writer.toString()
总是打印一个空字符串(旧的实现很好用).也许我做错了什么,但是呢? :(
But writer.toString()
prints always a void string (the old implementation works fine). Maybe I'm doing something wrong, but what? :(
我需要获取可以通过log4j以这种或其他方式管理的可打印内容.
I need to get something printable which can be managed by log4j, in this or other way.
有人可以帮助我吗?
推荐答案
也可能有用:在这里,一个将restAssured log()调用重定向到提供的记录器的类:
Might be useful too: Here a class which redirects the restAssured log() calls to a supplied logger:
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;
}
这篇关于如何确保输入日志到文本文件中可打印的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!