简体   繁体   English

LoggerFactory不能getLogger

[英]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? 这是为什么?

Thanks in advance! 提前致谢! 没有getLogger()类可用

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.

相关问题 java:找不到符号符号:类getLogger位置:类org.slf4j.LoggerFactory - java: cannot find symbol symbol: class getLogger location: class org.slf4j.LoggerFactory Slf4j LoggerFactory.getLogger和声纳 - Slf4j LoggerFactory.getLogger and sonarqube SessionBean 上的实例 LoggerFactory.getLogger 上的 StackOverFlowError - StackOverFlowError on instance LoggerFactory.getLogger on a SessionBean LoggerFactory.getLogger(ClassName.class) vs LoggerFactory.getLogger(this.getClass().getName()) - LoggerFactory.getLogger(ClassName.class) vs LoggerFactory.getLogger(this.getClass().getName()) LoggerFactory.getLogger“root”与“class”名称的区别是什么? - What the difference in LoggerFactory.getLogger “root” vs “class” name? 自定义Logger消息格式Java LoggerFactory.getLogger(getClass()) - Custom Logger message format Java LoggerFactory.getLogger(getClass()) 为什么不建议每次都调用LoggerFactory.getLogger(...)? - Why calling LoggerFactory.getLogger(…) every time is not recommended? 在Eclipse外部运行程序时出现NoSuchMethodError(org.slf4j.LoggerFactory.getLogger) - NoSuchMethodError (org.slf4j.LoggerFactory.getLogger) when running program outside Eclipse SLF4J,避免每次都写 LoggerFactory.getLogger(MyClassName.class) - SLF4J, To avoid writing LoggerFactory.getLogger(MyClassName.class) every time LoggerFactor.getLogger无法解析为某种类型 - LoggerFactor.getLogger cannot be resolved to a type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM