Log4j2 API在OSGi环境中找不到Log4j2内核 [英] Log4j2 api cannot find Log4j2 core in OSGi environment

查看:122
本文介绍了Log4j2 API在OSGi环境中找不到Log4j2内核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用log4j2 OSGi捆绑软件,但似乎log4j2 api在OSGi环境中找不到log4j2内核.我不断收到以下异常:

I'm trying to use log4j2 OSGi bundles, but it seems log4j2 api cannot find log4j2 core in an OSGi environment. I'm continuously getting the following exception :

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console

我在几个地方都发现了相同的例外,但仍然无法弄清楚这个问题.我怀疑出现此问题是因为log4j2 api在log4j2核心的META-INF目录内找不到log4j-provider.properties. 有什么线索为什么我会收到此异常以及如何解决该问题? (如果有人拥有正确的pom文件来添加log4j依赖项并进行捆绑,请与我共享)

I found the same exception discussed in few places but still I could not figure out this issue. Isuspect I'm getting this issue because log4j2 api cannot find the log4j-provider.properties inside the META-INF directory of log4j2 core. Is there any clue why I'm getting this exception and how can I correct the issue ? (If anybody has correct pom file for adding log4j dependencies and bundling please share it with me)

这些是我使用的依赖项

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.2</version>
    </dependency>

我使用apache felix作为捆绑插件.发生此错误是因为log4j api不可见log4j2核心的META-INF中的资源,特别是log4j-providoer.properties文件.

I use apache felix as the bundle plugin. This error occures because resources inside the META-INF of log4j2-core specially the log4j-providoer.properties file is not visible to log4j api.

谢谢!

推荐答案

启动Activator时,log4j-core捆绑包注册捆绑包侦听器,并开始搜索日志插件,如果发现某些内容,它将执行一系列类似于以下操作的操作通常的Logger初始化(不是真正的OSGi东西,我不确定它是否有效,但似乎至少设置了Log4jContextSelectorLoggerContextFactory),只是为了确定这一点,您是否安装了启动log4j-core捆绑包并验证是否没有任何更改?

