【问题标题】:How to add logger in RCP application?如何在 RCP 应用程序中添加记录器?
【发布时间】:2018-06-27 19:13:44
【问题描述】:

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

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

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

感谢任何帮助!

【问题讨论】:

  • 我使用了同一篇文章,一切正常。你能具体说明什么不适合你吗?

标签: eclipse-rcp rcp


【解决方案1】:

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

这样您的优势是来自 RCP 平台的日志记录和您自己的日志记录共享相同的日志文件。这样可以更轻松地关联日志文件中的事件。

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

【讨论】:

  • 您好@Stefan 感谢您的链接。这个对我有用。但是我确实有一个问题,1.我们可以在这里给出一个项目特定的日志文件名而不是使用默认日志吗?
  • 嗨@Mandy,对不起,我还没试过。
【解决方案2】:

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

【讨论】:

  • 我想修改我的问题以获得更多说明。我有log4j jar。我想为我的应用程序创建记录器。如何创建它以及如何在程序中使用它。如果我将此插件交付给用户,他可以在哪里找到记录的消息?
  • 您是否创建了 log4j.properties 文件?您可以编辑您的问题以包含其内容吗?
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    你的类路径中的jar,然后你可以用这个代码创建记录器

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

    您可以使用以下 log4j.properties 条目更改日志文件位置

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

    【讨论】:

      【解决方案5】:
      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("正在启动应用程序..");

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

      【讨论】:

        【解决方案6】:

        这段代码非常适合我。

        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 );
        	}
        }

        【讨论】:

          猜你喜欢
          • 2016-07-05
          • 1970-01-01
          • 1970-01-01
          • 2013-09-29
          • 2014-04-10
          • 1970-01-01
          • 2013-06-10
          • 2018-02-23
          • 1970-01-01
          相关资源
          最近更新 更多