1

On my debian server I have multiple tomcat installations. With version 8 this worked 100% now I'm upgrading to version 9 and the second instance of tomcat is logging to the default catalina.out file. I would like it to log to its own location. I tried to change the logging in logging.proprties but that seems not to work. The localhost and catalina rollover logs are in the right place, so the second instance has its own.

Here is my logging.properties of my second instance:

1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
1catalina.org.apache.juli.AsyncFileHandler.maxDays = 90

2localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
2localhost.org.apache.juli.AsyncFileHandler.maxDays = 90

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.SystemdFormatter

I tried adding:

java.util.logging.ConsoleHandler.directory = ${catalina.base}/logs

But that did not seem to help.

So anyone knows how to config the location so the catalina.out goes to the catalina.base/logs location?

In reaction to question: I have installed tomcat9 with apt install tomcat9 and the instance with tomcat9-instance-create -c 8009 staging

This is my tomcat9_staging.service to startup with service tomcat9_staging start/restart

#
# Systemd unit file for Apache Tomcat
#

[Unit]
Description=Apache Tomcat 9 Web Application Server - For Staging
Documentation=https://tomcat.apache.org/tomcat-9.0-doc/index.html
After=network.target
RequiresMountsFor=/var/log/tomcat9 /var/lib/tomcat9

[Service]

# Configuration
Environment="CATALINA_HOME=/usr/share/tomcat9"
Environment="CATALINA_BASE=/app/tomcat9/staging"
Environment="CATALINA_TMPDIR=/tmp"
Environment="JAVA_OPTS=-Djava.awt.headless=true"

# Lifecycle
Type=simple
ExecStartPre=+/usr/libexec/tomcat9/tomcat-update-policy.sh
ExecStart=/bin/sh /usr/libexec/tomcat9/tomcat-start.sh
SuccessExitStatus=143
Restart=on-abort

# Logging
SyslogIdentifier=tomcat9_staging

# Security
User=tomcat
Group=tomcat
PrivateTmp=yes
AmbientCapabilities=CAP_NET_BIND_SERVICE
NoNewPrivileges=true
CacheDirectory=tomcat9_staging
CacheDirectoryMode=750
ProtectSystem=strict
ReadWritePaths=/app/tomcat9/staging/
ReadWritePaths=/etc/tomcat9/Catalina/
ReadWritePaths=/var/lib/tomcat9/webapps/
ReadWritePaths=/var/log/tomcat9/

[Install]
WantedBy=multi-user.target

the tomcat-start.sh:

#!/bin/sh
#
# Startup script for Apache Tomcat with systemd
#

set -e

# Load the service settings
. /etc/default/tomcat9

# Find the Java runtime and set JAVA_HOME
. /usr/libexec/tomcat9/tomcat-locate-java.sh

# Set the JSP compiler if configured in the /etc/default/tomcat9 file
[ -n "$JSP_COMPILER" ] && JAVA_OPTS="$JAVA_OPTS -Dbuild.compiler=\"$JSP_COMPILER\""

export JAVA_OPTS

# Enable the Java security manager?
SECURITY=""
[ "$SECURITY_MANAGER" = "true" ] && SECURITY="-security"


# Start Tomcat
cd $CATALINA_BASE && exec $CATALINA_HOME/bin/catalina.sh run $SECURITY

The problem seems to be that the startup script runs tomcat with run and not with start.

there is a diff between start and run. with start the eval will be:

eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER "$JAVA_OPTS" "$CATALINA_OPTS" \
  -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \
  -classpath "\"$CLASSPATH\"" \
  -Djava.security.manager \
  -Djava.security.policy=="\"$CATALINA_BASE/policy/catalina.policy\"" \
  -Dcatalina.base="\"$CATALINA_BASE\"" \
  -Dcatalina.home="\"$CATALINA_HOME\"" \
  -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
  org.apache.catalina.startup.Bootstrap "$@" start \
  >> "$CATALINA_OUT" 2>&1 "&"

with run:

eval exec "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER "$JAVA_OPTS" "$CATALINA_OPTS" \
  -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \
  -classpath "\"$CLASSPATH\"" \
  -Djava.security.manager \
  -Djava.security.policy=="\"$CATALINA_BASE/policy/catalina.policy\"" \
  -Dcatalina.base="\"$CATALINA_BASE\"" \
  -Dcatalina.home="\"$CATALINA_HOME\"" \
  -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
  org.apache.catalina.startup.Bootstrap "$@" start

Not sure what the idea behind this is but the result is a diff in where the logging goes.

My problem now is that via the service it will not startup with start only with run

So why is run not setup to log the same as start is ?

tibi
  • 657
  • 1
  • 10
  • 22
  • `run` is meant for running Tomcat in the foreground - in which case you want to see the logs in the console window - and `start` is meant to run it in the background - in which case there is no console, so the logs need to be redirected somewhere else. – Konrad Botor Jul 28 '20 at 08:43
  • See my latest edit for another possible solution. – Konrad Botor Jul 28 '20 at 09:05

