首页 > 解决方案 > 无法让 Karaf 4.2.6 使用带有 JsonLayout 的 log4j2 记录布局类型

问题描述

在尝试了这么多组合之后,我整天都在做这件事,但没有让它发挥作用。归根结底,我正在寻找从 Karaf 获取 JSON 日志记录的明确步骤列表。我什至浏览了 Maven Karaf 插件源代码,试图解决这个问题,但也许我看的还不够远。

我正在使用卡拉夫 4.2.6。我正在尝试使用 karaf-maven-plugin 版本 4.2.6 构建 Karaf 部署。

如果我将layout.type各种附加程序更改为JsonLayout,我会得到一个堆栈跟踪,说它无法加载 JSON 布局,因为我缺少一个类。java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ser/FilterProvider.

我查看了 jar org.ops4j.pax.logging/pax-logging-log4j2/1.10.2,发现我需要 3 个 Jackson jar,Core v2.8.7、Annotations v2.8.0 和 Databind v2.8.7。

我一直在尝试以etc/startup.properties5 或 8 的起始级别来获取这些文件,以便它们在org.ops4j.pax.logging/pax-logging-log4j2/1.10.2启动时做好准备。我确保它们在system正确位置的文件夹中。大多数时候,如果我确实让它们进入etc/startup.properties,它们的起始等级为 30。我将它们放置在具有正确起始等级的功能中,但它们要么在使用后很好地开始,要么startup.properties只是从未出现。我尝试了许多可能的组合,startupBundles, startupFeatures, bootFeatures,但没有得到它startup.properties。我无法弄清楚内容如何startup.properties生成了。startup.properties有时,我想要的应用程序功能甚至无法启动,因为如果我只是将这些 jar 的引用放在 中,则有关配置的某些内容在设置日志记录时会被阻止

mvn\:com.fasterxml.jackson.core/jackson-core/2.8.7 = 5

下面是我的 pom.xml,之前的尝试注释掉了一些东西:

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <properties>
      <my_version>0.0.0</my_version>
      <myapp_common_version>0.0.0</myapp_common_version>
    </properties>    

    <groupId>com.me.myapp</groupId>
    <artifactId>myapp-karaf</artifactId>
    <version>${my_version}</version>
    <packaging>karaf-assembly</packaging>

    <name>myapp-karaf</name>
    <description>myapp-karaf details</description>

    <repositories>
        <!-- Apache ServiceMix repository (for region) -->
        <repository>
            <id>apache.servicemix.m2</id>
            <name>Apache ServiceMix M2 repository</name>
            <url>http://svn.apache.org/repos/asf/servicemix/m2-repo</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <!-- OPS4J SNAPSHOT repository -->
        <repository>
            <id>ops4j.sonatype.snapshots.deploy</id>
            <name>OPS4J snapshot repository</name>
            <url>https://oss.sonatype.org/content/repositories/ops4j-snapshots/</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <!-- Apache SNAPSHOT -->
        <repository>
            <id>apache.snapshots.deploy</id>
            <name>Apache snapshot repository</name>
            <url>https://repository.apache.org/content/groups/snapshots-group</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.apache.karaf.features</groupId>
            <artifactId>framework</artifactId>
            <version>4.2.6</version>
            <type>kar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.karaf.features</groupId>
            <artifactId>standard</artifactId>
            <version>4.2.6</version>
            <classifier>features</classifier>
            <type>xml</type>
        </dependency>
        <dependency>
            <groupId>org.apache.karaf.features</groupId>
            <artifactId>spring</artifactId>
            <version>4.2.6</version>
            <classifier>features</classifier>
            <type>xml</type>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.karaf.features</groupId>
            <artifactId>enterprise</artifactId>
            <version>4.2.6</version>
            <classifier>features</classifier>
            <type>xml</type>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.me.myapp</groupId>
            <artifactId>myapp-common-feature</artifactId>
            <version>${myapp_common_version}</version>
            <classifier>features</classifier>
            <type>xml</type>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.me.myapp</groupId>
            <artifactId>myapp-feature</artifactId>
            <!-- Annoyingly this cannot come from an environment variable -->
            <version>${my_version}</version>
            <classifier>features</classifier>
            <type>xml</type>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>${env.MYAPP_SOURCE_HOME}/myapp-common/server/karaf/assembly</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/filtered-resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.karaf.tooling</groupId>
                    <artifactId>karaf-maven-plugin</artifactId>
                    <version>4.2.6</version>
                    <extensions>true</extensions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>process-resources</id>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.karaf.tooling</groupId>
                <artifactId>karaf-maven-plugin</artifactId>
                <configuration>
                    <startupBundles> <!--
                        <bundle>mvn:com.fasterxml.jackson.core/jackson-annotations/2.8.0</bundle>
                        <bundle>mvn:com.fasterxml.jackson.core/jackson-core/2.8.7</bundle>
                        <bundle>mvn:com.fasterxml.jackson.core/jackson-databind/2.8.7</bundle> -->
                    </startupBundles>
                    <installedFeatures>
                    </installedFeatures>
                    <startupFeatures>
                        <feature>com.me.myapp/${my_version}</feature>
                    </startupFeatures>
                    <bootFeatures>
                        <feature>com.me.myapp.logging.provider/${myapp_common_version}</feature>
                        <!-- standard distribution -->
                        <!-- <feature>standard</feature> -->
                        <!-- minimal distribution -->
                        <!--<feature>minimal</feature>-->
                        <feature>jaas</feature>
                        <feature>shell</feature>
                        <feature>ssh</feature>
                        <feature>management</feature>
                        <feature>bundle</feature>
                        <feature>config</feature>
                        <feature>deployer</feature>
                        <feature>diagnostic</feature>
                        <feature>instance</feature>
                        <feature>kar</feature>
                        <feature>wrap</feature>
                        <feature>log</feature>
                        <feature>package</feature>
                        <feature>service</feature>
                        <feature>system</feature>

                    </bootFeatures>
                    <javase>1.8</javase>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

