gpt4 book ai didi

java - 如何在 log4j2.xml 中配置 Log4J2 Kubernetes 支持

转载 作者:行者123 更新时间:2023-12-01 16:51:47 26 4
gpt4 key购买 nike

我想使用 log4j2 Kubernetes 支持将我的日志和有关具有 Fluent 位的 pod 的信息发送到 ElasticSearch。根据https://logging.apache.org/log4j/2.x/log4j-kubernetes/index.html有一些配置值需要设置。就我而言,我必须设置 log4j2.kubernetes.client.namespace,但我未能正确设置该属性。

在我当前版本的 log4j2.xml 中,我设置了几乎所有我能想象到的变体:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log4j2.kubernetes.client.namespace">app-dev</Property>
<Property name="kubernetes.client.namespace">app-dev</Property>
<Property name="client.namespace">app-dev</Property>
<Property name="log4j2_kubernetes_client_namespace">app-dev</Property>
<Property name="kubernetes_client_namespace">app-dev</Property>
<Property name="client_namespace">app-dev</Property>
<Property name="namespace">app-dev</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<Socket name="Elastic" host="fluent-bit" port="2020" protocol="tcp" bufferedIo="true">
<JsonLayout includeStackTrace="true" includeNullDelimiter="true">
<KeyValuePair key="containerId" value="${docker:containerId:-}"/>
<KeyValuePair key="application" value="$${lower:${spring:spring.application.name}}"/>
<KeyValuePair key="kubernetes.serviceAccountName" value="${k8s:accountName:-}"/>
<KeyValuePair key="kubernetes.containerId" value="${k8s:containerId:-}"/>
<KeyValuePair key="kubernetes.containerName" value="${k8s:containerName:-}"/>
<KeyValuePair key="kubernetes.host" value="${k8s:host:-}"/>
<KeyValuePair key="kubernetes.labels.app" value="${k8s:labels.app:-}"/>
<KeyValuePair key="kubernetes.labels.pod-template-hash" value="${k8s:labels.podTemplateHash:-}"/>
<KeyValuePair key="kubernetes.master_url" value="${k8s:masterUrl:-}"/>
<KeyValuePair key="kubernetes.namespaceId" value="${k8s:namespaceId:-}"/>
<KeyValuePair key="kubernetes.namespaceName" value="${k8s:namespaceName:-}"/>
<KeyValuePair key="kubernetes.podID" value="${k8s:podId:-}"/>
<KeyValuePair key="kubernetes.podIP" value="${k8s:podIp:-}"/>
<KeyValuePair key="kubernetes.podName" value="${k8s:podName:-}"/>
<KeyValuePair key="kubernetes.imageId" value="${k8s:imageId:-}"/>
<KeyValuePair key="kubernetes.imageName" value="${k8s:imageName:-}"/>
</JsonLayout>
</Socket>
</Appenders>
<Loggers>
<Logger name="xx.xxx.xxxx" level="info" additivity="false">
<AppenderRef ref="Elastic"/>
<AppenderRef ref="Console"/>
</Logger>

<Root level="info">
<AppenderRef ref="Elastic"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

pod 启动时得到的 Stacktrace:

Exception in thread "main" java.lang.NoSuchMethodError: 'java.lang.String org.apache.logging.log4j.util.PropertiesUtil.getStringProperty(java.lang.String[], java.lang.String, org.apache.logging.log4j.util.Supplier)'
at org.apache.logging.log4j.kubernetes.KubernetesClientProperties.getApiVersion(KubernetesClientProperties.java:66)
at org.apache.logging.log4j.kubernetes.KubernetesClientBuilder.kubernetesClientConfig(KubernetesClientBuilder.java:42)
at org.apache.logging.log4j.kubernetes.KubernetesClientBuilder.createClient(KubernetesClientBuilder.java:35)
at org.apache.logging.log4j.kubernetes.KubernetesLookup.initialize(KubernetesLookup.java:67)
at org.apache.logging.log4j.kubernetes.KubernetesLookup.lookup(KubernetesLookup.java:115)
at org.apache.logging.log4j.core.lookup.AbstractLookup.lookup(AbstractLookup.java:33)
at org.apache.logging.log4j.core.lookup.Interpolator.lookup(Interpolator.java:198)
at org.apache.logging.log4j.core.lookup.StrSubstitutor.resolveVariable(StrSubstitutor.java:1060)
at org.apache.logging.log4j.core.lookup.StrSubstitutor.substitute(StrSubstitutor.java:982)
at org.apache.logging.log4j.core.lookup.StrSubstitutor.substitute(StrSubstitutor.java:878)
at org.apache.logging.log4j.core.lookup.StrSubstitutor.replace(StrSubstitutor.java:433)
at org.apache.logging.log4j.core.config.plugins.visitors.PluginBuilderAttributeVisitor.visit(PluginBuilderAttributeVisitor.java:49)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.injectFields(PluginBuilder.java:185)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:121)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1002)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:942)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:934)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:934)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:934)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:552)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:241)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:288)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:579)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:651)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:668)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:253)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:138)
at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:45)
at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:48)
at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:30)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:363)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:388)

我的 pom.xml 的 Log4j2 部分:

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-kubernetes</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>4.4.1</version>
</dependency>

如何正确设置该属性,或者可以将其设置为环境变量/jvm 参数,以便在不同的命名空间中更加灵活? (或者我会使用该文件创建一个配置映射)

最佳答案

您收到 NoSuchMethodError。正在调用的方法是在 Log4j 2.13.0 中添加的。这意味着您以某种方式从早于该版本的版本中获得了 org.apache.logging.log4j.util.PropertiesUtil 的版本。您需要检查所有 jar,以确保类路径上没有 2 个版本。

关于java - 如何在 log4j2.xml 中配置 Log4J2 Kubernetes 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61669521/

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