Details
-
New Feature
-
Resolution: Unresolved
-
Major
-
None
-
1.7.25, 1.8.0-beta4
Description
I would like to submit some code to help write unit tests that verify log behavior. Examples:
- Make sure that some code correctly logs errors and warnings
- The log messages contain the correct information (instead of, say, "[byte").
- Files are logged with absolute path
- Remove (expected) stack traces from test output
My code contains a class RedirectLogger which takes lists of classes or logger names and adds appenders to those which collect log events in a list.
At the end of the test, you can call a dump() method (optionally with a filter) to get everything that was logged.
Example code before Java 8:
RedirectLogger rl = new RedirectLogger(Tool.class, Util.class); try { rl.install(); tool.foo(); } finally { rl.deinstall(); } assertEquals("Expected log lines", rl.dump(Level.WARN));
Example > Java 8 with method that returns value:
RedirectLogger rl = new RedirectLogger(Tool.class, Util.class); Type result = rl.with(() -> { return tool.bar(); }); assertEquals(..., result); assertEquals("Expected log lines", rl.dump(Level.WARN));