com.me.myapp.logging.provider包含具有正确起始级别的 Jackson 罐子。

我在 Kubernetes 中运行我的容器,并希望 Kubernetes 中的标准日志收集工作。我在日志收集器上使用了一个额外的插件,至少在某种程度上将事物分离为 JSON 格式,但它很容易出错,我宁愿从 JSON 格式的日志开始,而不是稍后处理下游,这样我就可以分离记录时,例如执行操作的人。

我在 Stack Overflow 上进行了搜索,谷歌搜索并找到了提示,但没有明确的步骤可以找到解决方案。我意识到这里没有明确的文件列表,但是,正如我所说,我经历了很多组合。任何帮助将非常感激。我还将在 Karaf 用户组上发帖。

标签: jsonmavenlogginglog4j2apache-karaf

解决方案


好的,几天后,我想出了所有需要做的事情。谢谢@GrzegorzGrzybek 的帮助。

我需要以下几行

mvn\:org.ops4j.pax.logging/pax-logging-log4j2-extra/1.11.4 = 8
mvn\:com.fasterxml.jackson.core/jackson-core/2.9.10 = 8
mvn\:com.fasterxml.jackson.core/jackson-annotations/2.9.10 = 8
mvn\:com.fasterxml.jackson.core/jackson-databind/2.9.10 = 8

添加到我的etc/startup.properties. 如果我只是编辑文件以添加它们,并确保我在system存储库中有这些文件,那么它将无法正常工作。不知道,配置中的其他地方没有进行某些条目,etc因此容器无法启动,或者在安装处理程序之前启动了某些功能集wrap,因此整个容器将停止运行。

我尝试了很多组合,但最终找到了如何将东西放入etc/startup.properties. 我将我想要的捆绑包放入他们自己的功能中,并带有我希望它们具有的起始级别。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<features xmlns="http://karaf.apache.org/xmlns/features/v1.3.0" name="myapp-common-container-feature">
  <feature name="com.me.myapp.logging.provider" version="0.12.0" description="MyApp Logging Provider">
    <bundle start-level="8">mvn:com.fasterxml.jackson.core/jackson-annotations/2.9.10</bundle>
    <bundle start-level="8">mvn:com.fasterxml.jackson.core/jackson-core/2.9.10</bundle>
    <bundle start-level="8">mvn:com.fasterxml.jackson.core/jackson-databind/2.9.10</bundle>
    <bundle start-level="8">mvn:org.ops4j.pax.logging/pax-logging-log4j2-extra/1.11.4</bundle>
  </feature>
</features>

注意start-level属性。

然后,在我的程序集 POM 中,我放置了以下依赖项:

        <dependency>
            <groupId>com.me.myapp</groupId>
            <artifactId>myapp-common-container-feature</artifactId>
            <version>0.12.0</version>
            <classifier>features</classifier>
            <type>xml</type>
            <scope>compile</scope>
        </dependency>

这里重要的一行是<scope>compile</scope>. 如果放置在Karaf Maven 插件的配置中,compile范围内的功能将被放置。startup.properties<startupFeatures />

            <plugin>
                <groupId>org.apache.karaf.tooling</groupId>
                <artifactId>karaf-maven-plugin</artifactId>
                <configuration>
                  <startupFeatures>                
                    <feature>com.me.myapp.logging.provider/0.12.0</feature>
                  </startupFeatures>

现在容器以 JSON 日志记录启动。


推荐阅读