[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

WindowsのApacheのログローテートでログを削除する方法

インフラエンジニアの伊藤です。

LinuxのApacheのログのローテーション設定については、logrotateで行っています。

ログローテーションについてはこちらをご参照ください。
ログローテーション(logrotate)を使ってみる ( httpd(apache)の設定例 ) | レンタルサーバー・自宅サーバー設定・構築のヒント

そういえばWindowsのログローテーションってどうやってるんだろう、と調べてみると、
WindowsのApacheに付属している「rotatelogs.exe」でログのローテーションをしていますが、削除は出来ないようです。

ローテーション後の古いログの削除方法についてです。

Apacheのログ設定を確認する

Apacheのログファイルの出力設定を「httpd.conf」にて設定します。
ログの出力を1日ごとに設定変更します。

#元々の設定
   ErrorLog "|bin/rotatelogs.exe logs/error-%Y%m%d-%H.log 10M -l"
   CustomLog "|bin/rotatelogs.exe logs/access-%Y%m%d-%H.log 10M -l" common

#設定変更後
   ErrorLog "| bin/rotatelogs.exe logs/error_%Y%m%d.log 86400"
   CustomLog "| bin/rotatelogs.exe logs/access_%Y%m%d.log 86400" common

元々の設定が「ログが10MBに達するとローテーション」となっているので、
「86400秒(1日)経過するごとにローテーション」の設定にします。

Linuxであれば設定をしてあげればlogrotateがログの削除までしてくれますが、
Windowsの場合はここから一工夫が必要になります。

バッチファイルを作成する

バッチファイルを作成し、バッチを定期的に実行することで、ローテーションされた古いファイルを削除することが可能です。

forfiles /P "D:\Apache\logs" /D -7 /C "cmd /c del @file"

「forfiles」コマンドで、条件に合うファイルを抽出します。
各引数の意味については以下になります。

引数 説明
/P 対象のパス
/D ファイルの更新日付に基いて抽出(-7なら7日前)
/C 出力したファイルに対して、別コマンドを実行する(この場合はcmdコマンド)
@file 抽出したファイルの変数(@extなら抽出したファイルの拡張子を表す変数となる)

このようなバッチファイルを作成してタスクスケジューラに登録することで、
ローテーションしたファイルから、x日前のファイルを消すことが可能になります。
また、delコマンドでなく圧縮のコマンド(7zip等が必要になりますが)を使うことで、
古いログを圧縮することも可能です。組み合わせは無限大ですね。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
1
読み込み中...
1 票, 平均: 1.00 / 11
20,357
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author