Java Reference
In-Depth Information
17. The full configuration file should resemble this:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}
- %msg%n"/>
</Console>
<File name="ApacheLog-Info" filename=
"c:/users/n12063/ApacheLogging_Info.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1}
- %msg%n</pattern>
</PatternLayout>
</File>
<File name="ApacheLog-Warn" filename="src/ApacheLogging_Warn.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1}
- %msg%n</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" level="fatal"/>
<AppenderRef ref="ApacheLog-Info" level="info"/>
<AppenderRef ref="ApacheLog-Warn" level="warn"/>
</Root>
</Loggers>
</Configuration>
18. Now, you can run your program again. This time you should get a console output like this:
David Johnson will have $41.666666666666664 per month for retirement.
15:59:15.846 [main] FATAL chapter6.ApacheLogging - monthlyPension is too low
15:59:15.847 [main] FATAL chapter6.ApacheLogging - fatal message
19. Your log files should be stored in the designated folders on your computer. If you cannot locate
them, try searching for the names ApacheLogging_Info and ApacheLogging_Warn using your
computer's search function. You'll notice that files created in the src folder don't automatically
show up in Eclipse. If you place the file there in Eclipse yourself, your program will write to it dur-
ing execution and you'll be able to view it from Eclipse.
How It Works
Here's how it works:
1.
By attaching and importing the Log4j 2 API, you're allowing your program to use these resources.
2.
Very similarly to the example using the builtā€in logger, you created a single static logger object for
use in this class.
Search WWH ::




Custom Search