This article provides steps to get actual server(local) hostname using HOSTNAME environment variable
Problem Description
In case of centralized logging, It is quite useful to include application server hostname in logs to distinugish one server machine logs from another. In this article, we will talk about a quite popular logging framework named Logback along with Spring Boot.
Logging hostname seems a trivial task at first and you can just use HOSTNAME variable (available in Logback context) in log pattern of your logback-spring.xml or logback.xml as below.
%d{yyyy-MM-dd HH:mm:ss.SSS} ${HOSTNAME} %p --- [%t] %-40.40logger{39} : %m%n$%wEx
Logback uses following approach to get hostname of local machine in its ContextUtil class-
InetAddress.getLocalHost().getHostName()
However, you may end up seeing localhost in place of actual hostname (amit-machine.domain.com) in case your machine DNS settings are not configured properly. As we can see, localhost does not mean anything in case of centralized logging context. This usually happens if localhost appears next to loopback address (127.0.0.1) in your /etc/hosts file as follows -
127.0.0.1 localhost amit-machine.domain.com amit-machine.domain.com.
::1 localhost amit-machine.domain.com amit-machine.domain.com.
Solution using changes in /etc/hosts file
One solution would be to just make changes in your /etc/hosts file by moving actual hostname before localhost as shown below.
127.0.0.1 amit-machine.domain.com amit-machine.domain.com. localhost
::1 amit-machine.domain.com amit-machine.domain.com. localhost
However, it may not always be feasible to make changes in /etc/hosts file especially if you are building a service template and would not know what all machines it will end up working on. If you fall in this category, you can try next solution -
Solution using Spring Property (<springProperty>) Tag
This solution is only applicable if you are using Spring Boot along with Logback i.e. you use logback-spring.xml to configure logging. In this approach, we rely on HOSTNAME environment variable that is available by default on Unix/Linux machines. You can check it by executing following command -
echo $HOSTNAME
# Next line is output of above command
amit-machine.domain.com
Idea is simple, we define a Spring property using <springProperty> in context scope as follows -
<!-- Defines a property with name serverName from environment/System property/application property named HOSTNAME in context scope -->
<!-- Please note that property name was changed to serverName to avoid conflicts with HOSTNAME property -->
<springProperty scope="context" name="serverName" source="HOSTNAME" />
And use the above property in your log patter as follows -
# Default value is set to Logback context variable HOSTNAME with help of :- operator
%d{yyyy-MM-dd HH:mm:ss.SSS} ${serverName:-${HOSTNAME}} %p --- [%t] %-40.40logger{39} : %m%n$%wEx
That's about it! You should now be seeing actual hostname instead of localhost.
Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.