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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With