问题描述
我遇到了Eclipse 4 RCP应用程序的一个问题。我需要记录一些事件。我需要以某种方式获得对记录器的引用。我知道,如何使用 IEclipseContext
,但我找不到,如何获得 IEclipseContext
没有依赖注射,我不能在激活剂中使用。你知道怎么解决这个问题吗?
I got stuck on one problem with an Eclipse 4 RCP application. I need to log some events. I need obtain somehow a reference to the logger. I know, how to do that using IEclipseContext
, but I've nowhere found, how to obtain IEclipseContext
without the dependency injection, which I cannot use in the activator. Do you anybody know, how to sort it out this problem, please?
非常感谢
推荐答案
似乎很遗憾,没有使用注入就无法获得 IEclipseContext
。
有一个答案写在:
It seems regretably, that there is no way to obtain IEclipseContext
without using injection.There is written in an answer to How to use eclipse 4 DI in classes that are not attached to the application model:
尽管如此,我已经解决了伐木问题和我的事情,这个原理一般都有效。总有一些服务提供您需要的东西。如果你不能使用依赖注入,你必须以某种方式(互联网和实验经常)获得适当的服务类名称。如果您已获得服务类名称,则可以从捆绑上下文中获取实例引用。幸运的是,可以在不使用注入的情况下访问bundle上下文。
Nevertheless I have already sorted out the problem of logging and I thing, the principle works generally. There is always some service providing things you need. If you cannot use the dependency injection, you've to get somehow (Internet and experiments are very often) an appropriate service class name. If you have got the service class name, then you can obtain an instance reference from the bundle context. Fortunately, the bundle context is accessible without using injection.
回到我们的日志记录问题。被搜索的类是 org.osgi.service.log.LogService
:
Back to our logging problem. The class being searched is org.osgi.service.log.LogService
:
public class Activator implements BundleActivator {
...
private static BundleContext context;
...
public static BundleContext getContext() {
return context;
}
...
public void start(BundleContext bundleContext) throws Exception {
ServiceReference<?> logser = bundleContext.getServiceReference(LogService.class);
LogService ls = (LogService)bundleContext.getService(logser);
//print an error to test it (note, that info can be below the threshold)
ls.log(LogService.LOG_ERROR, "The bundle is starting...");
Activator.context = bundleContext;
}
...
}
Etvoilà!
!ENTRY eu.barbucha.rcp-experiment.kernel 4 0 2013-08-20 07:32:32.347
!MESSAGE The bundle is starting...
这就是全部。稍后您可以使用 Activator.getContext()
获取捆绑上下文,如果需要的话。
That's all. Later you can obtain the bundle context using Activator.getContext()
, if it would be needed.
重要提示:很遗憾,您现在无法降低阈值。 JVM参数 -Declipse.log.level
不会影响OSGI日志服务,您现在只使用OSGI记录器。不幸的是,他们(可能暂时)硬编码了记录阈值(参见 ,在可能的情况下。
Important note: Regretably you cannot decrease the threshold now. The JVM argument -Declipse.log.level
does not affect the OSGI log service and you're using just the OSGI logger now. Unfortunately they (may have provisionally) hardcoded the logging threshold (see How to log warnings and infos in eclipse 3.7). I found out, that they haven't repair it yet. Neither in the Kepler release. However you can make a compromise. You can do that injection-way, where possible.
最终解决方案(以便在全球范围内捕获例外情况)
我扩展了我的激活器:
ServiceReference<?> logreser = bundleContext.getServiceReference(LogReaderService.class);
LogReaderService lrs = (LogReaderService) bundleContext.getService(logreser);
lrs.addLogListener(new LogListener() {
@Override
public void logged(LogEntry entry) {
System.err.println("Something was logged: " + entry.getMessage());
}
});
以以某事记录开头的文字真的出现了,whenewer就是某个地方登录。但最大的优点是,这个班级是我的。我可以控制它。日志条目还包含级别。我也可以轻松设置阈值。例如,在命令行上。
The text beginning with Something was logged really appears, whenewer is something somewhere logged. But the very advantage is, that this class is mine. I can control it. The log entry contains also the level. I can also easily set the threshold. For example on the command line.
这篇关于如何在激活器中获取IEclipseContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!