使用log4j2和JsonLayout对布局类型进行log4.2.6登录失败。

问题描述 投票:0回答:1

我对着这个问题研究了一天,试了这么多组合都没有成功。最后,我在寻找一个明确的步骤列表来获取Karaf的JSON日志。我甚至翻阅了Maven Karaf插件的源代码,试图理清这个问题,不过可能我看得还不够远。

我使用的是Karaf 4.2.6。我试图使用4.2.6版本的karaf-maven-plugin构建一个Karaf部署。

如果我改变我的 layout.type 的各种附属物 JsonLayout我收到一个stacktrace,说它不能加载JSON布局,因为我缺少一个类。java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ser/FilterProvider.

我看了一下jar org.ops4j.pax.logging/pax-logging-log4j2/1.10.2 我发现我需要3个Jackson jars,Core v2.8.7,Annotations 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 或者就是一直没有出现。我试过很多可能的组合。startupBundles, startupFeatures, bootFeatures但什么也没有得到它 startup.properties. 我不明白,怎么会有这样的内容。startup.properties 得到了生成。有时,我的应用程序想要的功能甚至无法启动,因为一些关于配置的东西在设置日志时被阻止了,如果我只是把对这些jar的引用放在了 startup.properties例如

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上搜索过,也上过Googled,找到了一些提示,但没有明确的步骤,导致了解决方案。我知道这里没有明确的文件列表,但是,正如我所说,我经过了许多组合。任何帮助都将是非常感激的。我也会在Karaf用户组上发帖。

json maven logging log4j2 apache-karaf
1个回答
1
投票

好了,经过几天的努力,我想明白了所有需要做的事情。谢谢@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 范围将被放置在 startup.properties 如果放在 <startupFeatures /> 配置中的Karaf Maven插件。

            <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日志。

© www.soinside.com 2019 - 2024. All rights reserved.