16
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Tomcatのログ出力をlog4j2に置き換える手順

Last updated at Posted at 2015-01-15

Tomcat8.0.15で動作確認しました。

logging.propertiesのリネーム/削除

{TOMCAT_HOME}/conf/logging.propertiesを削除または拡張子を変更します。

Tomcat extrasライブラリのダウンロード

以下のライブラリを適当なミラーサーバからダウンロードします。
ここではjaistを選択します。

http://ftp.jaist.ac.jp/pub/apache/tomcat/tomcat-8/v8.0.15/bin/extras/tomcat-juli-adapters.jar
http://ftp.jaist.ac.jp/pub/apache/tomcat/tomcat-8/v8.0.15/bin/extras/tomcat-juli.jar

ライブラリの配置

tomcat-juli-adapters.jar => {TOMCAT_HOME}/lib/tomcat-juli-adapters.jar
tomcat-juli.jar => {TOMCAT_HOME}/bin/tomcat-juli.jar (既存のファイルを置き換えます)

log4j2ライブラリのダウンロードと配置

以下からバイナリのアーカイブをダウンロードします。
http://www.apache.org/dyn/closer.cgi/logging/log4j/2.1/apache-log4j-2.1-bin.zip

解凍したディレクトリにある以下のjarファイルを {TOMCAT_HOME}/lib/ 配下に置きます。

  • log4j-1.2-api-2.1.jar
  • log4j-api-2.1.jar
  • log4j-core-2.1.jar

log4j2.xmlの作成と配置

log4j2.xmlを作成して {TOMCAT_HOME}/lib/ や {TOMCAT_HOME}/conf/ に置きます。

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
    <Properties>
        <Property name="pattern">[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5p %-24c{1}- %m%n</Property>
    </Properties>
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="${pattern}" />
        </Console>
        <RollingFile name="catalina" fileName="/path/to/logs/catalina.log" filePattern="/path/to/logs/catalina_%d{yyyy-MM-dd}.gz">
            <PatternLayout pattern="${pattern}" />
            <Policies><TimeBasedTriggeringPolicy /></Policies>
        </RollingFile>
        <RollingFile name="localhost" fileName="/path/to/logs/localhost.log" filePattern="/path/to/logs/localhost_%d{yyyy-MM-dd}.gz">
            <PatternLayout pattern="${pattern}" />
            <Policies><TimeBasedTriggeringPolicy /></Policies>
        </RollingFile>
        <RollingFile name="manager" fileName="/path/to/logs/manager.log" filePattern="/path/to/logs/manager_%d{yyyy-MM-dd}.gz">
            <PatternLayout pattern="${pattern}" />
            <Policies><TimeBasedTriggeringPolicy /></Policies>
        </RollingFile>
        <RollingFile name="host-manager" fileName="/path/to/logs/host-manager.log" filePattern="/path/to/logs/host-manager_%d{yyyy-MM-dd}.gz">
            <PatternLayout pattern="${pattern}" />
            <Policies><TimeBasedTriggeringPolicy /></Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]" level="info">
            <AppenderRef ref="console" />
            <AppenderRef ref="localhost"/>
        </Logger>
        <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]" level="info" additivity="false">
            <AppenderRef ref="console" />
            <AppenderRef ref="manager"/>
        </Logger>
        <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]" level="info" additivity="false">
            <AppenderRef ref="console" />
            <AppenderRef ref="host-manager"/>
        </Logger>
        <Root level="info">
            <AppenderRef ref="console" />
            <AppenderRef ref="catalina" />
        </Root>
    </Loggers>
</Configuration>

環境変数JAVA_OPTSの設定

{TOMCAT_HOME}/bin/setenv.sh(Windowsの場合はsetenv.bat)に以下を追加します。(ない場合は新規作成)

# setenv.sh
JAVA_OPTS="$JAVA_OPTS -Dlog4j.configurationFile=file:///path/to/log4j2.xml"
:: setenv.bat
@SET JAVA_OPTS=%JAVA_OPTS% -Dlog4j.configurationFile=file://C:/path/to/log4j2.xml

Tomcatを起動するとログがlog4j2で出力されているのがわかります。

[2015-01-15 14:35:37.518] INFO  HostConfig              - Deployment of web application directory C:\server\apache-tomcat-8.0.15\webapps\examples has finished in 547 ms
[2015-01-15 14:35:37.518] INFO  HostConfig              - Webアプリケーションディレクトリ C:\server\apache-tomcat-8.0.15\webapps\host-manager を配備します
[2015-01-15 14:35:37.655] INFO  TldScanner              - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of J
ARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[2015-01-15 14:35:37.664] INFO  HostConfig              - Deployment of web application directory C:\server\apache-tomcat-8.0.15\webapps\host-manager has finished in 146 ms
[2015-01-15 14:35:37.664] INFO  HostConfig              - Webアプリケーションディレクトリ C:\server\apache-tomcat-8.0.15\webapps\manager を配備します
[2015-01-15 14:35:37.842] INFO  TldScanner              - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of J
ARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[2015-01-15 14:35:37.848] INFO  HostConfig              - Deployment of web application directory C:\server\apache-tomcat-8.0.15\webapps\manager has finished in 184 ms
[2015-01-15 14:35:37.848] INFO  HostConfig              - Webアプリケーションディレクトリ C:\server\apache-tomcat-8.0.15\webapps\ROOT を配備します
[2015-01-15 14:35:37.990] INFO  TldScanner              - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of J
ARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[2015-01-15 14:35:37.997] INFO  HostConfig              - Deployment of web application directory C:\server\apache-tomcat-8.0.15\webapps\ROOT has finished in 149 ms
[2015-01-15 14:35:38.000] INFO  Http11AprProtocol       - Starting ProtocolHandler ["http-apr-8080"]
[2015-01-15 14:35:38.036] INFO  AjpAprProtocol          - Starting ProtocolHandler ["ajp-apr-8009"]
[2015-01-15 14:35:38.038] INFO  Catalina                - Server startup in 1704 ms

以上。

16
18
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?