How to get rest-assured log into something printable in a text file

后端 未结 4 2259
攒了一身酷
攒了一身酷 2021-01-12 02:52

I\'m working into a method trying to change the default rest-assured log (which goes to the console) to a file using log4j.

It\'s a JUnit project which methods final

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-12 03:38

    One possible modification to Heri's answer - instead of StringBuilder one can use ByteArrayOutputStream - that helps if one deals with multi-language data, e.g.

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

提交回复
热议问题