无法使Karaf 4.2.6使用布局类型的Log4j2和JsonLayout进行记录 [英] Failing to get Karaf 4.2.6 to log using log4j2 with JsonLayout for a layout type

查看:25
本文介绍了无法使Karaf 4.2.6使用布局类型的Log4j2和JsonLayout进行记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经这样做了一整天,在尝试了这么多组合之后,仍然没有奏效。归根结底,我正在寻找从Karaf获取JSON日志的明确步骤列表。我甚至翻阅了Maven Karaf插件的源代码,试图解决这个问题,尽管我可能看得还不够深入。

我使用的是Karaf 4.2.6。我正在尝试使用Karaf-maven-plugin 4.2.6版构建一个Karaf部署。

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

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

我一直在尝试将这些文件放入etc/startup.properties,开始级别为5或开始级别为8,以便它们在org.ops4j.pax.logging/pax-logging-log4j2/1.10.2启动时做好准备。我确保它们位于system文件夹中的正确位置。大多数时候,如果我让他们进入etc/startup.properties,他们的起点级别是30。我把它们放在一个具有正确开始级别的功能中,但它们要么在使用startup.properties之后开始得很好,要么就是从来没有出现过。我尝试了许多可能的组合,startupBundlesstartupFeaturesbootFeatures,但都没有在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 Jars。

我在Kubernetes中运行我的容器,希望Kubernetes中的标准日志收集能够工作。我在日志收集器上使用了一个额外的插件,以至少将一些东西分离成JSON格式,但它很容易出错,我宁愿从JSON格式的日志开始,而不是稍后进行后续处理,这样我就可以在日志记录时分离日志的组件,例如执行操作的人员。

我搜索了Stack Overflow,搜索了Google,找到了提示,但没有明确的步骤来解决问题。我知道这里没有明确的文件列表,但正如我所说的,我经历了许多组合。任何帮助都将不胜感激。我还会在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>。如果将compile范围内的功能放置在Karaf Maven插件配置的<startupFeatures />中,则startup.properties范围内的功能将被放置在startup.properties内。

            <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日志记录启动。

这篇关于无法使Karaf 4.2.6使用布局类型的Log4j2和JsonLayout进行记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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