Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Scout/HowTo/3.8/Create a CustomLogManager for log4j"

< Scout‎ | HowTo‎ | 3.8
(Creating the CustomLogManager-class)
Line 16: Line 16:
  
 
== Creating the CustomLogManager-class ==
 
== Creating the CustomLogManager-class ==
Now the class <code>org.eclipse.scout.commons.logger.CustomLogManager</code> can be created as follows. (this implementation does not support the additional Scout logging features, described {{ScoutLink|Concepts/Logging#Additional_Logging_Features||here}})
+
Now the class <code>org.eclipse.scout.commons.logger.CustomLogManager</code> can be created as follows (this implementation does not support the additional Scout logging features, described {{ScoutLink|Concepts/Logging#Additional_Logging_Features||here}}).
 
<source lang="java">
 
<source lang="java">
 
package org.eclipse.scout.commons.logger;
 
package org.eclipse.scout.commons.logger;

Revision as of 10:14, 7 May 2012

The Scout documentation has been moved to https://eclipsescout.github.io/.

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 can be created as follows (this implementation does not support the additional Scout logging features, described The Scout documentation has been moved to https://eclipsescout.github.io/.).

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 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(Log4jLogWrapper.class.getName(), 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;
    }
  }
}

See Also

Back to the top