[英]LoggerFactory cannot getLogger
I'm currently implementing a Logger and I'm wondering why the code won't run. 我正在实现一个Logger,我想知道为什么代码不会运行。 Most of the codes snipets are like these: snipets的大多数代码都是这样的:
Logger log = LoggerFactory.getLogger(this.getClass());
My imported classes: 我导入的课程:
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogReaderService;
import org.osgi.service.log.LogService;
import org.osgi.service.log.Logger;
import org.osgi.service.log.LoggerFactory;
But I can't seem to use getLogger. 但我似乎无法使用getLogger。
Why is that? 这是为什么?
Here is the source code of org.osgi.service.log.LoggerFactory
. 这是org.osgi.service.log.LoggerFactory
的源代码。
As you can see, it's an interface with no static methods therefore this code: 如您所见,它是一个没有静态方法的接口,因此这段代码:
Logger log = LoggerFactory.getLogger(this.getClass());
is simply not valid. 根本无效。
To fix this use slf4j
as front end (this means replace org.osgi.service.log.LoggerFactory
import with org.slf4j.LoggerFactory
etc.). 为了解决这个问题使用slf4j
作为前端(这意味着替换org.osgi.service.log.LoggerFactory
与进口org.slf4j.LoggerFactory
等)。
UPDATE UPDATE
If you want to stick with org.osgi.service.log.LoggerFactory
then follow this : 如果你想坚持org.osgi.service.log.LoggerFactory
然后按照这个 :
Obtain the LoggerFactory instance: 获取LoggerFactory实例:
public class Activator implements BundleActivator
{
private volatile LoggerFactory loggerFactory;
public void start(BundleContext context) throws Exception
{
ServiceReference ref = context.getServiceReference(LoggerFactory.class.getName());
if (ref != null)
{
loggerFactory = (LoggerFactory) context.getService(ref);
}
}
//..
Elsewhere in the bundle you can then use the LoggerFactory to get a Logger for any class: 在捆绑包中的其他地方,您可以使用LoggerFactory为任何类获取Logger:
Logger logger = loggerFactory.getLogger(Foo.class);
UPDATE2 UPDATE2
A better alternative would be to get a reference who's service type is LoggerFactory like this: 一个更好的选择是获得一个参考谁的服务类型是LoggerFactory像这样:
@Reference(service = LoggerFactory.class)
private Logger logger;
LoggerFactory is an OSGi service. LoggerFactory是一个OSGi服务。 You need to get it from the OSGi service registry. 您需要从OSGi服务注册表获取它。 See my EclipseCon Europe 2018 presentation for more information. 有关更多信息,请参阅我的EclipseCon Europe 2018演示文稿 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.