1 Answers1

1

java.util.logging.ConsoleHandler.directory = ${catalina.base}/logs did not work,. because ConsoleLogger always logs to console, so setting on directory is meaningless.

catalina.out is created by a redirect in catalina.sh as explained in the answer to this question. It's possible that the contents of this file have changed between Tomcat 8 and 9. You should check and edit it as needed.

Edit:

I think I know what’s going on. The script used by your setup to start Tomcat calls catalina.sh run.

According to the source code for Tomcat 9.x if you start Tomcat that way system variable CATALINA_OUT is ignored and the output is sent to standard output and standard error files.

Change run to start and it should work the way you want it to and create file catalina.out in the specified directory.

Edit 2:

I found this article about running Tomcat using systemd and this question about using systemd directly to redirect stdout and stderr to files. I hope they'll prove helpful.

Redirecting can be as simple as adding this:

StandardOutput=append:/app/log/tomcat9_staging_catalina.out
StandardError=append:/app/log/tomcat9_staging_error.out
ReadWritePaths=/app/log/

to: /lib/systemd/system/tomcat9_staging.service

tibi
  • 657
  • 1
  • 10
  • 22
Konrad Botor
  • 4,765
  • 1
  • 16
  • 26
  • it still states/: CATALINA_OUT Default is $CATALINA_BASE/logs/catalina.out strange – tibi Jul 27 '20 at 13:19
  • It is strange. Have you tried setting `CATALINA_OUT` manually before starting the second Tomcat instance? – Konrad Botor Jul 27 '20 at 13:24
  • I have logged it and it is fine: /app/tomcat9/staging/logs/catalina.out (staging is my second instance) although nothing is in that location. – tibi Jul 27 '20 at 13:26
  • How do you start your Tomcat? – Konrad Botor Jul 27 '20 at 13:32
  • from the service (service tomcat9_staging restart) – tibi Jul 27 '20 at 13:54
  • in the tomcat9_staging.service i have also added the readwritepath like this: (ReadWritePaths=/app/tomcat9/staging/) – tibi Jul 27 '20 at 13:58
  • Please paste the entire contents of `tomcat9_staging.service` in the question. – Konrad Botor Jul 27 '20 at 13:59
  • This odes not appear to be a standard Tomcat startup file: /usr/libexec/tomcat9/tomcat-start.sh? What does it do? – Konrad Botor Jul 27 '20 at 14:16
  • It is installed by apt-get I only copied it and changed it to add 'staging' my copy of tomcat (which is made by tomcat9-instance-create -c 8009 staging ). I will add the tomcat-start.sh to the question – tibi Jul 27 '20 at 17:38
  • i just wanted to post that i see a diff between run and start :) thanks for your help!! – tibi Jul 27 '20 at 18:36
  • I added the place where it diffs between start and run, but the credit goes to you :) – tibi Jul 27 '20 at 18:39
  • hmmm was happy to soon because now it does not startup. catalina.out is created in the right place but empty and the service does not start... investigate further... – tibi Jul 27 '20 at 18:46
  • probably because run will just run tomcat and start will create a new process – tibi Jul 27 '20 at 18:48
  • I was happy to help. I think you need to adjust `Lifecycle` part of the systemd configuration. I would try setting `CATALINA_PID` under `Configuration` and specifying `PIDFile` or attempt to configure it as `Type=oneshot`. See https://www.freedesktop.org/software/systemd/man/systemd.service.html for more details and of course feel free to ask more questions on this website. – Konrad Botor Jul 27 '20 at 19:17
  • So basically the question comes down to how to redirect logging with 'start' like it is done with 'run' – tibi Jul 28 '20 at 08:35
  • The article from edit 2 is interesting it seems that I should nut trust al the startup files: "And let us mention another horror in catalina.sh: the logging. The dæmon process has its standard output and standard error redirected to a log file. This is a known-bad way to go about logging. One cannot size-cap the log file; ... – tibi Jul 28 '20 at 09:28
  • ...one cannot rotate the log file; one cannot shrink the log file. It grows, as one file, forever until the dæmon is shut down. Again, this is ridiculous when we have systemd. systemd can send standard output and standard error to multiple log files that — whatever else one may think of them — are most definitely size capped and rotatable." – tibi Jul 28 '20 at 09:28
  • Yes, it seems if you’re running Tomcat from systemd, you shouldn’t be using `catalina.out`. Although to be fair, with Tomcat 9 you can use `CATALINA_OUT_CMD` to provide some external command that is capable of creating rotatable logs. But why complicate things if systemd can already manage that for you? – Konrad Botor Jul 28 '20 at 09:37
  • 1
    in the end i just left everything the way it was but added StandardOutput=file:/app/log/tomcat9_staging_catalina.out StandardError=file:/app/log/tomcat9_staging_error.out (see my edit to your post) – tibi Jul 28 '20 at 12:46