使用cronolog 分割Tomcat日誌 Apache日誌


1. 安裝cronolog

官網下載:http://cronolog.org/usage.html


./configure

make;make install


默認安裝位置:


# which cronolog

/usr/local/sbin/cronolog


2. 配置分割Tomcat日誌

編輯tomcat目錄bin下的catalina.sh文件


# vi bin/catalina.sh


找到下面這行


elif [ "$1" = "start" ] ; then


      ……


      org.apache.catalina.startup.Bootstrap "$@" start \

      >> "$CATALINA_OUT" 2>&1 &


在這個elif語句中類似這樣的行有2處,第一處是tomcat時帶“-security”參數的啓動,第二處是默認tomcat啓動方式,也就是else下面的那部分,只修改這裏就可以。爲了都使用cronolog切割,我們兩處都修改。


另外還要把touch “$CATALINA_OUT"這行註釋掉。


完整的修改如下:


#  touch "$CATALINA_OUT"

  if [ "$1" = "-security" ] ; then

    if [ $have_tty -eq 1 ]; then

      echo "Using Security Manager"

    fi

    shift

    "$_RUNJAVA" "$LOGGING_CONFIG" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \

      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \

      -Djava.security.manager \

      -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \

      -Dcatalina.base="$CATALINA_BASE" \

      -Dcatalina.home="$CATALINA_HOME" \

      -Djava.io.tmpdir="$CATALINA_TMPDIR" \

      org.apache.catalina.startup.Bootstrap "$@" start | /usr/local/sbin/cronolog /usr/local/tomcat/logs/catalina.%Y-%m-%d.out >> /dev/null &

#      >> "$CATALINA_OUT" 2>&1 &


  else

    "$_RUNJAVA" "$LOGGING_CONFIG" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \

      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \

      -Dcatalina.base="$CATALINA_BASE" \

      -Dcatalina.home="$CATALINA_HOME" \

      -Djava.io.tmpdir="$CATALINA_TMPDIR" \

      org.apache.catalina.startup.Bootstrap "$@" start 2>&1 | /usr/local/sbin/cronolog /usr/local/tomcat/logs/catalina.%Y%m%d.out >> /dev/null &

#      >> "$CATALINA_OUT" 2>&1 &


  fi


保存退出


重啓Tomcat服務


[root@VM tomcat]# bin/shutdown.sh 

Using CATALINA_BASE:   /usr/local/tomcat

Using CATALINA_HOME:   /usr/local/tomcat

Using CATALINA_TMPDIR: /usr/local/tomcat/temp

Using JRE_HOME:        /usr/java/jdk1.6.0_29

Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar

[root@VM tomcat]# bin/startup.sh 

Using CATALINA_BASE:   /usr/local/tomcat

Using CATALINA_HOME:   /usr/local/tomcat

Using CATALINA_TMPDIR: /usr/local/tomcat/temp

Using JRE_HOME:        /usr/java/jdk1.6.0_29

Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar


查看日誌目錄是否生成catalina.yymmdd.out的日誌文件


-rw-r--r-- 1 root root 10537 Jul 30 10:50 catalina.20140730.out


配置cronolog完成了,觀察每天是否有一個新的catalina.yymmdd.out的日誌文件生成,定期刪除日期較舊的日誌文件。


注意,當catalina.out日誌有新內容時cronolog纔會產生新日誌文件,而不是定點生成。


3. 分割Apache日誌

編輯apache的默認配置文件httpd.conf,找到Apache日誌位置,修改如下(本例是按照每天分割):


ErrorLog "|/usr/local/sbin/cronolog /var/log/httpd/error_%Y-%m-%d.log"


CustomLog "|/usr/local/sbin/cronolog /var/log/httpd/access_%Y-%m-%d.log" combined


保存


重啓apache服務


# /etc/init.d/httpd restart

Stopping httpd: [  OK  ]

Starting httpd: [  OK  ]


注意:Apache的日誌目錄權限默認是700,只能生成error_xxxxxx.log日誌,無法生產access_xxxxxx.log日誌,我們需要給/var/log/httpd目錄賦予other用戶w權限


# chmod o+w /var/log/httpd


可以看見/var/log/httpd目錄中生成了access_xxxxxx.log日誌。


知識點:配置了cronolog,它並不會像定時那樣在指定的時間到了就生成新日誌,它生成日誌的前提條件只有在日誌中有了新內容後纔會創建新日誌文件,沒有日誌內容更新不會創建新日誌。


3.1 測試


若想很快看到效果,可以按照下面的配置,週期設定爲1分鐘,新日誌文件命名加上小時分鐘


CustomLog "|/usr/local/sbin/cronolog --period=1minutes /var/log/httpd/access_%Y-%m-%d-%H%M.log" combined


重啓Apache服務,使之生效


然後訪問http頁面,使新日誌內容出現,只要有日誌更新,就會每分鐘生成一個新的access_xxxxxx.log文件,沒有日誌更新不會生成新日誌。


-rw-r--r-- 1 root root 17280 Aug  2 01:10 access_2014-08-02-0100.log

-rw-r--r-- 1 root root 28620 Aug  2 01:13 access_2014-08-02-0110.log

-rw-r--r-- 1 root root  2160 Aug  2 01:18 access_2014-08-02-0115.log


附錄

cronolog使用語法:


CustomLog "|/path/to/cronolog [OPTIONS] logfile-spec"[format]


常用options


--period=時間


單位必須指明,可用的單位包括 seconds, minutes, hours, days, weeks, months.


常用format


SpecifierDescription

%%a literal%character

%na new-line character

%ta horizontal tab character

Time fields

%Hhour (00..23)

%Ihour (01..12)

%pthe locale's AM or PM indicator

%Mminute (00..59)

%Ssecond (00..61, which allows for leap seconds)

%Xthe locale's time representation (e.g.: "15:12:47")

%Ztime zone (e.g. GMT), or nothing if the time zone cannot be determined

Date fields

%athe locale's abbreviated weekday name (e.g.: Sun..Sat)

%Athe locale's full weekday name (e.g.: Sunday .. Saturday)

%bthe locale's abbreviated month name (e.g.: Jan .. Dec)

%Bthe locale's full month name, (e.g.: January .. December)

%cthe locale's date and time (e.g.:"Sun Dec 15 14:12:47 GMT 1996")

%dday of month (01 .. 31)

%jday of year (001 .. 366)

%mmonth (01 .. 12)

%Uweek 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)

%Wweek 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)

%wday of week (0 .. 6, where 0 corresponds to Sunday)

%xlocale's date representation (e.g. today in Britain: "15/12/96")

%yyear without the century (00 .. 99)

%Yyear with the century (1970 .. 2038)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章