How to make fluent-logger-java work with logback appender in springboot

风流意气都作罢 提交于 2019-12-13 02:13:25

问题


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.


回答1:


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/sndyuk/logback-more-appenders/blob/master/src/main/java/ch/qos/logback/more/appenders/FluencyLogbackAppender.java

https://github.com/komamitsu/fluency



来源:https://stackoverflow.com/questions/50464458/how-to-make-fluent-logger-java-work-with-logback-appender-in-springboot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!