Hi all, testing logging looks like a strange requirement to me :
shouldn't logging be a separated concern from production code ?
Now I must admit to have tested some logging, and testing log4j with
static logger instance is a pain :)
I followed same path as Mark, as I failed to find testing artifact for
this purpose :(
1- have a spy
S1- have a concrete spy appender, S2- have a testing log4j
configuration resource, S3- have context use this log file and S4-
assertThat captured output is as specified
This is "cumbersome", isn't it ?
straightforward S1 was always sufficient for me, something like
public class Log4jCapturingAppender extends AppenderSkeleton {
private Object capture; // + have get to implement spy
@Override
protected void append(LoggingEvent event) {
setCapture(event.getMessage());
}
// + empty required method
}
why was it sufficient ?
may be because you can just execute just enough production code to
produce the logging call you want to spy ...
i found difficult to have an appender defined in a spring xml
resource, and have log4j take advantage of it, has both container are
unrelated
so i followed this "cumbersome" path, and yes it is a pain
Now, may be groovy could help, as it has some bytecode manipulation
capability, but I did not tried yet
I have a groovy task in my private backlog, but this one looks easy to
try, so I shall give it a first go :)
One last thing, isn't the instruction following your FATAL statement
a ... System.exit ??
On Dec 9, 3:51 am, "Mark Derricutt" <
m...@talios.com> wrote:
> I've not tried this, but I've tried a similar approach:
>
> - Write your own in-memory appender that gives you an ability to check the
>
> {