The log4j-core bundle registers a bundle listener when the Activator is started and starts searching for log plugins and if something is found it performs a sequence of operations similar to the usual Logger initialization (not really idiomatic OSGi stuff and i'm not sure it works, but it seems to set at least Log4jContextSelector and LoggerContextFactory), just to be sure of it, did you install and start the log4j-core bundle and verified that nothing changed?

更新:

我进行了一些测试,找到了解决log4j2 OSGi问题的可接受的解决方案/解决方法.但是,正如其他人所建议的那样,或者我将使用slf4j,pax日志记录或仅使用OSGi日志服务(这是其中最简单的一种).

I did some testing and found what is an acceptable solution/workaround for log4j2 OSGi issues. But as someone else recommended, alternatively i would use slf4j, pax-logging or simply the OSGi Log Service (the simpler of the bunch).

@Grant,您有两个需要修复的独立问题:

@Grant, you have 2 separate things that need to be fixed:

1..正如您所描述的,"Log4j2找不到日志记录实现"错误是由于log4j2-api捆绑包找不到 log4j-provider的事实引起的.属性文件,此问题得到解决后,log4j2-api找不到log4j2-core类(这是一个不同的包,并且log4j2-api没有特定的导入包:这些类).

1. As you described, the "Log4j2 could not find a logging implementation" error is caused by the fact that the log4j2-api bundle is unable to find the log4j-provider.properties file and, after that is fixed, log4j2-api cannot find the log4j2-core classes (it's a different bundle and log4j2-api doesn't have a specific Import-Package: for those classes).

此问题的解决方法是为log4j2-api(我称为mine log4j-api-config.jar)创建一个小的片段包,并在META-INF中使用该.properties文件和一个清单进行强制动态导入:

The workaround for this is to create a small fragment bundle for log4j2-api (i called mine log4j-api-config.jar) with that .properties file in META-INF and a manifest that forces a dynamic import:

    Manifest-Version: 1.0
    Bundle-ManifestVersion: 2
    Bundle-Name: Log4j API configurator
    Bundle-SymbolicName: org.apache.logging.log4j.apiconf
    Bundle-Version: 1.0.0
    Bundle-Vendor: None
    Bundle-RequiredExecutionEnvironment: OSGi/Minimum-1.2
    Fragment-Host: org.apache.logging.log4j.api
    DynamicImport-Package: *

我要在此处导入*,您可以通过添加log4j2-api所需的log4j2-core软件包的必需子集来对其进行改进.

I'm importing * here, you can improve it adding the required subset of log4j2-core packages that log4j2-api needs.

有了这个,该错误将消失,但是log4j会注意到您没有提供log4j2配置文件,这是下一个要修复的问题(仅在这种情况下需要注意).

With this, that error will disappear, but log4j will notice that you didn't provide a log4j2 configuration file, next thing to fix (only if you care in this case).

2..此时,Felix将显示以下内容:

2. At this point Felix will display this:

log4j2.xml not found by org.apache.logging.log4j.core
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

,我想您可能想添加自己的 log4j2.xml 而又不会弄乱原来的log4j2-core.jar.您可以执行此操作以创建另一个片段捆绑包,这次由log4j2-core托管,在根目录中仅包含一个 log4j2.xml 配置文件和一个简单的清单:

and i suppose you could want to add your own log4j2.xml without messing with the original log4j2-core.jar. You can do this creating another fragment bundle, this time hosted by log4j2-core, with just a log4j2.xml configuration file in the root and a simple manifest:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Log4j Core configurator
Bundle-SymbolicName: org.apache.logging.log4j.coreconf
Bundle-Version: 1.0.0
Bundle-Vendor: None
Bundle-RequiredExecutionEnvironment: OSGi/Minimum-1.2
Fragment-Host: org.apache.logging.log4j.core

我在测试期间使用了以下简单的 log4j2.xml 配置:

I used this simple log4j2.xml configuration during my tests:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

有了这个,您将不再需要下面描述的那种桥接"捆绑软件,而您只需要一个简单的Import-Package: org.apache.logging.log4j即可使用捆绑软件中的log4j.

With this you will not need that sort of "bridge" bundle you described below anymore, and you'll just need a simple Import-Package: org.apache.logging.log4j to use log4j from your bundle.

更新2:

重要的是,这两个片段与原始捆绑包无关(无需修改log4j jar或什至您的捆绑包以添加导入/导出),因此原始捆绑包和您自己的自定义捆绑包将保持不变. 而且,它们也不依赖于原始捆绑包,它们只是带有清单和附加文本文件的基本jar存档,没有代码,不需要Import-Package或Export-Package.

Important to note that the two fragments are NOT dependencies of the original bundles (no need to modify log4j jars or or even your bundles to add import/export), so the original bundles and your own custom ones will remain untouched. Also, they don't depend on the original bundle either, they are just basic jar archive with a manifest and an additional text file, no code, no need for Import-Package or Export-Package.

您只需要在安装了主机捆绑包之后安装每个片段.

You just need to install each fragment after their host bundle is installed.

我已经从一个空的jar手动创建了这两个片段,并在存档中复制了属性文件,并使用文本编辑器修改了MANIFEST.MF,您可以使用此pom.xml来创建它们,记住要复制log4j- pom.xml所在的provider.properties.

I've created both fragments manually starting from an empty jar and copying inside the archive the properties file and modifying the MANIFEST.MF with a text editor, you can create them both with this pom.xml, remember to copy log4j-provider.properties where pom.xml is located.

对于log4j2-api片段:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>my.group</groupId>
    <artifactId>log4j2-api-config</artifactId>
    <version>1.0</version>
    <name>log4j2-api-config</name>
    <packaging>bundle</packaging>
    <properties>
        <java-version>1.7</java-version>
    </properties>

    <dependencies>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.0.0</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>org.apache.logging.log4j.apiconf</Bundle-SymbolicName>
                        <Bundle-Name>Log4j API Configurator</Bundle-Name>
                        <Bundle-Version>1.0.0</Bundle-Version>
                        <Fragment-Host>org.apache.logging.log4j.api</Fragment-Host>
                        <DynamicImport-Package>
                            *;resolution:=optional
                        </DynamicImport-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>.</directory>
                <includes>
                    <include>log4j-provider.properties</include>
                </includes>
                <targetPath>META-INF</targetPath>
            </resource>
        </resources>
    </build>
</project>

在适当的地方修改此pom(包括文件,捆绑包名称)以生成具有log4j2-core配置的另一个pom.

Modify this pom where appropriate(included file, bundle names) to generate the other one with the log4j2-core configuration.

这篇关于Log4j2 API在OSGi环境中找不到Log4j2内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