example of Appendable that logs stdout and stderr

39 views
Skip to first unread message

Todd Bradley

unread,
Jan 25, 2018, 12:38:26 PM1/25/18
to Yet another Expect for Java
Hi, does anyone have an example of an Appendable that I could use to help my debug logging? The problem I often run into is that my application (a test framework) has multiple Expect instances running in separate threads, and when I have them echo their stdout and stderr, I can't figure out which output came from which thread. So I'd like to prepend that output with the thread name or some other string to help troubleshooting.

I would use this Appendable with this, on my ExpectBuilder:
withEchoOutput(Appendable echoOutput)

For example, instead of this...

Last login: Thu Jan 25 01:51:11 2018 from brmopc-x5-2l-7.us.oracle.com 
Last login: Thu Jan 25 01:51:11 2018 from brmopc-x5-2l-7.us.oracle.com 
?[32mUsing /home/paasusr/.rvm/gems/ruby-2.2.6?[0m 
?[32mUsing /home/paasusr/.rvm/gems/ruby-2.2.6?[0m

I'd like this...

[thread1-stdout] Last login: Thu Jan 25 01:51:11 2018 from brmopc-x5-2l-7.us.oracle.com 
[thread2-stdout] Last login: Thu Jan 25 01:51:11 2018 from brmopc-x5-2l-7.us.oracle.com 
[thread2-stdout] ?[32mUsing /home/paasusr/.rvm/gems/ruby-2.2.6?[0m 
[thread1-stdout] ?[32mUsing /home/paasusr/.rvm/gems/ruby-2.2.6?[0m


Any advice?


Thanks,
Todd.

Todd Bradley

unread,
Jan 29, 2018, 1:56:08 PM1/29/18
to Yet another Expect for Java
Does anyone else use this feature?

--
You received this message because you are subscribed to the Google Groups "Yet another Expect for Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-expectit+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexey Gavrilov

unread,
Jan 29, 2018, 2:23:30 PM1/29/18
to Todd Bradley, Yet another Expect for Java
Hi,

It can be as simple as this:

Expect expect = new ExpectBuilder()
        .withOutput(channel.getOutputStream())
        .withInputs(channel.getInputStream(), channel.getExtInputStream())
        .withEchoInput(new AppendableAdapter() {
            @Override
            public Appendable append(final CharSequence csq) throws IOException {
                System.out.println(Thread.currentThread().getName() + ": " + csq);
                return this;
            }
        })
        .withEchoOutput(System.err)
        .withInputFilters(removeColors(), removeNonPrintable())
        .withExceptionOnFailure()
        .build();


Or you can use a Logging framework (standard JUL or SLF4J-Logback) where you can customise output records in many different ways

Regards,
Alexey

To unsubscribe from this group and stop receiving emails from it, send an email to java-expecti...@googlegroups.com.

Todd Bradley

unread,
Jan 31, 2018, 4:58:46 PM1/31/18
to Alexey Gavrilov, Yet another Expect for Java
Thanks. Sadly, AppendableAdapter is package private, so it can't be used in a standalone test. However, your suggestion sent me down the right path toward making my own Appendable.


Thank you,
Todd.
Reply all
Reply to author
Forward
0 new messages