Change Tomcat logging location to custom location with hostname as a directory

Viewed 337

Change Tomcat logging location to custom location with hostname as directory example: if the server hostname is "frontend" tomcat logs directory should be: frontend/logs/

Which property must be defined in tomcat logging.properties ?? to fetch hostname ?

Edit (from deleted "answer"): frontend is the name of the machine running Tomcat. The OS is Ubuntu-18.04.

1 Answers

The ClassLoaderLogManager, which reads the logging configuration in Tomcat has a limited placeholder ${...} replacement capability. It can replace:

  • Java system properties,
  • the three virtual properties classloader.webappName (context name of the application), classloader.hostName (name of the <Host> element), classloader.serviceName (name of the <Service> element).

Since no Java property contains the hostname, the easiest way to use it in logging.properties is to hardcode it.

Otherwise you need to create a Java system property containing the hostname (e.g. hostname) by passing a -Dhostname=... JVM option to Tomcat. On Debianoids you can add the switch in /etc/default/tomcat9:

JAVA_OPTS="${JAVA_OPTS} -Dhostname=$HOSTNAME"

Then you can use the system property defined above in logging.properties, e.g. you can replace:

1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs

with

1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/${hostname}/logs
Related