In case you are not doing it already, using asynchronous logging is generally a good idea. You don’t want your application to slow down if the server IO is a little behind flushing all that logging to the filesystem. By making it asynchronous your application can continue running without having to wait for the log lines to be written to their final destination.
My personal choice for Java logging is log4j, there are a lot of different frameworks (including Suns own logging API), but log4j works great and is extremely flexible.
Log4j provides a built-in appender that provides asynchronous event logging, this appender wraps around the appender you actually want to use (file, console, event log, etc.) and provides the required de-coupling.
To use it you need to configure log4j programatically or using XML, you cannot configure the asynchronous appender using a properties file. A very simple XML configuration file to use asynchronous logging writing the events to the console would be:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>
<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="500"/>
<appender-ref ref="stdout"/>
</appender>
<root>
<priority value="error"></priority>
<appender-ref ref="ASYNC"/>
</root>
</log4j:configuration>
The root appender references the AsyncAppender, and the AsyncAppender references the ConsoleAppender in turn. If you want to define your own logger you just have to point it to the ASYNC appender:
<logger name="com.spartanjava" additivity="false">
<level value="info" />
<appender-ref ref="ASYNC" />
</logger>
A few tips on using AsyncAppender and log4j in general:
Pingback: Log4j를 좀더 빠르게 써보자!! « Beyondj2ee's Blog
Pingback: JavaPins