Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logstash/Logback setup using Spring-boot configuration

I am trying to programmatically set up logstash with logback in my Spring boot project in order to set it up using the environment.

In order to do so, I use a simple @Configuration bean (simplified code, I will then setup host/port using environment) :

@Component
@Slf4j
public class LogstashConfiguration {

    private LogstashTcpSocketAppender logstashTcpSocketAppender;

    @PostConstruct
    public void init() {
        Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        LoggerContext loggerContext = rootLogger.getLoggerContext();
        //loggerContext.reset(); // shouldn't need to use that

        logstashTcpSocketAppender = new LogstashTcpSocketAppender();
        logstashTcpSocketAppender.setName("logstash");
        logstashTcpSocketAppender.setContext(loggerContext);
        logstashTcpSocketAppender.setRemoteHost("localhost");
        logstashTcpSocketAppender.setPort(4560);

        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setContext(loggerContext);
        encoder.start();

        logstashTcpSocketAppender.setEncoder(encoder);
        logstashTcpSocketAppender.start();

        rootLogger.addAppender(logstashTcpSocketAppender);
        rootLogger.setLevel(Level.ALL);
        rootLogger.warn("TEST");

}

I can see that the TcpSocketAppender is logging on my logstash server, however I never receive the TEST log. But I can see "TEST" in my console.

Also if a setup the logger as mentioned in https://github.com/logstash/logstash-logback-encoder#tcp it also works.

Do you have any suggestion to make that work ?

Thank you,

Fabrice

like image 499
FabriceD Avatar asked Jun 30 '15 16:06

FabriceD


1 Answers

Actually the appender was correctly binded to the logger, but connection to logstash failed with an SocketException (broken pipe).

To solve it, I just needed to use LogstashEncoder instead of PatternLayoutEncoder.

like image 95
FabriceD Avatar answered Nov 15 '22 09:11

FabriceD