所以我在 Ubuntu 18.04.2 中使用 Tomcat 9 时遇到了问题。Tomcat 8 运行良好,它将 Java 堆栈跟踪记录到 /var/log/catalina.out。不幸的是,Tomcat 9 仅通过 apt 使用默认 Ubuntu 配置记录 GET 和 POST 请求。
我查了一下,似乎有几个线程如何修复,最近的一个是这个:tomcat 9 中的 catalina.out 在哪里?——但问题是我的 catalina.sh 看起来与这个答案中显示的文件非常不同。
我的 catalina.sh 中类似(但有很大不同)的部分如下。出于某种原因,在 apt 安装中,它看起来不是试图启动 catalina,而是试图找到它的 PID?:
if [ -z "$CATALINA_OUT" ] ; then
CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
fi
[...]
elif [ "$1" = "start" ] ; then
if [ ! -z "$CATALINA_PID" ]; then
if [ -f "$CATALINA_PID" ]; then
if [ -s "$CATALINA_PID" ]; then
echo "Existing PID file found during start."
if [ -r "$CATALINA_PID" ]; then
PID=`cat "$CATALINA_PID"`
ps -p $PID >/dev/null 2>&1
if [ $? -eq 0 ] ; then
echo "Tomcat appears to still be running with PID $PID. Start aborted."
echo "If the following process is not a Tomcat process, remove the PID file and try again:"
ps -f -p $PID
exit 1
else
echo "Removing/clearing stale PID file."
rm -f "$CATALINA_PID" >/dev/null 2>&1
if [ $? != 0 ]; then
if [ -w "$CATALINA_PID" ]; then
cat /dev/null > "$CATALINA_PID"
else
echo "Unable to remove or clear stale PID file. Start aborted."
exit 1
fi
fi
fi
else
echo "Unable to read PID file. Start aborted."
exit 1
fi
else
rm -f "$CATALINA_PID" >/dev/null 2>&1
if [ $? != 0 ]; then
if [ ! -w "$CATALINA_PID" ]; then
echo "Unable to remove or write to empty PID file. Start aborted."
exit 1
fi
fi
fi
fi
fi
[...]
但是,我在“安全”下看到了对 Boostrap 流程的引用。我应该在这里添加这行吗?
注意 - catalina.out.2019-05-07.log 文件正在被写入,但同样,只包含请求而不是错误。
if [ "$1" = "-security" ] ; then
if [ $have_tty -eq 1 ]; then
echo "Using Security Manager"
fi
shift
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 "&" <-- and here?
2\>\&1 \&\& echo \$! \>\"$catalina_pid_file\" \; \} $catalina_out_command "&"
else
eval \{ $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \
-classpath "\"$CLASSPATH\"" \
-Dcatalina.base="\"$CATALINA_BASE\"" \
-Dcatalina.home="\"$CATALINA_HOME\"" \
-Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
org.apache.catalina.startup.Bootstrap "$@" start \
"$CATALINA_OUT" 2>&1 "&" <-- and here?
2\>\&1 \&\& echo \$! \>\"$catalina_pid_file\" \; \} $catalina_out_command "&"
fi
我的日志属性文件确实有控制台日志作为处理程序:
handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler
以下是正在记录的内容。
illa/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0 https://localhost:8444/asWeb/Fitness.jsp
07-May-2019 18:17:52.687 INFO [https-openssl-nio-8444-exec-5] org.restlet.engine.log.LogFilter.afterHandle 2019-05-07 18:17:52 10.0.2.2 - 10.0.2.15 8444 POST /asWeb/r/WebLinks - 200 502 22 113 https://localhost:8444 Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0 https://localhost:8444/asWeb/Fitness.jsp
<<<<<< this is where it should have caught a System.out.println("DEBUG message to log"); <<<<<<<<
07-May-2019 18:17:56.318 INFO [https-openssl-nio-8444-exec-6] org.restlet.engine.log.LogFilter.afterHandle 2019-05-07 18:17:56 10.0.2.2 - 10.0.2.15 8444 POST /asWeb/r/Chart - 200 93 52 3372 https://localhost:8444 Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0 https://localhost:8444/asWeb/Fitness.jsp
07-May-2019 18:17:58.471 INFO [https-openssl-nio-8444-exec-9] org.restlet.engine.log.LogFilter.afterHandle 2019-05-07 18:17:58 10.0.2.2 - 10.0.2.15 8444 POST /asWeb/r/Fitness - 200 - 67 5520 https://localhost:8444 Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0 https://localhost:8444/asWeb/Fitness.jsp
我确实注意到控制台输出的日志处理程序设置为 SystemdFormatter 而不是 SimpleFormatter。我尝试将日志处理程序更改为 Simple,但没有效果。我将其重新设置为 SystemdFormatter。
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = org.apache.juli.SystemdFormatter
经过进一步挖掘,我能够查看 systemd 状态中的异常......
root@ass:/var/log/tomcat9# service tomcat9 status
● tomcat9.service - Apache Tomcat 9 Web Application Server
Loaded: loaded (/lib/systemd/system/tomcat9.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-05-07 19:28:36 CDT; 1h 21min ago
Docs: https://tomcat.apache.org/tomcat-9.0-doc/index.html
Process: 17539 ExecStartPre=/usr/libexec/tomcat9/tomcat-update-policy.sh (code=exited, status=0/SUCCESS)
Main PID: 17543 (java)
Tasks: 42 (limit: 4681)
CGroup: /system.slice/tomcat9.service
└─17543 /usr/lib/jvm/default-java/bin/java -Djava.util.logging.config.file=/var/lib/tomcat9/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.awt.headless=true -XX:+UseG1GC -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.ha
May 07 20:48:25 ass tomcat9[17543]: DEBUG Fitness All called!
May 07 20:48:25 ass tomcat9[17543]: 2019-05-07 20:48:25 10.0.2.2 - 10.0.2.15 8444 POST /asWeb/r/Fitness - 200 - 67 6696 https://localhost:8444 Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv
May 07 20:48:35 ass tomcat9[17543]: 2019-05-07 20:48:35 10.0.2.2 - 10.0.2.15 8444 POST /asWeb/r/Wx - 200 - 115 2260 https://localhost:8444 Mozilla/5.0 (Android 4.4.2; Mobile; rv:66.0) G
May 07 20:49:10 ass tomcat9[17543]: DEBUG Fitness All called!
May 07 20:49:10 ass tomcat9[17543]: 2019-05-07 20:49:10 10.0.2.2 - 10.0.2.15 8444 POST /asWeb/r/Fitness - 200 - 67 3860 https://localhost:8444 Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv
May 07 20:49:51 ass tomcat9[17543]: DEBUG Fitness All called!
May 07 20:49:51 ass tomcat9[17543]: 2019-05-07 20:49:51 10.0.2.2 - 10.0.2.15 8444 POST /asWeb/r/Fitness - 200 - 67 2196 https://localhost:8444 Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv
May 07 20:49:55 ass tomcat9[17543]: 2019-05-07 20:49:55 10.0.2.2 - 10.0.2.15 8444 POST /asWeb/r/MediaServer - 200 - 40 722 https://localhost:8444 Mozilla/5.0 (X11; Ubuntu; Linux x86_64;
使用这个我可以通过使用“模拟” catalina.out
journalctl -u tomcat9 -f
但我希望它在 catalina.out 中,而不必查看日志 - 如果可能的话。
答案1
您需要编辑该文件:/lib/systemd/system/tomcat9.service
注释掉以下行:SyslogIdentifier=tomcat9
并添加以下两行:
StandardOutput=append:/var/log/tomcat9/catalina.out
StandardError=append:/var/log/tomcat9/catalina.out