Java Reference
In-Depth Information
log.warn("warning message");
log.error("error message");
log.fatal("fatal message");
}
public static void recalculate(double fundAmount, double rate) {
log.entry();
fundAmount = fundAmount * (1 + rate);
log.info("fundAmount = " + fundAmount);
log.exit();
}
}
10. Run your program. Your output should look like this:
David Johnson will have $41.666666666666664 per month for retirement.
13:39:10.511 [main] FATAL chapter6.ApacheLogging - monthlyPension is too low
13:39:10.514 [main] ERROR chapter6.ApacheLogging - error message
13:39:10.514 [main] FATAL chapter6.ApacheLogging - fatal message
11. You only see these log messages because the default log level is set to Error . You can of course
adjust this, and many other settings. In Log4j, this is done using an .xml configuration file.
12. Create your configuration file by opening a new text file and copying the following .xml code
into it:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
13. This code essentially sets the configuration file according to the default configuration. Save the file
as :Log4j2.xml and place it in the src folder for your project. Here you can change the root level
and output destination.
14. First, change <Root level="error"> to <Root level="trace"> . Run your program and you'll
see all the error messages output to the console.
14:18:30.719 [main] TRACE chapter6.ApacheLogging - entry
14:18:30.720 [main] INFO chapter6.ApacheLogging - fundAmount = 11000.0
14:18:30.720 [main] TRACE chapter6.ApacheLogging - exit
14:18:30.720 [main] TRACE chapter6.ApacheLogging - entry
14:18:30.720 [main] INFO chapter6.ApacheLogging - fundAmount = 11000.0
14:18:30.720 [main] TRACE chapter6.ApacheLogging - exit
14:18:30.720 [main] TRACE chapter6.ApacheLogging - entry
Search WWH ::




Custom Search