Search code examples
spring-bootlogstash-configurationipv4spring-logback

Logging IPv4 address in the startup logs using logback-spring


Wondering if anyone has been able to successfully log the host's IP address in ipv4 format in the startup logs of a spring-boot application while using logback spring.

I have a spring-boot microservices application and we use logback-spring for logging. I use logstash's LoggingEventCompositeJsonEncoder for formatting the log in json format. One of the requirements we have is to have the host's ipv4 address in the logs

I am able to leverage MDC to put the ipv4 formatted address for every api requests by adding a servlet Filter that intercepts the requests. This just covers for the requests not for the startup/shutdown logs.

Wondering if anyone else has come across this kind of requirement and been able to resolve it. I am able to output the hostname, just not the ipv4 address


Solution

  • This is what I ended up doing. Not sure if there is a better way to do this:

    I wrote java code to find the ipv4 address in the main method of my spring boot Application class. I then set this to a env variable that I reference in my logback-spring xml