Usually the application has log message that write the INFO or DEBUG to plain text. For example, JBoss combines with log4j to record your any log activities into server log. But how to write your own application log message just as you need? For example, your log looks like

2007-04-25 12:20 | transaction – 1009 start… | user=debbie | withdraw=650. Here is my receipt.

1. Create a log4j.xml at %JBoss%/server/default/conf

<?xml version=”1.0″ encoding=”utf-8″?>
<!DOCTYPE log4j:configuration SYSTEM “log4j.dtd”>
<log4j:configuration xmlns:log4j=”http://jakarta.apache.org/log4j/&#8221; debug=”false”>

<!– A size based file rolling appender –>
<appender name=”FILE”
class=”org.apache.log4j.RollingFileAppender”>
<errorHandler class=”org.apache.log4j.helpers.OnlyOnceErrorHandler” />
<param name=”File” value=”${jboss.server.home.dir}/log/server.log”/>
<param name=”Append” value=”true” />
<param name=”MaxFileSize” value=”20000KB” />
<param name=”MaxBackupIndex” value=”400″ />
<layout class=”org.apache.log4j.PatternLayout”>
<param name=”ConversionPattern” value=”%d | %-5r | %-5p | %c | %t:%x | %m%n”/>
</layout>
</appender>

<appender name=”CONSOLE” class=”org.apache.log4j.ConsoleAppender”>
<errorHandler class=”org.jboss.logging.util.OnlyOnceErrorHandler”/>
<param name=”Target” value=”System.out”/>
<param name=”Threshold” value=”DEBUG”/>
<layout class=”org.apache.log4j.PatternLayout”>
<param name=”ConversionPattern” value=”%d{ABSOLUTE} %-5p [%c{1}] %m%n”/>
</layout>
</appender>

<!– your own log message –>
<appender name=”APPLOG”
class=”org.apache.log4j.RollingFileAppender”>
<errorHandler class=”org.apache.log4j.helpers.OnlyOnceErrorHandler” />
<param name=”File” value=”${jboss.server.home.dir}/log/myapp/application.log”/>
<param name=”Append” value=”true” />
<param name=”MaxFileSize” value=”20000KB” />
<param name=”MaxBackupIndex” value=”400″ />
<layout class=”org.apache.log4j.PatternLayout”>
<param name=”ConversionPattern” value=”%d | %-5r | %t | %m%n”/>
</layout>
</appender>

<!– your looger –>
<logger name=”com.me.ApplicationLogger” additivity=”false”>
<priority value=”INFO” />
<appender-ref ref=”APPLOG” />
</logger>

<!– Setup the root logger –>
<root>
<priority value=”DEBUG” />
<appender-ref ref=”CONSOLE” />
<appender-ref ref=”FILE” />
</root>

</log4j:configuration>

NOTE: you have to create a new directory under %JBoss%/log/myapp.

2. Create a dummy logger

package com.me;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ApplicationLogger {
   public static Log getLogger(){
       return LogFactory.getLog(ApplicationLogger.class);
   }
}

3. Insert cut-point to write message into application.log

package com.me;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class WithdrawInterface {
   public void withdraw(String user, double amount) {
          Log appLogger = ApplicationLogger.getLogger();
          appLogger.info("withdraw: " + user + " ;" + amount);
   }
}

If yor application is development with Spring, then you can benefit the cut-point of AOP to insert your log output easily.

Ref:

Short introduction to log4j

Log4j Frequently Asked Questions

Simple Log

Advertisements