gpt4 book ai didi

java - 如何使 Fluent-logger-java 在 springboot 中与 logback Appender 一起工作

转载 作者:行者123 更新时间:2023-11-30 02:09:41 25 4
gpt4 key购买 nike

我正在尝试将 fluid-logger-java 与带有 logback 附加程序的 springboot 项目集成。我可以使用 https://github.com/fluent/fluent-logger-java 中给出的示例代码将数据发送到 Fluentd 聚合器。

但是当添加为 logback 附加程序时,它会抛出 NullPointerException。我尝试使用 https://mvnrepository.com/artifact/com.sndyuk/logback-more-appenders/1.4.3 中的 logback-more-appenders 。但它没有从应用程序发送任何数据。

要重现该问题,请从 https://start.spring.io/ 生成一个简单的 Web 项目使用默认值。

将 src\main\java\com\example\demo\FluentLogbackAppender.java 添加到项目中

package com.example.demo;
import java.util.HashMap;
import java.util.Map;
import org.fluentd.logger.FluentLogger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.UnsynchronizedAppenderBase;

public class FluentLogbackAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
private FluentLogger fluentLogger;
private String label = "test";

@Override
public void start() {
super.start();
this.fluentLogger = FluentLogger.getLogger(label, "localhost", 24224);
Map<String, Object> data = new HashMap<String, Object>();
data.put("msg", "Starting App");
fluentLogger.log(label, data);
}

@Override
protected void append(ILoggingEvent rawData) {
String msg = rawData.toString();
Map<String, Object> data = new HashMap<String, Object>(1);
data.put("msg", msg);
fluentLogger.log(label, data);
}

@Override
public void stop() {
super.stop();
fluentLogger.close();
}
}

将 src\main\resources\logback.xml 添加到项目

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE logback>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="fluent" class="com.example.demo.FluentLogbackAppender"/>
<root level="info" additivity="false">
<appender-ref ref="fluent" />
</root>
</configuration>

在pom.xml中添加依赖

<dependency>
<groupId>org.fluentd</groupId>
<artifactId>fluent-logger</artifactId>
<version>0.3.3</version>
</dependency>

我删除了一些代码以使其简单。运行此程序时,我会看到

2018-05-22 14:39:42.190 ERROR 18788 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.spi.Interpreter@5:77 - RuntimeException in Action for tag [appender] java.lang.NullPointerException
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:166)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.reinitialize(LogbackLoggingSystem.java:212)
at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:75)
at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:114)
at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:264)
at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:237)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:200)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:173)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:358)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:317)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
at com.example.demo.DemoApplication.main(DemoApplication.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

NullPointerException 从 FluentLogger 类内的 sender.emit() 返回。有趣的是,这段代码实际上在崩溃之前将数据推送到 Fluentd 聚合器。有什么办法可以实现这一点吗?

最佳答案

我猜,您没有在此处粘贴完整的代码。我认为你的类可能有标签字段的 getter、setter。如果是这样,那么您的 logback 配置丢失 <tag>test</tag>这可能是原因(仍然猜测)。 FluentLogger 有很多 Unresolved 问题,他们建议使用 Fluency 客户端库将数据刷新到 Fluentd。

一些引用资料 - https://github.com/fluent/fluent-logger-java/issues/72

https://github.com/sndyuk/logback-more-appenders/blob/master/src/main/java/ch/qos/logback/more/appenders/FluencyLogbackAppender.java

https://github.com/komamitsu/fluency

关于java - 如何使 Fluent-logger-java 在 springboot 中与 logback Appender 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50464458/

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