3

RCP アプリケーションを作成しました。そのために、log4j を使用してロギングを追加したいと考えています。

誰でも解決策を提案できますか?

すでにこれで試しましが、使用できません。

どんな助けでも大歓迎です!

4

6 に答える 6

2

Eclipse ロギングの使用を検討してください。Eclipse Wiki で説明されています: http://wiki.eclipse.org/E4/EAS/Logging_and_Tracing

このようにして、RCP プラットフォームからのログと独自のログが同じログファイルを共有するという利点があります。これにより、ログ ファイル内のイベントを簡単に関連付けることができます。

すでに E4 を使用している場合、ログ サービスを挿入するのは簡単です (つまり、プラグインにアクティベーター クラスは必要ありません)。上記の wiki リファレンスにも、その方法が説明されています。

于 2012-11-29T11:17:49.527 に答える
0

log4jjarファイルはすでにOSGiバンドルとしてパッケージ化されていますこれをターゲットプラットフォームのpluginsフォルダー(または、ターゲットとして使用している場合はEclipse pluginsフォルダー)にドロップし、プロジェクトのplugin.xmlへの依存関係として追加します。

于 2012-11-28T09:52:37.917 に答える
0

jar をクラスパスに追加すると、このコードでロガーを作成できます

private Log logger = LogFactory.getLog(getClass());

log4j.properties の次のエントリを使用して、ログ ファイルの場所を変更できます。

log4j.appender.file.File=${workspace_loc}/.metadata/MyLogFile.log

于 2013-08-13T06:39:42.083 に答える
0

org.osgi.service.log.LogServiceすべてのメッセージをLogListenerログに記録し、ログを他のカスタムログ (log4j、slf4j...など) に転送するために使用することをお勧めします 。このようにして、RCP フレームワークが行うログを失うことはありません。

于 2012-11-28T20:51:04.700 に答える
0
  1. プロジェクトに log4j.jar を追加します。
  2. MANIFEST.MF に参照を追加します。 Require-Bundle: org.apache.log4j;bundle-version="1.2.15"
  3. build.properties に参照を追加します。

    bin.includes = plugin.xml,\
    META-INF/,\
    .,\
    lib/log4j-1.2.17.jar

  4. .java ファイルでロガーを使用します。

    PropertyConfigurator.configure(log4jConfPath);
    LOGGER.debug("アプリケーションを開始しています..");

このチュートリアルは役に立ちました。確認してください-RCPにlog4jを追加してください

于 2016-02-03T12:22:35.147 に答える
-1

このコードは私にとって非常にうまく機能します。

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.RollingFileAppender;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import com.example.addresbook.util.PluginLogListener;



/**
 * The activator class controls the plug-in life cycle.
 * @author Iakov Senatov
 */
public class Activator extends AbstractUIPlugin {
	private static final String LOG4J_PROPERTIES = "META-INF/log4j.properties";
	private static final String LOG4J_FILE_PATTTERN = "%d{ISO8601} [%t] %-5p %c %x - %m%n";
	private static final Logger LOG = Logger.getLogger(Activator.class );
	public static final String PLUGIN_ID = "com.example.addresbook";
	final private List<PluginLogListener> pluginLogHooks = new ArrayList<PluginLogListener>();
	private static Activator plugin;
	
	
	
	/**
	 * The constructor
	 */
	public Activator() {
	}
	
	
	
	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void start(BundleContext context ) throws Exception {
		initLog4j(context );
		LOG.debug("start()" );
		super.start(context );
		plugin = this;
	}
	
	
	
	/**
	 * Inits the log4j.
	 *
	 * @throws IOException
	 */
	private void initLog4j(BundleContext context ) throws IOException {
		final String log4jfile = LOG4J_PROPERTIES;
		final URL confURL = getBundle().getEntry(log4jfile );
		//Define file appender with layout and output log file name
		final PatternLayout layout = new PatternLayout(LOG4J_FILE_PATTTERN );
		final String logPath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "logs"
						+ File.separator + "addtBook.log";
		final RollingFileAppender fileAppender = new RollingFileAppender(layout, logPath );
		PropertyConfigurator.configure(FileLocator.toFileURL(confURL ).getFile() );
		LOG.debug("Logging using log4j and configuration " + FileLocator.toFileURL(confURL ).getFile() );
		Logger.getRootLogger().addAppender(fileAppender );
		hookPluginLoggers(context );
		LOG.info("contextInitialized()" );
		LOG.info("Log4j initialized with " + confURL );
	}
	
	
	
	// Hook all loaded bundles into the log4j framework
	private void hookPluginLoggers(final BundleContext context ) {
		for(final Bundle bundle : context.getBundles() ) {
			final ILog pluginLogger = Platform.getLog(bundle );
			pluginLogHooks.add(new PluginLogListener(pluginLogger, Logger.getLogger(bundle.getSymbolicName() ) ) );
			LOG.info("Added logging hook for bundle: " + bundle.getSymbolicName() );
		}
	}
	
	
	
	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void stop(BundleContext context ) throws Exception {
		LOG.debug("stop()" );
		plugin = null;
		super.stop(context );
	}
	
	
	
	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		LOG.debug("getDefault()" );
		return plugin;
	}
	
	
	
	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path ) {
		LOG.debug("getImageDescriptor()" );
		return imageDescriptorFromPlugin(PLUGIN_ID, path );
	}
}

于 2015-05-06T01:46:26.053 に答える