DATA 전문가로 가는 길

Tomcat 5.0.x /4.1.x log rotation solutions (톰캣의 catalina.out 로그 관리) 본문

OS

Tomcat 5.0.x /4.1.x log rotation solutions (톰캣의 catalina.out 로그 관리)

EstenPark 2009. 11. 19. 10:49

* cronolo를 이용한 방법


1. cronolog 다운받기

 

http://cronolog.org

위 사이트에 접속하여, 적절한 버전을 다운로드 한다.

 

2. compile  및 설치

 

# tar xzvf cronolog-1.6.1.tar.gz   또는  gzip d FileName

# cd cronolog-1.6.1

# ./configure --prefix=/usr/local/cronolog

# make;make install

 

3. TOMCAT 설정

 


경로 : /usr/local/apache-tomcat-4.1.37/bin

설정 변경 확인 vi catalina.sh 

 

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

 

  shift

  #touch "$CATALINA_OUT"

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

    echo "Using Security Manager"

    shift

    "$_RUNJAVA" $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 \

      >> "$CATALINA_OUT" 2>&1 &

 

      if [ ! -z "$CATALINA_PID" ]; then

        echo $! > $CATALINA_PID

      fi

  else

    "$_RUNJAVA" $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/cronolog/sbin/cronolog "$CATALINA_OUT".%Y-%m-%d >> /dev/null &

 

      if [ ! -z "$CATALINA_PID" ]; then

        echo $! > $CATALINA_PID

      fi

  fi


 

 

4. 결과 확인

 

 ls -al catalina.out*

// 기본 파일

catalina.out

// 변경된 파일

catalina.out.2009-11-19

// 수동으로 백업 한 파일



* Perl을 이용한 방법


File Name : spk.log.rotate

 
#!/usr/bin/perl
# (be sure to set this to your perl path and 'chmod +x' this script.)

# spk.log.rotate script for piped logging
#
# Copyright 2004 Steven P. Kneizys
#
# sample usage for piping from program "program":
#
# ... program | spk.log.rotate /path/to/logs/%Y-%M-%D-logname_log
#

use strict;
use warnings;


my $logname_template = $ARGV[0];
my $file = "";

while(<STDIN>) {
my $line = $_;
my $time = time;
my $last_time;
my $temp;
if (($file eq '') || ($time gt (60 + $last_time))) {
chomp(my $mydate=`date +%Y-%m-%d`);
$last_time = $time;
(my @date)=split(/-/,$mydate);
$temp = $logname_template;
$temp =~ s/%Y/$date[0]/gi;
$temp =~ s/%M/$date[1]/gi;
$temp =~ s/%D/$date[2]/gi;
}
if ($file ne $temp) {
if ($file ne '') {close MYFILE;}
$file = $temp;
open MYFILE,">>$file";
select((select(MYFILE), $| = 1)[0]); # autoflush
}
print MYFILE $line;
}
if ($file ne '') {close MYFILE;}



1. 해당 파일을 "$CATALINA_OUT" 파일이 있는 /logs 디렉토리에  만든다.

2. chmod 755 spk.log.rotate

3.  vi catalina.sh

 

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

 

  shift

  #touch "$CATALINA_OUT"

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

    echo "Using Security Manager"

    shift

    "$_RUNJAVA" $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 \

      >> "$CATALINA_OUT" 2>&1 &

 

      if [ ! -z "$CATALINA_PID" ]; then

        echo $! > $CATALINA_PID

      fi

  else

    "$_RUNJAVA" $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 \

      | ./"CATALINA_BASE"/logs/spk.log.rotate "$CATALINA_OUT".%Y-%M-%D >>

/dev/null &

 

      if [ ! -z "$CATALINA_PID" ]; then

        echo $! > $CATALINA_PID

      fi

  fi



참조한 문서 : http://doylestownpa.us/webadvisor/tomcat5_log_rotation.html

Comments