How to get a log with confidence that can be printed in a text file

I am working on a method trying to change the default break log (which goes to the console) to a file using log4j.

This is a JUnit project whose method finally calls a REST facade that has 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 changed the method as follows:

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

      

But it writer.toString()

always prints a void string (the old implementation works fine). Maybe I'm doing something wrong, but what? :(

I need to get something printable that can be manipulated by log4j in one way or another.

Can anyone help me?

+3


source to share


4 answers


I just solved the problem by writing this to the method RestSuite.setUp()

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

      

and keeping the old code.



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

      

I hope this can help someone in the future.

+3


source


It might also be useful: Here's a class that redirects restAssured log () calls to the 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;
}

      

+1


source


PrintStream fileOutPutStream = new PrintStream(new File("somefile.txt"));
RestAssured.config = config().logConfig(new LogConfig().defaultStream(fileOutPutStream)); 

      

Use printStream to point to a file and specify the name of the file you want to print. Place the above code in your setup method for tests and then just call the log on your RequestSpecification instance as shown below.

requestSpecification.log().all();

      

0


source


One possible modification to Heri's answer - a ByteArrayOutputStream can be used instead of a StringBuilder - this helps when it comes to multilingual data, for example.

// [...]
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
}
// [...]

      

0


source







All Articles