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