Tomcat9.0.12でコンソールにスタックトレースを出力させる方法

https://ameblo.jp/ill-grasper/entry-11173169674.html
を参考にしました。
Tomcat9とJVM8の組み合わせのデフォルトではスタックトレースがEclipseのコンソールに出力されなかったため上記記事をもとに自分の環境で試した結果を記載しています。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/325048/80224cf8-dea3-804d-9a1a-28ff9f364a9d.png)
が完成形。
※赤文字で「情報」に加えて、「重大」も出るようになってます。

テストサーブレットのコード

```
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		out.println("Hello");
		
		
		throw new ServletException();
		
	}
```


JVM引数はこちらから
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/325048/fbbd9340-57cf-000c-e701-8f0dae046cde.png)

意図的に配置したlogging.properties。Tomcatのzipを解凍後編集なしのデフォルトでOKでした。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/325048/7189aec5-22c1-3ffa-f3b9-8cc5cbcbe13f.png)

次に、log4j周りの設定不備によりTomcat起動にこける場合の対処。
※下記のようなエラーを再現するのに苦労しました。。。

```
07-Apr-2022 22:00:58.667 重大 [main] org.apache.catalina.startup.Catalina.start The required Server component failed to start so Tomcat is unable to start.
 org.apache.catalina.LifecycleException: A child container failed during start
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:960)
	at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:261)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:682)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:350)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:492)
Caused by: java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: A child container failed during start
	at java.util.concurrent.FutureTask.report(FutureTask.java:122)
	at java.util.concurrent.FutureTask.get(FutureTask.java:192)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:951)
	... 13 more
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:960)
	at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
	at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:944)
	... 13 more
Caused by: java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/TomcatTest]]
	at java.util.concurrent.FutureTask.report(FutureTask.java:122)
	at java.util.concurrent.FutureTask.get(FutureTask.java:192)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:951)
	... 21 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/TomcatTest]]
	at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:441)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
	at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:944)
	... 21 more
Caused by: java.lang.NoClassDefFoundError: Lorg/apache/log4j/Logger;
	at java.lang.Class.getDeclaredFields0(Native Method)
	at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
	at java.lang.Class.getDeclaredFields(Class.java:1916)
	at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:110)
	at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:269)
	at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:137)
	at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:69)
	at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:328)
	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:768)
	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5007)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	... 27 more
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1309)
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1138)
	... 40 more

```

対処方法

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/325048/9ec2ace4-a5d2-4060-cfce-8d6f01858020.png)

上記のようにtomcatホームの下のlibフォルダにlog4jのjarファイルをコピーする。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/325048/03a6ad71-1375-d383-214d-376551bc71d2.png)

log4j.propertiesファイルをWEB-INF\classesに配置する。
テスト目的のため。簡単な設定

```
log4j.rootCategory=DEBUG, Appender1
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss.SSS} [%t] %-6p %m%n
```

Servletのソースコードは下記のとおり

```
import org.apache.log4j.Logger;

@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
	
    private Logger log = Logger.getLogger(TestServlet.class);
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		log.info("log4jテスト");
		
		PrintWriter out = response.getWriter();
		out.println("Hello");
	}


```