Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minazou67/9f8a93474b8e9efc6bc1ae7e1d4e1fd9 to your computer and use it in GitHub Desktop.
Save minazou67/9f8a93474b8e9efc6bc1ae7e1d4e1fd9 to your computer and use it in GitHub Desktop.
How to configure logging and log rotation of Apache and Tomcat on Bitnami Tomcat Stack

How to configure logging and log rotation of Apache and Tomcat on Bitnami Tomcat Stack

Bitnami Tomcat Stack の Apache と Tomcat のロギングとログローテーションの設定方法です。

Environment

  • Debian wheezy 7.11, jessie 8.5
  • tomcatstack-7.0.64-0-linux-x64

Customize the Apache log output content

アパッチのログ出力内容をカスタマイズします。

$ sudo vi /opt/tomcatstack-7.0.64-0/apache2/conf/httpd.conf
<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    # Custom log format
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{User-Agent}i\"" custom

    # Exclude unnecessary request information
    SetEnvIf Request_URI "\.(gif|jpg|jpeg|png|ico|js|css|woff|ttf)$" nolog

    #
    # The location and format of the access logfile (Common Logfile Format).
    # If you do not define any access logfiles within a <VirtualHost>
    # container, they will be logged here.  Contrariwise, if you *do*
    # define per-<VirtualHost> access logfiles, transactions will be
    # logged therein and *not* in this file.
    #
    #CustomLog "logs/access_log" common

    #
    # If you prefer a logfile with access, agent, and referer information
    # (Combined Logfile Format) you can use the following directive.
    #
    #CustomLog "logs/access_log" combined

    # Logged custom format and env
    CustomLog "logs/access_log" custom env=!nolog
</IfModule>

設定ファイルに記述ミスがないかチェックします。

$ sudo /opt/tomcatstack-7.0.64-0/apache2/bin/apachectl -t
Syntax OK

設定内容を反映します。

sudo /opt/tomcatstack-7.0.64-0/apache2/bin/apachectl graceful

Set up the log rotation

Disable the Tomcat log rotation setting

logrotate を使用するため Tomcat 自身のログローテーション設定を無効化します。

$ sudo vi /opt/tomcatstack-7.0.64-0/apache-tomcat/conf/server.xml
<!-- Access log processes all example.
     Documentation at: /docs/config/valve.html
     Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" rotatable="false" prefix="localhost_access" suffix=".log" pattern="%h %l %u %t &quot;%r&quot; %s %b"/>

Tomcat を再起動します。

sudo /opt/tomcatstack-7.0.64-0/ctlscript.sh restart tomcat

Change the settings of the logrotate

Bitnami が提供している 3 つの設定ファイル(apache.conf, mysql.conf, tomcat.conf)の設定内容を変更します。

$ sudo vi /opt/tomcatstack-7.0.64-0/config/logrotate/logrotate.d/*.conf
/opt/tomcatstack-7.0.64-0/apache2/logs/*_log {$
 weekly
 rotate 15
 dateext
 compress
 copytruncate
 missingok
}

tomcat.conf に関しては、状況に応じて処理対象のファイルを変更します。

/opt/tomcatstack-7.0.64-0/apache-tomcat/logs/catalina.out
/opt/tomcatstack-7.0.64-0/apache-tomcat/logs/localhost_access.log {
  …
}

Enable logrotate to create a symbolic link

シンボリックリンクを作成し、logrotate を有効化します。

sudo ln -s /opt/tomcatstack-7.0.64-0/config/logrotate/bitnami.conf /etc/logrotate.d/bitnami-tomcatstack

設定内容を確認します。

sudo logrotate -d /etc/logrotate.d/bitnami-tomcatstack

Run a logrotate test

必要に応じて強制的に logrotate を実行し、テストを実施します。

sudo logrotate -f /etc/logrotate.d/bitnami-tomcatstack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment