I would like to log all the fields of a few POJOs in my Spring Boot application, for various use cases.
In my application, I am using logstash-logback-encoder
(7.3) with the following basic logback.xml
in my Spring Boot application.
<configuration>
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<root level="info">
<appender-ref ref="console" />
</root>
</configuration>
As of now, I have done the following:
private static final Logger log = LoggerFactory.getLogger(MyClass.class);
log.info(Markers.append("id", myPojo.getId())
.and(Markers.append("name", myPojo.getName()))
.., "pojo details");
I am getting the fields in the log now as follows:
{
...
"id": "1234",
"name": "test",
...
}
But the problem with this approach is: that it’s not scalable, I mean if I have a POJO with a long list of fields, it would become a long list of Markers.append()
.
So, I am wondering if logstash-logback-encoder
provides any utility to achieve the same. Thanks.