Saturday, December 25, 2010

Log into separate files according to package

log4j.properties

#log4j.rootLogger=debug, stdout, dave, daveutil

log4j.logger.dave=DEBUG, dave
log4j.logger.daveutil=DEBUG, daveutil

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.dave=org.apache.log4j.RollingFileAppender
# do not send to other logs
log4j.additivity.dave=false
log4j.appender.dave.File=dave.log

log4j.appender.dave.MaxFileSize=10KB
# Keep one backup file
log4j.appender.dave.MaxBackupIndex=5

log4j.appender.dave.layout=org.apache.log4j.PatternLayout
log4j.appender.dave.layout.ConversionPattern=%p %t %c - %m%n


log4j.appender.daveutil=org.apache.log4j.RollingFileAppender
# do not send to other logs
log4j.additivity.daveutil=false
log4j.appender.daveutil.File=daveutil.log

log4j.appender.daveutil.MaxFileSize=10KB
# Keep one backup file
log4j.appender.daveutil.MaxBackupIndex=5

log4j.appender.daveutil.layout=org.apache.log4j.PatternLayout
log4j.appender.daveutil.layout.ConversionPattern=%p %t %c - %m%n




configure log4j in application lifecycle listener

package start;


import java.net.URL;

import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.helpers.Loader;

import weblogic.application.ApplicationLifecycleEvent;
import weblogic.application.ApplicationLifecycleListener;

public class DaveStart extends ApplicationLifecycleListener {


public void preStart(ApplicationLifecycleEvent event){

System.out.println("DaveStart preStart");
}

public void postStart(ApplicationLifecycleEvent event){

System.out.println("DaveStart postStart");

// configure log4j
URL url = Loader.getResource("log4j.properties");
PropertyConfigurator.configure(url);

System.out.println("DaveStart log4j initialized");

}


}



using defined logger
package daveutil;

import org.apache.log4j.Logger;

public class TestDave {

static Logger logger = Logger.getLogger(TestDave.class.getName());

public String hello(String name) {

System.out.println("TestDave: START ");
logger.info("TestDave: START ");

System.out.println("Hello from TestDave to " + name);
logger.info("Hello from TestDave to " + name);

System.out.println("Using fast-swap");
logger.info("Using fast-swap");

return name;
}

}


created logs
[dave@dave base_domain]$ ls -lt
total 84
-rw-r-----. 1 dave vesedan 1112 Dec 25 15:37 dave.log
-rw-r-----. 1 dave vesedan 4888 Dec 25 15:37 daveutil.log
-rw-r-----. 1 dave vesedan 10830 Dec 25 15:37 dave.log.1



dave.log
[dave@dave base_domain]$ more dave.log
INFO [ACTIVE] ExecuteThread: '19' for queue: 'weblogic.kernel.Default (self-tuning)' dave.TestSesssionEJB - timeFromBegin2 timeToLive180000
0



daveutil.log
[dave@dave base_domain]$ more daveutil.log 
INFO [ACTIVE] ExecuteThread: '20' for queue: 'weblogic.kernel.Default (self-tuning)' daveutil.TestDave - TestDave: START
INFO [ACTIVE] ExecuteThread: '20' for queue: 'weblogic.kernel.Default (self-tuning)' daveutil.TestDave - Hello from TestDave to dave
INFO [ACTIVE] ExecuteThread: '20' for queue: 'weblogic.kernel.Default (self-tuning)' daveutil.TestDave - Using fast-swap
INFO [ACTIVE] ExecuteThread: '20' for queue: 'weblogic.kernel.Default (self-tuning)' daveutil.TestDave - TestDave: START
INFO [ACTIVE] ExecuteThread: '20' for queue: 'weblogic.kernel.Default (self-tuning)' daveutil.TestDave - Hello from TestDave to dave
INFO [ACTIVE] ExecuteThread: '20' for queue: 'weblogic.kernel.Default (self-tuning)' daveutil.TestDave - Using fast-swap

No comments:

Post a Comment