|
|
Line 1: |
Line 1: |
− | {{ScoutPage|cat=HowTo 4.0}}
| + | The Scout documentation has been moved to https://eclipsescout.github.io/. |
− | | + | |
− | == Creating the new fragment ==
| + | |
− | Create a new fragment to the host-Plug-In <code>org.eclipse.scout.commons</code> to get a <code>MANIFEST.MF</code> looking like this:
| + | |
− | <pre>
| + | |
− | 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
| + | |
− | </pre>
| + | |
− | | + | |
− | == 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}}).
| + | |
− | <source lang="java">
| + | |
− | 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();
| + | |
− | }
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | == Creating the Log4jLogWrapper-class ==
| + | |
− | Create the <code>Log4jLogWrapper</code> which will write every Scout log statement as a log4j log statement
| + | |
− | <source lang="java">
| + | |
− | 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());
| + | |
− | }
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | == 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 <code>Log4jLogUtility</code>.
| + | |
− | <source lang="java">
| + | |
− | 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;
| + | |
− | }
| + | |
− | }
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | == See Also ==
| + | |
− | *[[{{BASEPAGENAME}}/Make_use_of_the_additional_Scout_logging_features|Make use of the additional Scout logging features]]
| + | |
− | *[[{{BASEPAGENAME}}/Setup_different_Log-Configuration_for_Production_and_Development|Setup different Log-Configuration for Production and Development]]
| + | |
− | *[[Scout/Concepts/Logging|Concepts for logging]]
| + | |