【发布时间】:2016-01-14 22:18:01
【问题描述】:
我当前的应用程序使用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 作为记录器。
【问题讨论】:
-
您无需登录即可找出捆绑软件无法启动的原因。只需从 shell 显式启动它并观察那里的错误消息。
-
你的绝对正确。但是,我通常需要能够执行日志记录,并且捆绑包能够将其日志记录传递给主应用程序框架。
标签: java logging osgi apache-felix blueprint-osgi