如何在springboot中使fluent-logger-java与logback appender一起使用 [英] How to make fluent-logger-java work with logback appender in springboot
问题描述
我正在尝试将fluent-logger-java与带有回退附加程序的springboot项目集成在一起.而且我能够使用 https://github中给出的示例代码将数据发送到流利的聚合器.com/fluent/fluent-logger-java
但是当添加为回退附加程序时,它将引发NullPointerException. 我尝试使用 https://mvnrepository.com/artifact/com.sndyuk/logback-more-appenders/1.4.3 . 但是它没有从应用程序发送任何数据.
要重现此问题,请从 https://start.spring.io/具有默认值.
将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()返回. 有趣的是,此代码实际上在崩溃之前将数据推送到流利的聚合器. 有什么办法可以使这项工作.
我正在猜测,您没有在此处粘贴完整的代码.我认为您的班级可能会在标签领域使用吸气剂,吸气剂.如果是这样,则您的logback配置缺少<tag>test</tag>
,这可能是原因(仍是猜测). FluentLogger存在很多未解决的问题,建议使用fluency客户端库来刷新要流利的数据.
一些参考- https://github.com/fluent/fluent-logger-java/issues/72
https://github.com/komamitsu/fluency
I am trying to integrate fluent-logger-java with a springboot project with a logback appender. And I am able to send data to a fluentd aggregator with the sample code given in https://github.com/fluent/fluent-logger-java
But when added as a logback appender, it throws NullPointerException. I have tried to use logback-more-appenders from https://mvnrepository.com/artifact/com.sndyuk/logback-more-appenders/1.4.3. But it is not sending any data from the application.
To reproduce the issue, generate a simple web project from https://start.spring.io/ with defaults.
Add src\main\java\com\example\demo\FluentLogbackAppender.java to the project
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();
}
}
Add src\main\resources\logback.xml to the project
<?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>
Adding the dependency in pom.xml
<dependency>
<groupId>org.fluentd</groupId>
<artifactId>fluent-logger</artifactId>
<version>0.3.3</version>
</dependency>
I have stripped some code to make it simple. When running this, I am presented with
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 is returned from sender.emit() inside FluentLogger class. The interesting thing is, this code actually pushes the data to fluentd aggregator just before crash. Is there any way to make this work.
I am guessing, you did not paste full code here. I think your class might have getter, setter for tag field. If so then your logback configuration is missing <tag>test</tag>
which might be the reason(still guess). FluentLogger has lot of open issues and they recommend to use fluency client library for flushing data to fluentd.
Some references - https://github.com/fluent/fluent-logger-java/issues/72
https://github.com/komamitsu/fluency
这篇关于如何在springboot中使fluent-logger-java与logback appender一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!