Aaron Digulla created SLF4J-461:
-----------------------------------

             Summary: JUnit Support
                 Key: SLF4J-461
                 URL: https://jira.qos.ch/browse/SLF4J-461
             Project: SLF4J
          Issue Type: New Feature
          Components: Unspecified
    Affects Versions: 1.8.0-beta4, 1.7.25
            Reporter: Aaron Digulla
            Assignee: SLF4J developers list


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:
{code:java}
RedirectLogger rl = new RedirectLogger(Tool.class, Util.class);
try {
    rl.install();

    tool.foo();
} finally  {
    rl.deinstall();
}

assertEquals("Expected log lines", rl.dump(Level.WARN));
{code}
Example > Java 8 with method that returns value:
{code:java}
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));{code}
 



--
This message was sent by Atlassian JIRA
(v7.3.1#73012)
_______________________________________________
slf4j-dev mailing list
slf4j-dev@qos.ch
http://mailman.qos.ch/mailman/listinfo/slf4j-dev

Reply via email to