[英]Apache Felix OSGi logging with Logback
我当前的应用程序使用Logback进行日志记录。 我已经使用Apache Felix部署了OSGi框架,该框架允许在运行时动态注册包。 Felix的设置如下:
...
//System.out.println("Building OSGi Framework");
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
Map<String, String> config = new HashMap<>();
// specify the class loader
config.put(Constants.FRAMEWORK_BUNDLE_PARENT, Constants.FRAMEWORK_BUNDLE_PARENT_FRAMEWORK);
// specify the osgi cache directory
config.put(Constants.FRAMEWORK_STORAGE, appConfig.getOsgiCacheDir());
// make sure the cache is cleaned
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
// expose api
config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "list.of.packages.to.export");
// more properties available at: http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html
config.put("ds.showtrace", "false");
config.put("ds.showerrors", "true");
config.put("felix.fileinstall.dir", appConfig.getActiveOsgiExtDir());
config.put("felix.fileinstall.tmpdir", appConfig.getTempOsgiExtDir());
config.put("felix.fileinstall.bundles.startTransient","true");
config.put("felix.fileinstall.poll","5000");
config.put("felix.fileinstall.bundles.new.start", "true");
LOG.debug("OSGi config: " + config.toString());
framework = frameworkFactory.newFramework(config);
try {
framework.init();
FrameworkEvent event;
do {
framework.start();
...
唯一的问题是,Felix似乎没有日志记录。 当由于某种原因无法加载捆绑软件时,我看不到原因! 我知道我可以在捆绑软件中使用以下内容来获取父记录器:
ServiceReference ref = bundleContext.getServiceReference(LogService.class.getName());
if (ref != null) {
LogService log = (LogService) bundleContext.getService(ref);
...
}
但是,我不明白如何才能让felix首先使用logback作为记录器。
我猜您使用Apache Felix Log在OSGi容器中具有LogService。 Apache Felix Log实现了OSGi企业规范的“ 101 Log Service Specification ”一章。
LogService实现的基本功能会存储记录的条目一段时间,但不会将其写入任何地方。 您可以通过LogReaderService或LogListener查询记录的条目。
另一方面,您可以通过将slf4j-api和slf4j-logback添加到OSGi容器的方式来使用Logback。 这样,通过slf4j-api类记录的所有内容都将通过logback记录。
如果我的猜测是正确的,则osgi-loglistener-slf4j捆绑包可以为您提供帮助。 该捆绑软件不执行任何其他操作,但为环境中的每个LogReaderService注册一个LogListener并将其捕获的每个日志条目转发到slf4j-api 。 该捆绑包在maven-central上可用
如果您不想使用slf4j API,则可以编写一个类似的捆绑包,将日志从LogReaderService转发到Logback 。 如果检查我链接的包的来源,您将看到它仅用几行代码即可实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.