如何在Eclipse RCP应用程序中正确设置Logback?

4

我已经花了过去的2到3天时间,试图让Logback在我的新Eclipse RCP项目中正确工作。(我第一次尝试使用RCP,但在其他Java项目中使用过logback)。

我想问大家的问题是:“是否有一个简单的Eclipse RCP(E4)插件来启用Logback日志记录?应该使用这里的插件(http://logback.qos.ch/p2/)吗?如果没有一个简单的插件可以使用,那么启用此日志记录的正确方法是什么?”

我已经多次阅读了这个页面(http://devblog.virtage.com/2012/07/logback-and-eclipse-attaching-logback-xml/),它看起来很有希望,但我还无法使其正常工作。


如果这只是用于记录 e4 RCP 中的错误,您应该使用 org.eclipse.e4.core.services.statusreporter.StatusReporter - greg-449
你能给我一个使用StatusReporter的好例子吗?我的搜索没有给出一个好的/清晰的例子。 - user2857108
IStatus status = statusReporter.newStatus(IStatus.ERROR, "message", any exception); statusReporter.report(status, StatusReporter.LOG); - greg-449
Logback需要找到它的配置文件。请尝试使用“-Dlogback.statusListenerClass=ch.qos.logback.core.status.OnConsoleStatusListener”。 - Martin Schröder
3个回答

5

我已成功配置了适用于RCP 3.x和4.x的logback工作配置。我使用Tycho进行构建,并使用功能和产品来组装应用程序(有关详细信息,请参见Vogella教程)。步骤如下:

  1. In your defining plugin (usually the one containing your application), add dependencies to ch.qos.logback.classic and ch.qos.logback.core, as well as org.slf4j.api. All other plugins just need to depend on org.slf4j.api to access loggers.
  2. In the feature that includes your defining plugin, add the ch.qos.logback.slf4j fragment on the Included PLug-ins page. Without this, SLF4J won't be able to find Logback in the deployed product.
  3. Create a logback.xml file in your defining plugin somewhere in classpath (src/main/resources if you are using Tycho/Maven).
  4. For an e4 application, add a life cycle manager, and initialize Logback in its @ProcessAdditions method:

    ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();  
    LoggerContext loggerContext = (LoggerContext) iLoggerFactory;  
    loggerContext.reset();  
    JoranConfigurator configurator = new JoranConfigurator();  
    configurator.setContext(loggerContext);  
    try {  
      configurator.doConfigure(getClass().getResourceAsStream("/logback.xml"));  
    } catch (JoranException e) {  
      throw new IOException(e.getMessage(), e);  
    }  
    
  5. For an Eclipse 3.x, application, the above code goes into the start() method of your application plugin's Activator.

您可能需要使用文件附加器,因为标准输出除非使用 -consoleLog 作为程序参数,否则不会显示。如果您将文件位置指定为 <file>${user.dir}/path/to/file</file>,则该文件将被创建在应用程序安装目录中。通常情况下,您会使用工作区元数据目录,以便与 Eclipse 日志放置在一起,例如:<file>${user.dir}/workspace/.metadata/rcp.log</file>


1
我尝试过这个。在IDE中运行时它可以工作,但是当我构建RCP产品时它就无法工作了。它可以找到logback.xml文件并进行配置,但是logback没有生成任何日志文件。你有什么想法是什么原因呢? - singularity

1
非常简单。 将您的logback.xml文件放在目录/config(或者您想要的任何位置)中。 在您的插件项目(plugin.xml)中,执行以下操作。
  • 依赖选项卡:添加:org.slf4j.api,ch.qos.logback.classic ch.qos.logback.core
  • 构建选项卡,“二进制构建”,勾选文件“config/logback.xml”
在您的初始化类(由plugin.xml中的“lifeCycleURI”指向的类)中添加以下内容:
@PostContextCreate
public void init()
   ...
   SLF4JConfigurator.configure();
   ...
}

SLF4JConfigurator类:

public final class SLF4JConfigurator {

private static final String BUNDLE_NAME = FrameworkUtil.getBundle(SLF4JConfigurator.class).getSymbolicName();
private static final String LOGBACK_XML = "platform:/plugin/" + BUNDLE_NAME + "/config/logback.xml";

public static void configure() {

  // Reset Current SLF4J config
  JoranConfigurator configurator = new JoranConfigurator();
  LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
  configurator.setContext(loggerContext);
  loggerContext.reset();

  // Read Configuration file
  try {
     URL configFile = new URL(LOGBACK_XML);
     InputStream configurationStream = configFile.openStream();
     configurator.doConfigure(configurationStream);
     configurationStream.close();
  } catch (JoranException | IOException e) {
     // Problem when reading file...
     e.printStackTrace();
  }
  StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);

  // Confirm message
  Logger logger = LoggerFactory.getLogger(SLF4JConfigurator.class);
  logger.info("Logging configuration initialised.");
}
}

在你的“产品”定义中包含3个依赖项。如果使用tycho构建,则来自eclipse的slf4j + logback插件将自动捆绑到您的应用程序中,无需添加特定于pom文件的内容。
我在JMSToolBox(在sourceforge上)中是这样做的。

0

这是我正在工作中的RCP应用程序插件的pom.xml文件的摘录:

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
    <groupId>es.fcc.rds</groupId>
  <artifactId>fcc.ima.oda</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  <packaging>eclipse-plugin</packaging>
  <properties>
    <tycho.version>0.19.0</tycho.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <repositories>
    <repository>
      <id>eclipse</id>
      <url>http://download.eclipse.org/releases/kepler</url>
      <layout>p2</layout>
    </repository>
    <repository>
      <id>logback</id>
      <url>http://logback.qos.ch/p2/</url>
      <layout>p2</layout>
    </repository>
  </repositories>
  ....

正如您所看到的,我正在使用http://logback.qos.ch/p2/存储库。

注意:经过多个月的正常工作后,它现在偶尔会失败,可能是由于p2存储库的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接