logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Matt Sicker (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (LOG4J2-786) Unit testing with log4j2
Date Tue, 19 Aug 2014 01:20:19 GMT

    [ https://issues.apache.org/jira/browse/LOG4J2-786?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14101665#comment-14101665
] 

Matt Sicker commented on LOG4J2-786:
------------------------------------

You could use any of the file appenders and set
{code}
fileName="${ctx:testName}"
{code}
in the config file. Then, as a JUnit test rule:

{code}
public class TestLogConfig implements TestRule {
  public Statement apply(final Statement stmt, final Description desc) {
    return new Statement() {
      public void evaluate() throws Throwable {
        ThreadContext.put("testName", desc.getClassName());
        try {
          stmt.evaluate();
        } finally {
          ThreadContext.remove("testName");
        }
      }
    }
  }
}
{code}

And then in your JUnit tests, you would include this field:

{code}
public class FooTest {
  @ClassRule
  public static TestRule log = new TestLogConfig();
  // ...
}
{code}

That would create a different log file for each test (or at least it should in theory). Requires
JUnit 4.9+, but that shouldn't be an issue, right?

> Unit testing with log4j2 
> -------------------------
>
>                 Key: LOG4J2-786
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-786
>             Project: Log4j 2
>          Issue Type: Improvement
>            Reporter: Alex Newman
>            Priority: Critical
>
> I still have no idea, how with a maven project with submodules, how to create a separate
file per test with the test output in it. I used to just output to the console and then use
maven.test.redirectTestOutput. This no longer works. 
> It was nice to be able to just turn this on and off with a simple property.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message