Thursday, June 14, 2007

Configuring log4j within a Spring env.

Configuring log4j in a web environment is easy. See this and this
However Spring provides no-outof box configuration class. Log4jConfigurer is abstract..

Here is a simple class that initialize log4j for a standalone application.


public class ConcreteLog4jConfigurer extends Log4jConfigurer {
private String location;

public void init() throws Exception {
initLogging(location);
}


public void destroy() throws Exception {
shutdownLogging();
}


/**
* @return the location
*/
public String getLocation() {
return location;
}


/**
* @param location the location to set
*/
public void setLocation(String location) {
this.location = location;
}


}

The trick is to configure this class with an init-method and destroy-method.


bean id="concreteLog4jConfigurer" class="ConcreteLog4jConfigurer" method="init"
property name="location"
value classpath:myLog4J.properties /value
/property
/bean


There are other properties like refresh period etc, that can be exposed too. That is left as an exercise to readers -:)

3 comments:

Jeeva said...

Thanks for a useful post

libc6 said...

Thanks. Great conf.

Anonymous said...

fantastic, great help!!!