LAMP--Apache 日誌切割

來源:互聯網
上載者:User

標籤:apache   日誌切割   

       在配置了日誌的情況下,我們每訪問一次網站,就會記錄若干條日誌。日誌如果不去管理,時間長了記錄檔會越來越大,大到不可能用 cat、less 以及 vim 開啟的,head 和 tail 還可以。為了避免產生這麼大的記錄檔,apache有相關的配置,使日誌按照我們的需求進行歸檔,比如每天或每小時一個新日誌。

[[email protected] ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

       開啟apache的虛擬機器主機設定檔,可以看到樣本設定檔:

#<VirtualHost *:80>#    ServerAdmin [email protected]#    DocumentRoot "/usr/local/apache2/docs/dummy-host.example.com"#    ServerName dummy-host.example.com#    ServerAlias www.dummy-host.example.com#    ErrorLog "logs/dummy-host.example.com-error_log"#    CustomLog "logs/dummy-host.example.com-access_log" common#</VirtualHost>

       其中的 ErrorLog 是錯誤記錄檔,CustomLog 是訪問日誌。雙引號裡的路徑是 針對 /usr/local/apache2/ 目錄的相對路徑,可以通過這段配置設定日誌名稱,common是日誌的格式。這種方式是固定組建記錄檔的,會造成上面說的記錄檔過大,那麼我們需要對現有的主機配置做如下操作。

       在對應的虛擬機器主機設定檔中加入ErrorLog和CustomLog兩行:

<VirtualHost *:80>    DocumentRoot "/data/www"    ServerName www.123.com    ServerAlias www.test.com    ErrorLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/123.com-error_%Y%m%d_log 86400"    CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/123.com-access_%Y%m%d_log 86400" combined  <Directory /data/www/admin.php>   AllowOverride AuthConfig   AuthName "Please input the passwd"

       雙引號中,最前面的豎線就是管道符,意思是把產生的日誌傳給 rotatelogs ,這個工具是 apache 內建的切割日誌的工具。 -l 的作用是校準時區為 UTC,北京時間。最後面的 86400 ,單位是秒,意思是一天。雙引號外的 combined 為日誌格式,關於日誌格式在 /usr/local/apache2/conf/httpd.conf 裡面定義。

[[email protected] ~]# grep LogFormat /usr/local/apache2/conf/httpd.conf    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined    LogFormat "%h %l %u %t \"%r\" %>s %b" common      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio

     設定好切割配置後,重啟apache

[[email protected] ~]# /usr/local/apache2/bin/apachectl -tSyntax OK[[email protected] ~]# /usr/local/apache2/bin/apachectl restart

     用瀏覽器訪問對應網站後,查看記錄檔

[[email protected] ~]# ls /usr/local/apache2/logs/123.com-access_20160518_log         dummy-host.example.com-access_logaccess_log                          dummy-host.example.com-error_logdummy-host2.example.com-access_log  error_logdummy-host2.example.com-error_log   httpd.pid

     已經出現對應日誌,更改下時間,再瀏覽測試一下

[[email protected] ~]# date -s "2016-05-19 09:00:00"2016年 05月 19日 星期四 09:00:00 CST[[email protected] ~]# ls /usr/local/apache2/logs/123.com-access_20160518_log         dummy-host.example.com-access_log123.com-access_20160519_log         dummy-host.example.com-error_logaccess_log                          error_logdummy-host2.example.com-access_log  httpd.piddummy-host2.example.com-error_log

     又產生了新的記錄檔,切割配置成功。


本文出自 “散宜生的學習筆記” 部落格,請務必保留此出處http://sanyisheng.blog.51cto.com/11154168/1795813

LAMP--Apache 日誌切割

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.