Jump to: navigation, search

Scout/HowTo/3.7/Create a CustomLogManager for log4j

< Scout‎ | HowTo‎ | 3.7


Scout
Wiki Home
Website
DownloadGit
Community
ForumsBlogTwitter
Bugzilla
Bugzilla


Creating the new fragment

Create a new fragment to the host-Plug-In org.eclipse.scout.commons to get a MANIFEST.MF looking like this:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: org.eclipse.scout.commons.log4j.fragment
Bundle-SymbolicName: org.eclipse.scout.commons.log4j.fragment;singleton:=true
Bundle-Version: 1.0.0.qualifier
Fragment-Host: org.eclipse.scout.commons
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.apache.log4j;resolution:=optional,
 org.apache.log4j.helpers;resolution:=optional

Creating the CustomLogManager-class

Now the class org.eclipse.scout.commons.logger.CustomLogManager could be created. (this implementation does not support the additional Scout logging features, described here)

package org.eclipse.scout.commons.logger;
 
import java.io.File;
 
/**
 * Simple CustomLogManager which will redirect every Scout log statement to the log4j logger.
 *
 * Attention: This implementation does not support the additional Scout logging features.
 */
public class CustomLogManager implements IScoutLogManager {
 
  @Override
  public void initialize() {
  }
 
  @Override
  public IScoutLogger getLogger(String name) {
    return new Log4jLogWrapper(name);
  }
 
  @Override
  public IScoutLogger getLogger(@SuppressWarnings("rawtypes") Class clazz) {
    return getLogger(clazz.getName());
  }
 
  @Override
  public void setGlobalLogLevel(Integer globalLogLevel) throws UnsupportedOperationException {
    throw new UnsupportedOperationException();
  }
 
  @Override
  public Integer getGlobalLogLevel() throws UnsupportedOperationException {
    throw new UnsupportedOperationException();
  }
 
  @Override
  public boolean startRecording() throws UnsupportedOperationException {
    throw new UnsupportedOperationException();
  }
 
  @Override
  public File stopRecording() throws UnsupportedOperationException {
    throw new UnsupportedOperationException();
  }
}

Creating the Log4jLogWrapper-class

Create the Log4jLogWrapper which will write every Scout log statement as a log4j log statement

package org.eclipse.scout.commons.logger;
 
import java.util.logging.LogRecord;
 
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.eclipse.scout.commons.logger.internal.AbstractScoutLogger;
 
/**
 * Wrapper-implementation for log4j-{@link Logger}
 */
public class Log4jLogWrapper extends AbstractScoutLogger {
 
  private static final String FQCN = AbstractScoutLogger.class.getName();
 
  private Logger m_logger;
 
  public Log4jLogWrapper(String name) {
    m_logger = Logger.getLogger(name);
  }
 
  @Override
  public String getName() {
    return m_logger.getName();
  }
 
  @Override
  public int getLevel() {
    return Log4jLogUtility.convertLog4jToScoutLevel(m_logger.getEffectiveLevel());
  }
 
  @Override
  public void setLevel(int level) {
    m_logger.setLevel(Log4jLogUtility.convertScoutToLog4jLevel(level));
  }
 
  @Override
  protected void logImpl(LogRecord record) {
    int scoutLevel = JavaLogUtility.javaToScoutLevel(record.getLevel());
    Level log4jLevel = Log4jLogUtility.convertScoutToLog4jLevel(scoutLevel);
 
    m_logger.log(FQCN, log4jLevel, record.getMessage(), record.getThrown());
  }
}

Creating the Log4jLogUtility-class

As the last step the Scout log levels have to be converted to the log4j log levels and vice versa which is done by the Log4jLogUtility.

package org.eclipse.scout.commons.logger;
 
import org.apache.log4j.Level;
 
public class Log4jLogUtility {
 
  private Log4jLogUtility() {
  }
 
  public static int convertLog4jToScoutLevel(Level level) {
    if (level != null) {
      switch (level.toInt()) {
        case Level.ALL_INT:
        case Level.TRACE_INT:
          return IScoutLogger.LEVEL_TRACE;
        case Level.DEBUG_INT:
          return IScoutLogger.LEVEL_DEBUG;
        case Level.INFO_INT:
          return IScoutLogger.LEVEL_INFO;
        case Level.WARN_INT:
          return IScoutLogger.LEVEL_WARN;
        case Level.ERROR_INT:
          return IScoutLogger.LEVEL_ERROR;
        case Level.FATAL_INT:
          return IScoutLogger.LEVEL_ERROR;
        case Level.OFF_INT:
          return IScoutLogger.LEVEL_OFF;
      }
    }
    return IScoutLogger.LEVEL_WARN;
  }
 
  public static Level convertScoutToLog4jLevel(int level) {
    switch (level) {
      case IScoutLogger.LEVEL_TRACE:
        return Level.TRACE;
      case IScoutLogger.LEVEL_DEBUG:
        return Level.DEBUG;
      case IScoutLogger.LEVEL_INFO:
        return Level.INFO;
      case IScoutLogger.LEVEL_WARN:
        return Level.WARN;
      case IScoutLogger.LEVEL_ERROR:
        return Level.ERROR;
      case IScoutLogger.LEVEL_OFF:
        return Level.OFF;
      default:
        return Level.WARN;
    }
  }
}