gpt4 book ai didi

java - 从嵌入式 OSGi 框架使用 Log4j2

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:23 28 4
gpt4 key购买 nike

我们使用Apache Felix(版本 4.4.1)作为嵌入式 OSGi 框架。提供该框架的主要应用程序使用Log4j2(版本 2.0.1)作为日志记录框架。存在对 log4j-api 和 log4j-core 的依赖。当然,当主应用程序启动时,Log4j2 也会被加载和配置。主要部分中触发的所有日志消息都正确写入配置的附加程序。

现在是重点。加载的所有 bundle 应该能够使用相同的配置,并为此使用与主应用程序相同的 LoggingContext。我试图做的是通过将所有 Log4j2 包(来自 log4j2-api 和 log4j-core)作为 org.osgi.framework.system.packages.extra 放入框架配置中来提供它们。但是,当加载包时,log4j2 包不会像其他提供的包那样出现在接线包列表中,并且当包类尝试访问 Log4j2 类时,将抛出异常。

所有包都以相同的方式配置,并且在提供范围的情况下具有对 log4j2-api 和 log4j2-core 的 Log4j2 依赖性。当我查看 MANIFEST.MF 时,使用的 Log4j2 包位于Import-Packages部分

所以我的问题是。

Why exporting of Log4j2 packages this way does not work? How do I have to configure my environment so I can use same LoggingContext in my bundles and main application?

这是我在 Java 代码中对 OSGi 框架的配置部分:

Map<String, String> temp = new HashMap<String, String>();
// setting parameter that the felix cache is new initialized on each
// start of the application
temp.put(org.osgi.framework.Constants.FRAMEWORK_STORAGE_CLEAN, "onFirstInit");
// setting the packages that are provided by the main system and can
// be used by the osgi bundles
temp.put(org.osgi.framework.Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
// have to export these explicitly as the automatic export does
// not work for this internal java packages
+ "com.sun.org.apache.xerces.internal.dom,"
+ "com.sun.org.apache.xerces.internal.jaxp,"
+ "com.sun.org.apache.xerces.internal.util,"
// export the looging packages
+ "org.apache.log4j," + "org.apache.logging.log4j," + "org.apache.logging.log4j.message,"
+ "org.apache.logging.log4j.simple," + "org.apache.logging.log4j.spi,"
+ "org.apache.logging.log4j.status," + "org.apache.logging.log4j.util,"
+ "org.apache.logging.log4j.impl," + "org.apache.logging.log4j.appender,"
+ "org.apache.logging.log4j.appender.db," + "org.apache.logging.log4j.appender.db.jdbc,"
+ "org.apache.logging.log4j.appender.db.jpa," + "org.apache.logging.log4j.appender.db.jpa.converter,"
+ "org.apache.logging.log4j.appender.jms," + "org.apache.logging.log4j.appender.rewrite,"
+ "org.apache.logging.log4j.appender.rolling," + "org.apache.logging.log4j.appender.rolling.action,"
+ "org.apache.logging.log4j.appender.routing," + "org.apache.logging.log4j.async,"
+ "org.apache.logging.log4j.config," + "org.apache.logging.log4j.config.json,"
+ "org.apache.logging.log4j.config.plugins," + "org.apache.logging.log4j.config.plugins.osgi,"
+ "org.apache.logging.log4j.config.plugins.processor,"
+ "org.apache.logging.log4j.config.plugins.util," + "org.apache.logging.log4j.config.plugins.visitors,"
+ "org.apache.logging.log4j.config.status," + "org.apache.logging.log4j.config.xml,"
+ "org.apache.logging.log4j.config.yaml," + "org.apache.logging.log4j.filter,"
+ "org.apache.logging.log4j.impl," + "org.apache.logging.log4j.jackson,"
+ "org.apache.logging.log4j.jmx," + "org.apache.logging.log4j.layout,"
+ "org.apache.logging.log4j.lookup," + "org.apache.logging.log4j," + "org.apache.logging.log4j.net,"
+ "org.apache.logging.log4j.net.jms," + "org.apache.logging.log4j.net.server,"
+ "org.apache.logging.log4j.net.ssl," + "org.apache.logging.log4j.pattern,"
+ "org.apache.logging.log4j.selector," + "org.apache.logging.log4j.tools,"
+ "org.apache.logging.log4j.util," + "sun.misc," + "javax.jms," + "javax.mail,"
+ "javax.mail.internet," + "javax.mail.util," + "javax.persistence," + "javax.servlet,"
+ "com.sun.xml.internal.bind");
temp.put(FelixConstants.LOG_LEVEL_PROP, "4");
OSGI_CONFIG = Collections.unmodifiableMap(temp);

bundle 中对 log4j2 库的依赖定义如下:

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>

最佳答案

我让 log4j2 在 osgi 中以相对简单的配置工作。

您必须创建两个片段。

  • 第一个片段应将 log4j-api 作为宿主。它应该包含一个文件:META-INF/log4j-providers.properties(将其从 log4j2-core 中提取出来)
  • 第二个片段应该有 log4j-core 作为宿主。它应该包含一个文件:log4j2.xml(或任何你想要的配置文件)

只需将 log4j-api 和 log4j-core jar 文件放入插件目录(如果您使用的是 equinox,则为 p2)。我有以下链工作得很好:

commons-logging (jcl-over-slf4j) -> slf4j (slf4j-api) -> log4j-slf4-impl -> log4j-api -> log4j-core。

注意: slf4j 版本 1.7.7 log4j2 版本 2.1

关于java - 从嵌入式 OSGi 框架使用 Log4j2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25175794/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com