Log4j Configuration

From SiggersWiki

Note: These instructions have been tested to work correctly with JCAPS 5.1.2 and Log4j 1.2.14, on either an AIX or Windows platform. The latest version of log4j can be downloaded from here.

This example uses a sample project called prjTest, and a sample JCD called jcdTest.

First, download the log4j.jar file to your local system, and import the jar into the repository via the eDesigner by right-clicking on the relevant JARs folder, and selecting Import->File..., and selecting the log4j JAR file from the local disk, as shown in the screenshot below:

Image:Log4j_ed_screenshot_1.gif

Then, open up the JCD and import the JAR into the JCD, as shown in the screenshot below:

Image:Log4j_ed_screenshot_2.gif

After this is completed, add the following java constructor code to the JCD, along with the log variable declaration and the import of the log4j java files. This constructor code will be called after the deployment when the first message is received by the JCD.

package prjTest;

import org.apache.log4j.*;

public com.stc.codegen.logger.Logger logger;
public com.stc.codegen.alerter.Alerter alerter;
public com.stc.codegen.util.CollaborationContext collabContext;
public com.stc.codegen.util.TypeConverter typeConverter;
public org.apache.log4j.Logger log;

public jcdTest()
{
  try {
    log = org.apache.log4j.Logger.getLogger( this.getClass().getName() );
  } catch ( Exception e ) {
    throw new RuntimeException( "SEVERE ERROR: problem during initialization of " + this.getClass().getName() + "! " + e.getMessage(), e );
  }
  log.info( "INIT - " + this.getClass().getName() + " has been started." );
}

After this, the logging is setup. The log4j logger is used by using the log4j 'log' object (not the standard 'logger' object).

Do not build/deploy the project at this point.

Next, log into the integration server administration screen, go to the IS configuration->Path Settings screen, and enter the path that will be used for the log4j.properties configuration file (I would suggest /<logicalhost>/is/domains/<domainname>/config as a location):

Image:Log4j_isconfig_screenshot.gif

Make sure that you apply the changes to the IS (this will most likely require a restart of the IS process).

The next step is time to create the log4j.properties file. This file must be located in the exact path that was specified in the previous step. Here is a sample log4j.properties file:

log4j.rootCategory=INFO,FILE
log4j.rootLogger=INFO,FILE
log4j.logger.prjTest.jcdTest=INFO,prjTest.jcdTest

log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
log4j.appender.FILE.File=../logs/ALL-jcaps.log
log4j.appender.FILE.MaxFileSize=10MB
log4j.appender.FILE.MaxBackupIndex=10
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{ISO8601} %-5p [%t] [%c] [%x] %m%n

log4j.appender.prjTest.jcdTest=org.apache.log4j.DailyRollingFileAppender
log4j.appender.prjTest.jcdTest.DatePattern=’.’yyyy-MM-dd
log4j.appender.prjTest.jcdTest.File=../logs/prjTest.jcdTest.log
log4j.appender.prjTest.jcdTest.MaxFileSize=10MB
log4j.appender.prjTest.jcdTest.MaxBackupIndex=10
log4j.appender.prjTest.jcdTest.layout=org.apache.log4j.PatternLayout
log4j.appender.prjTest.jcdTest.layout.ConversionPattern=%d{ISO8601} %-5p [%t] [%c] [%x] %m%n

What this log4j.properties file does, is that there are two loggers defined. The root logger is called 'FILE', and ALL log4j log entries are written to the file defined here (../logs/ALL-jcaps.log). The second logger is called 'prjTest.jcdTest', and this is the separate log file that is only used for log entries from the jcdTest collaboration. It writes its log entries to a separate file (../logs/prjTest.jcdTest.log).

Note that this has the effect, that all entries are written two times - once to ALL-jcaps.log, and once to prjTest.jcdTest.log. However, this is often exactly what is required. It may be easier to use ALL-jcaps.log for monitoring and for getting an overview of the system process flow, but the prjTest.jcdTest.log file may be more useful if many projects are involved, or in a development environment with many developers.

If another JCD would be added to the system (say, prjNewTest, jcdNewTest), the log4j.properties file would be updated as follows:

log4j.rootCategory=INFO,FILE
log4j.rootLogger=INFO,FILE
log4j.logger.prjTest.jcdTest=INFO,prjTest.jcdTest
log4j.logger.prjNewTest.jcdNewTest=INFO,prjNewTest.jcdNewTest

log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
log4j.appender.FILE.File=../logs/ALL-jcaps.log
log4j.appender.FILE.MaxFileSize=10MB
log4j.appender.FILE.MaxBackupIndex=10
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{ISO8601} %-5p [%t] [%c] [%x] %m%n

log4j.appender.prjTest.jcdTest=org.apache.log4j.DailyRollingFileAppender
log4j.appender.prjTest.jcdTest.DatePattern=’.’yyyy-MM-dd
log4j.appender.prjTest.jcdTest.File=../logs/prjTest.jcdTest.log
log4j.appender.prjTest.jcdTest.MaxFileSize=10MB
log4j.appender.prjTest.jcdTest.MaxBackupIndex=10
log4j.appender.prjTest.jcdTest.layout=org.apache.log4j.PatternLayout
log4j.appender.prjTest.jcdTest.layout.ConversionPattern=%d{ISO8601} %-5p [%t] [%c] [%x] %m%n

log4j.appender.prjNewTest.jcdNewTest=org.apache.log4j.DailyRollingFileAppender
log4j.appender.prjNewTest.jcdNewTest.DatePattern=’.’yyyy-MM-dd
log4j.appender.prjNewTest.jcdNewTest.File=../logs/prjNewTest.jcdNewTest.log
log4j.appender.prjNewTest.jcdNewTest.MaxFileSize=10MB
log4j.appender.prjNewTest.jcdNewTest.MaxBackupIndex=10
log4j.appender.prjNewTest.jcdNewTest.layout=org.apache.log4j.PatternLayout
log4j.appender.prjNewTest.jcdNewTest.layout.ConversionPattern=%d{ISO8601} %-5p [%t] [%c] [%x] %m%n

This would ensure that jcdNewTest would also have it's own log file. However, if this step was forgotten or left out, the log entries would still be written to the ../logs/ALL-jcaps.log file.

At this point, you can now deploy your project, which should now log to the files defined in the log4j.properties file.

Note: Depending on your operating system, it may not be advisable to rename, move, delete or compress the currently active log4j file. On AIX, if this is done, the JVM does not notice that this file no longer exists, and log entries until the next file rollover are lost.