如何在RCP应用程序中添加记录器?

时间:2012-11-28 09:37:07

标签: eclipse-rcp rcp

我写了一个RCP应用程序。为此,我想使用log4j添加日志记录。

任何人都可以提出任何解决方案吗?

我已尝试使用this,但我无法使用它。

感谢任何帮助!

6 个答案:

答案 0 :(得分:2)

考虑使用Eclipse日志记录。它在Eclipse Wiki中描述:http://wiki.eclipse.org/E4/EAS/Logging_and_Tracing

通过这种方式,您可以从RCP平台和您自己的日志记录共享相同的日志文件。这样可以更容易地关联日志文件中的事件。

如果您正在使用E4,则很容易注入日志服务(即您的插件不需要激活器类)。上面的wiki参考也描述了如何做到这一点。

答案 1 :(得分:0)

log4j jar文件已打包为OSGi包。简单地放入目标平台的plugins文件夹(如果你将它作为目标,则放入Eclipse插件文件夹中),然后将其作为依赖项添加到项目plugin.xml中。

答案 2 :(得分:0)

我建议您使用org.osgi.service.log.LogService记录所有邮件,并添加LogListener以将日志转发到您拥有的任何其他自定义日志(log4j,slf4j ......等)。通过这种方式,您不会丢失RCP框架所做的任何日志。

答案 3 :(得分:0)

在您的类路径中使用jar,然后您可以使用此代码创建记录器

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

您可以使用以下log4j.properties

条目更改日志文件位置
  

log4j.appender.file.File = $ {workspace_loc} /。元数据/ MyLogFile.log

答案 4 :(得分:0)

  1. 在项目中添加log4j.jar。
  2. 在MANIFEST.MF中添加引用。 .article
  3. 在build.properties中添加引用。

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

  4. 在.java文件中使用记录器。

    PropertyConfigurator.configure(log4jConfPath);
     LOGGER.debug(“启动应用程序......”);

  5. 我发现本教程很有帮助。请检查 - Add log4j in RCP

答案 5 :(得分:-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 );
	}
}
&#13;
&#13;
&#13;

相关问题