OSGI LoggerFactory

问题描述 投票:1回答:2

在我的OSGI努力中,我正在努力解决另一个看似简单的日志记录问题。

我们已经将日志记录包含在我们的捆绑包中并且可以正我们实际上正在使用pax-logging服务为我们做繁重的工作。

import org.ops4j.pax.logging.PaxLoggingService;
import org.osgi.service.component.*;

@Component( immediate=true )
public class ComponentImpl implements TimeService {

    @Reference
    private PaxLoggingService logs;

    @Activate
    public void activate(ComponentContext ctx)
    {
        // deprecated legacy interface
        logs.log(PaxLoggingService.LOG_INFO, "Activate called at " + getTime());
        logs.log(PaxLoggingService.LOG_INFO, "Activated component " + ctx.getProperties().get("component.id"));
    }
}

但有两件事情困扰着我们。首先,直接使用public void log(int level, String message)方法是deprecated since OSGI v1.4。其次,我们宁愿登录OSGI LogService。

然而,这似乎并不那么容易。我们首次尝试使用更新的日志记录接口,首先构建一个记录器实例,然后登录,结果是Java AbstractMethodError:

@Component( immediate=true )
public class ComponentImpl implements TimeService {

    @Reference
    private PaxLoggingService logs;

    @Activate
    public void activate(ComponentContext ctx)
    {
        // fancy, new logging interface - throws exception
        Logger logger = logs.getLogger(ComponentImpl.class);
        logger.log(PaxLoggingService.LOG_INFO, "Activate called at " + getTime());
    }
}

运行时异常(当我们尝试Apache Felix的LoggerService实现时也会发生这种情况)

java.lang.AbstractMethodError: org.ops4j.pax.logging.service.internal.PaxLoggingServiceImpl$1ManagedPaxLoggingService.getLogger(Ljava/lang/Class;)Lorg/osgi/service/log/Logger;
        at com.foobar.baz.osgi.testservice.ComponentImpl.activate(ComponentImpl.java:31)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        ...
        at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)
        at aQute.launcher.Launcher.startBundles(Launcher.java:517)
        at aQute.launcher.Launcher.activate(Launcher.java:423)
        at aQute.launcher.Launcher.run(Launcher.java:301)
        at aQute.launcher.Launcher.main(Launcher.java:147)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at aQute.launcher.pre.EmbeddedLauncher.main(EmbeddedLauncher.java:47)

理想情况下,您不希望在activate()中初始化记录器实例,但需要通过框架进行设置。 OSGI spec shows an example of this in section 112.3.12

@Component
public class MyComponent {
  @Reference(service=LoggerFactory.class)
  private Logger logger;
  @Activate
  void activate(ComponentContext context) {
    logger.trace(“activating component id {}”,
      context.getProperties().get(“component.id”));
  }
}

不幸的是,这个例子也不起作用。引用没有得到解决,因此捆绑从未运行...我一直在网上搜索,但没有找到任何相关的东西。似乎大多数人不使用OSGI服务接口进行日志记录,而只是使用slf4j(或其他façade); Pax将以任一方式捕获日志条目。所以在技术上它没有任何区别。

我认为我们的问题是没有人(PAX和Felix都没有)实现了OSGI LoggerFactory接口......

  • 是否存在实现LoggerFactory的OSGI LogService实现?
  • 登录“隐式”通道(如导入slf4j)而不是使用“显式”OSGI接口是否有利?
osgi
2个回答
1
投票

目前登录OSGi的最佳做法是使用slf4j作为前端。

Logger log = LoggerFactory.getLogger(this.getClass());

只需在课堂上使用它。 Pax-Logging为它提供后端,它也可以与后备后端一起使用。

OSGi R7提供了一些改进的日志服务集成,但我认为这还没有在平台上广泛使用。

使用@Reference进行日志记录的优点是,当您的日志记录后端尚未可用时,它可以消除启动时的计时问题。

像上面这样的slf4j集成的优点是它甚至可以用于需要在OSGi之外工作的混合罐。


0
投票
  • 到现在为止(2009年3月),(至少)Apache Felix Log完全实现了OSGi Logging 1.4。
  • 明确使用我所知道的OSGi Logger的最简单方法是通过这个logging facade。声明记录器就像使用slf4j或log4j一样简单,并且它不需要SCR(或类似的)。
© www.soinside.com 2019 - 2024. All rights reserved.