使用 cronolog 作 Apache log 的分割處理

使用 Apache, 它的 log 檔 default 是叫 access.log, 是一個連續且不斷長大的檔案, 只要 log 不是關閉狀態, 凡是用戶連向你的網站, 就會有像這樣的一筆記在 log:
192.168.0.2 - - [01/Jul/2005:17:22:04 +0800] "GET /wp/archives/2005/06/27/12/28/41/ HTTP/1.1" 200 9760 "http://www.saab.club.tw/guestbook/guestcontent.asp?TopicID=1363&Board_Level=3" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"
依序是用戶 IP, 時間, 動作, 回覆碼, 回傳資料大小(bytes), referer(從什麼地方連過來), 用戶端瀏覽器資料;後端 log 分析程式可以依此計算出計多有趣的統計.

但是一個連續且不斷長大的 log 檔是不方便使用、管理和備份的, 最好是可以依照某種方法把檔案作分割. cronolog 就是因應這樣的需求而生的, 它可以在 win32 或 unix 平台運作, 這裡可以下載.

使用的方法也很簡單, 只要修改你的 Apache 設定檔 httpd.conf, 把 default 的
CustomLog logs/access.log combined
改成
CustomLog "|c:/bin/cronolog.exe logs/access_%Y%m%d.log" combined
就好了. 其中 c:/bin/cronolog.exe (這個是 win32 用的) 就是你放 cronolog 的目錄, 後面的 logs/access_%Y%m%d.log 是 log 產出的目錄 (logs) 及形式, access_%Y%m%d.log 會生出像 access_20050702.log 這樣的檔名, 每天一個檔案. 別忽略了一開始的 “|” 那是 pipeline 給 cronolog 作處理的意思.

你也許會問: 啊留 Apache 的 log 要作什麼? 用途多者呢, 一來出問題的時候可以查問題, 像上次的盜連事件; 二來可以交給後端的 log 分析程式, 如免費的 awstats, 統計看看有多少人看你的網頁、每天流量、每小時流量比較、他們從哪裡來、用什麼樣的字詞從搜尋引擎搜尋到你的網頁等等等… 有很多好玩的! 例如這個可以看2005年六月份用戶來自國家的統計, 好多奇奇怪怪的國家 🙂 、2005年六月份用戶端瀏覽器的使用率; 嗯,Firefox還是要多加油,一直只在8%~10%左右;.

cronolog 詳細參數意義:

Specifier Description
%% a literal % character
%n a new-line character
%t a horizontal tab character
Time fields
%H hour (00..23)
%I hour (01..12)
%p the locale’s AM or PM indicator
%M minute (00..59)
%S second (00..61, which allows for leap seconds)
%X the locale’s time representation (e.g.: "15:12:47")
%Z time zone (e.g. GMT), or nothing if the time zone cannot be determined
Date fields
%a the locale’s abbreviated weekday name (e.g.: Sun..Sat)
%A the locale’s full weekday name (e.g.: Sunday .. Saturday)
%b the locale’s abbreviated month name (e.g.: Jan .. Dec)
%B the locale’s full month name, (e.g.: January .. December)
%c the locale’s date and time (e.g.: "Sun Dec 15 14:12:47 GMT 1996")
%d day of month (01 .. 31)
%j day of year (001 .. 366)
%m month (01 .. 12)
%U week of the year with Sunday as first day of week (00..53, where week 1 is the week containing the first Sunday of the year)
%W week of the year with Monday as first day of week (00..53, where week 1 is the week containing the first Monday of the year)
%w day of week (0 .. 6, where 0 corresponds to Sunday)
%x locale’s date representation (e.g. today in Britain: "15/12/96")
%y year without the century (00 .. 99)
%Y year with the century (1970 .. 2038)
(Visited 16,342 times, 1 visits today)

在〈使用 cronolog 作 Apache log 的分割處理〉中有 10 則留言

  1. 以前的工作,同事請假時我就要幫忙看這一大堆 web server 的 log, 以前都是直接在 unix 下直接用 vi 看上面那一堆亂七八糟的文字,有看或許等於沒看,根本就是讓人看得眼花撩亂而已。

    時代「一天天」地在進步,現在將 log 檔整理為這種一目瞭然的報表,真是系統管理員的一大福音啊!

    回覆
  2. 請問在我主機底下的虛擬主機~ 我看他們都沒有去做分割的樣式~
    原格式CustomLog “|c:/bin/cronolog.exe logs/access_%Y%m%d.log” combined

    請問還要多打什麼?連虛擬主機的log跟著分割?
    目前只有測試成功access.log 會自己分割而已! 請您指導一下! 

    回覆

發佈回覆給「Mark」的留言 取消回覆