2013-01-23 26 views
3

我正在尝试使用log4j将缺省的其余保证日志(转到控制台)更改为文件。如何获得放心的日志到文本文件中可打印的东西

这是一个JUnit项目,其方法最终调用REST外观,它有像这样的方法。

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

official doc,我已经改变了方法是这样的:

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()打印总是一个无效的字符串(旧实现正常工作)。也许我做错了什么,但是什么? :(

我需要得到的东西打印可以通过log4j的管理,在这个或其他方式。

谁能帮助我?

回答

3

我刚刚解决了写这篇进入RestSuite.setUp()问题方法

RestAssured.config = config().logConfig(new LogConfig(defaultPrintStream)); 

,并完整地保留旧代码。

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

我希望它能帮助未来的某个人。

1

可能是太有用:这里会被重定向restAssured日志()类调用所提供的记录:

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; 
} 
0
PrintStream fileOutPutStream = new PrintStream(new File("somefile.txt")); 
RestAssured.config = config().logConfig(new LogConfig().defaultStream(fileOutPutStream)); 

用一个PrintStream指向一个文件&给你想要的文件名打印日志。 把上面的代码在你的设置方法进行了检测&然后就打电话登录您的RequestSpecification实例如下

requestSpecification.log().all(); 
0

一个可能的修改和日的答案 - 而不是StringBuilder的一个可以使用ByteArrayOutputStream - 如果一个交易,帮助与多语言数据,例如

// [...] 
PrintStream getPrintStream() { 
    if (printStream == null) { 
     OutputStream output = new OutputStream() { 
      ByteArrayOutputStream baos = new ByteArrayOutputStream() 

      @Override 
      public void write(int b) throws IOException { 
       baos.write(b) 
      } 

      @Override 
      public void flush() { 
       logger.debug(this.baos.toString()) 
       baos = new ByteArrayOutputStream() 
      } 
     } 
     printStream = new PrintStream(output, true) // true: autoflush must be set! 
    } 
    return printStream 
} 
// [...] 
相关问题