Home > Software > Configuring logging in Spring

Configuring logging in Spring


If you’re like me, then you probably don’t want thousands of configuration files for your applications and keep everything in one place.
Few years back I was using Log4J for logging, but as a new solution has made its way into the SDK, I changed too. I know that a lot of people will argue and say that Log4J is much better, but I like more to use standard solution. Also Log4J became more like a de facto industry standard, so it becomes more like of a personal preference.
Anyway the solution proposed onward can be applied to both java.util.logging and Log4J, but I will exemplify only the first.
With Spring IoC container, you can easily create beans in your context. The idea is very simple, just create a bean using the Spring IoC and use the setter methods to configure the logging system.

public class LogConfigurator {

  public void setLogLevels(Map<String, String> logLevels) {
    for (String loggerName : logLevels.keySet()) {
      Logger.getLogger(loggerName).setLevel(Level.parse(logLevels.get(loggerName)));
    }
  }

  public void setLogHandlers(Map<String, Handler> logHandlers) {
    for (String loggerName : logHandlers.keySet()) {
      Logger.getLogger(loggerName).addHandler(logHandlers.get(loggerName));
    }
  }

  public void setLogUseParentHandlers(Map<String, Boolean> logUseParentHandlers) {
    for (String loggerName : logUseParentHandlers.keySet()) {
      Logger.getLogger(loggerName).setUseParentHandlers(logUseParentHandlers.get(loggerName));
    }        
  }
}

The above methods are used to set the logging levels, handlers and if to use the parent logger handlers. They receive as parameter a map between the logger name and the setting.

Now in your applicationContext.xml file you will have:

<bean id="logConfigurator" class="LogConfigurator">
  <property name="logHandlers">
    <map>
      <entry key="module.1" value-ref="customHandler"/>
    </map>
  </property>
  <property name="logLevels">
    <map>
      <entry key="module.2" value="INFO"/>
    </map>
  </property>
</bean>

This will set the log level of the second module to INFO and will add a custom handler to the first module.

You can apply the same technique to some other configurations as well.

Categories: Software Tags: ,
  1. Jayanth
    July 18, 2008 at 4:00 pm

    Hi,

    We have a problem when using the Log4j: It seems that not
    all log statements are writing to the file and It is Multithreaded environment.

    In Detailed:
    1 All the messages are writing at the Database
    2. By using Sys Prints – we could able see all the messages.
    3. But not able to see all the messages in the log file.
    4. It is Multithreaded env and Springs and Hybernate configuration

    Example:
    We are dealing with around 11,000 Messages and all the 11,000 messages are getting updated at the Database,Could able to see 11,000 SysOutPut messages but 100 messages are missing if we trying through the log4j.

    Any help would be highley appreciated.

    Please revert for any questions/clarifications.

    Regards,
    Jayanth

  2. July 21, 2008 at 10:40 am

    Have you tried to use the ConsoleAppender or JDBCAppender? Also consider using the AsyncAppender in a multithreaded environment.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: