I just started looking into Log4j 2.0. Looking at the user guide, it looks like some new things have been implemented that I'm currently a bit confused as to when I should be using. The primary example is Flow Logging.

public class LogTest {

    public static void main(final String[] args) {
        logger.entry();
        doTest("value");
        try {
            doTest(null);
        }
        catch (final Exception e) {
            logger.catching(e);
        }
        logger.exit();
    }

    private static void doTest(final String value) {
        logger.entry();
        if (value == null) {
            final Exception e = new IllegalArgumentException(
                "value must not be null");
            throw logger.throwing(e);
        }
        System.out.println(value);
        logger.exit();
    }
}

My question is, am I using the logger.throwing() and logger.catching() in the intended way?

有帮助吗?

解决方案

Yes that looks correct. Note that if you use entry() and exit() in many places, the resulting messages are hard to tell apart unless you enable location information in your pattern layout (with %location etc). This has significant performance impact though, so beware.

As for the catching() and throwing() methods, it may be a matter of taste whether you prefer these methods to logger.error(throwable);.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top