8

log4jを使用して、デフォルトの安心ログ(コンソールに送られる)をファイルに変更しようとする方法に取り組んでいます。

これは、メソッドが最終的に REST ファサードを呼び出す JUnit プロジェクトであり、このようなメソッドがあります。

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

公式ドキュメントに従って、次のようにメソッドを変更しました。

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 で管理できる印刷可能なものを取得する必要があります。

誰でも私を助けることができますか?

4

4 に答える 4

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

将来誰かに役立つことを願っています。

于 2013-01-25T15:26:57.577 に答える
3

これも役に立つかもしれません: ここでは、restAssured log() 呼び出しを提供されたロガーにリダイレクトするクラス:

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;
}
于 2015-10-28T10:24:57.750 に答える