%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %X{requestId} %msg%n%exception{full}
in this pattern
Can I send to logstash over TCP in json format?
I don't want to do xml configuration. I will make all the adjustments with java code
Will this code do what I want? How do I make JsonProviders?
LogstashEncoder encoder = new LogstashEncoder();
encoder.setContext(loggerContext);
encoder.setProviders(new LogStashProviders());
encoder.start();
LogstashTcpSocketAppender appender = new LogstashTcpSocketAppender();
appender.setContext(loggerContext);
appender.addDestination("localhost:9200");
appender.setEncoder(encoder);
appender.start();
//-- class
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.StackTraceElementProxy;
import com.fasterxml.jackson.core.JsonGenerator;
import net.logstash.logback.composite.JsonProvider;
import net.logstash.logback.composite.JsonProviders;
import java.io.IOException;
import java.util.List;
public class LogStashProviders extends JsonProviders<ILoggingEvent> {
@Override
public void start() {
super.start();
}
@Override
public void stop() {
super.stop();
}
@Override
public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException {
generator.writeStartObject();
generator.writeStringField("@timestamp", String.valueOf(event.getTimeStamp()));
generator.writeStringField("level", event.getLevel().toString());
generator.writeStringField("logger", event.getLoggerName());
generator.writeStringField("thread", event.getThreadName());
generator.writeStringField("requestId", String.valueOf(event.getMDCPropertyMap().get("requestId")));
generator.writeStringField("message", event.getFormattedMessage());
if (event.getThrowableProxy() != null) {
generator.writeObjectFieldStart("exception");
generator.writeStringField("class", event.getThrowableProxy().getClassName());
generator.writeStringField("message", event.getThrowableProxy().getMessage());
generator.writeArrayFieldStart("stack_trace");
for (StackTraceElementProxy stackTraceElement : event.getThrowableProxy().getStackTraceElementProxyArray()) {
generator.writeStartObject();
generator.writeStringField("class", stackTraceElement.getStackTraceElement().getClassName());
generator.writeStringField("method", stackTraceElement.getStackTraceElement().getMethodName());
generator.writeStringField("file", stackTraceElement.getStackTraceElement().getFileName());
generator.writeNumberField("line", stackTraceElement.getStackTraceElement().getLineNumber());
generator.writeEndObject();
}
generator.writeEndArray();
generator.writeEndObject();
}
generator.writeEndObject();
}
public LogStashProviders() {
super();
}
@Override
public List<JsonProvider<ILoggingEvent>> getProviders() {
return super.getProviders();
}
}