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

log4j jar 文件已打包为 OSGi 包。只需将其拖放到目标平台的插件文件夹中(如果您将其用作目标,则将其拖放到 Eclipse 插件文件夹中),然后将其作为依赖项添加到您的项目 plugin.xml 中。

于 2012-11-28T09:52:37.917 回答
0

我建议您使用 org.osgi.service.log.LogService记录所有消息并将LogListener日志添加到您拥有的任何其他自定义日志记录(log4j、slf4j...等)。这样,您就不会丢失 RCP 框架所做的任何日志。

于 2012-11-28T20:51:04.700 回答
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
  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 回答