This hostname requirement in Logs line or logs path required when logs collector like filebeat or logstash etc are sending the logs line to centralize logging location or all the servers are mounted with one drive where all logs are forwarding. if the logs path or logs line are having the host name then easily troubleshoot the issue by checking the hostname or server.
Solutions
Based on your requirement you can configure the HostName for logs, Here you will learn about the log4j/Log4j2 and tomcat logging configuration.
Log4j/Log4j2 HostName Configuration
Latest version of log4j/Log4j2 is having the property for HostName. You can print the host name as part of log line or also configure the log file name or folder path with host name.
Configuration for log line:
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m${hostname}%n" />
Configuration for log file name:
<param name="file" value="C:\\Users\\FacingIssuesOnIT\\App-${hostname}.log" />
Configuration for log file folder path:
<param name="file" value="C:\\Users\\FacingIssuesOnIT\\${hostname}\\App.log" />
Complete Example

Note: hostname property by default configured by log4j/log4j2, In case You can set any other property also you can set in your main method or application initializer as below:
System.setProperty("HostName", InetAddress.getLocalHost().getHostName());
Tomcat Logging Host Name Configuration
Follow below step to configure host name in tomcat:
- Add line of statement in your /conf/catalina.properties
JAVA_OPTS="${JAVA_OPTS} -Dhostname=$HOSTNAME"
Here the property -Dhostname will pass as additional parameter while run tomcat.
- Update your conf/ logging.properties for replacing the statement
from
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
to
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/${hostname}/logs
- In case want to print hostname as logs statement then update server.xml as below:
<Valve className="org.apache.catalina.valves.AccessLogValue" directory="C:\FacingIssuesOnIT\${hostname} prefix="localhost_access_log" suffix=".log" pattern="%h %l %u %t " %r " ${hostname} %s %b" />
Conclusion
In this post you learn about the hostname/IP printing in logs statement and configuring the path.
References
- https://stackoverflow.com/questions/68134391/change-tomcat-logging-location-to-custom-location-with-hostname-as-a-directory
- https://stackoverflow.com/questions/36170962/how-to-append-hostname-to-log-file-in-log4j-xml
Related Posts
Your Feedback Motivate Us
If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.
Happy Learning !!!
You must log in to post a comment.