Missing last request/response log using a non-default PrintStream

391 views
Skip to first unread message

alejo...@nomasystems.com

unread,
Apr 19, 2013, 11:35:05 AM4/19/13
to rest-a...@googlegroups.com
Hi!

I'm using a PrintStream different from System.out to print the log to something handable by log4j.

Example:

Suite

private static StringWriter writer = new StringWriter();
    private static WriterOutputStream writerOutputStream =
            new WriterOutputStream(writer, Charset.defaultCharset(), 1024, true);
    private static PrintStream defaultPrintStream = new PrintStream(writerOutputStream, true);
    private static Logger primaryLogger; // To screen.
    private static Logger secondaryLogger; // To file.

@BeforeClass
    public static void init() {
        RestAssured.config = config().logConfig(logConfig().defaultStream(defaultPrintStream));
        ...
    }

public static String getRestAssuredLog() {
        writer.flush();
        String restAssuredLog = writer.toString();
        StringBuffer buffer = writer.getBuffer();
        buffer.replace(0, buffer.length(), "");
        return restAssuredLog;
    }

Facade

// Only a pair of examples.

private RequestSpecification requestSpecification(RequestData requestData) {
        RequestSpecification requestSpecification = given();
       
        Iterator<String> names = requestData.pathParamsIterator();
        while (names.hasNext()) {
            String name = names.next();
            requestSpecification = requestSpecification.pathParam(name, requestData.pathParamsGet(name));
        }
        names = requestData.queryParamsIterator();
        while (names.hasNext()) {
            String name = names.next();
            requestSpecification = requestSpecification.queryParam(name, requestData.queryParamsGet(name));
        }
        if (requestData.getHeaders() != null) {
            requestSpecification = requestSpecification.headers(requestData.getHeaders());           
        }
        if (requestData.getBody() != null) {
            requestSpecification = requestSpecification.contentType(ContentType.JSON).body(requestData.getBody().toString());
        }
        if (debug) {
            if (log) {
                requestSpecification = requestSpecification.log().all();
                String restAssuredLog = getRestAssuredLog();
                if (!restAssuredLog.equalsIgnoreCase("")) {
                    getSecondaryLogger().info(restAssuredLog);
                }
            }
        }
        return requestSpecification;
    }

...

private ResponseSpecification responseSpecification(RequestSpecification requestSpecification, int statusCode) {
        ResponseSpecification responseSpecification = requestSpecification.expect().statusCode(statusCode);
        if (log) {
            if (debug) {
                responseSpecification = responseSpecification.log().all();
            } else {
                responseSpecification = responseSpecification.log().ifError();
            }
            String restAssuredLog = getRestAssuredLog();
            if (!restAssuredLog.equalsIgnoreCase("")) {
                getSecondaryLogger().info(restAssuredLog);
            }
        }
        return responseSpecification;
    }

...


It works fine printing to System.out, but when I configure it to print to other PrintStream (which finally prints to the StringWriter), it prints all right except the last request/response. So, when a test fails and I need to see the output in the log file, I can't, because it is the last one and it is not printed.

After a lot of tests trying to discard several things, I'm sure it is not a log4j problem. And I have no more ideas to find it. So, can anyone help me? I'm not sure at all, but could even be a rest-assured bug?

Thanks in advance,

Alejo.

Johan Haleby

unread,
Apr 22, 2013, 2:26:27 AM4/22/13
to rest-a...@googlegroups.com
Hi, 

There are several tests in REST Assured for validating that logging to a printstream other than System.out works but perhaps there's something I've missed? In those cases I'm using "new PrintStream(new WriterOutputStream(writer), true);" where writer is a StringWriter. You could try the same thing in your app and see if you get the same problem or not.

Regards,
/Johan


--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages