Apache日志分割的3种方法

来源:http://holy2010.blog.51cto.com/1086044/426346

一、rotatelogs(apache 自带的工具)

    rotatelogs是一个配合Apache管道日志功能使用的简单程序。举例:
    CustomLog "|bin/rotatelogs /var/logs/logfile 86400" common
    此配置会建立文件/var/logs/logfile.nnnn,其中的nnnn是名义上的日志启动时的系统时间 (此时间总是回卷时间的倍数,可以用于cron脚本的同步)。 在回卷时间到达时(在此例中是24小时以后),会产生一个新的日志。
    CustomLog "|/opt/apache/bin/rotatelogs /opt/apache/logs/access_log.%d-%m-%y 800M" common 
    此配置会在日志文件大小增长到800兆字节时回卷该日志。
 概要
    rotatelogs logfile [ rotationtime [ offset ]] | [ filesizeM ]

选项
    logfile 
    它加上基准名就是日志文件名。如果logfile中包含'%',则它会被视为用于的strftime(3)的格式字串;否则,它会被自动加上以秒为单位的.nnnnnnnnnn后缀。这两种格式都表示新的日志开始使用的时间。 
rotationtime 
日志文件回卷的以秒为单位的间隔时间 
offset 
相对于UTC的时差的分钟数。如果省略,则假定为0,并使用UTC时间。比如,要指定UTC时差为-5小时的地区的当地时间,则此参数应为-300。 
filesizeM 
指定回卷时以兆字节为单位的后缀字母M的文件大小,而不是指定回卷时间或时差。

可移植性:下列日志文件格式字串可以为所有的strftime(3)实现所支持,见各种扩展库对应的strftime(3)的手册。

%A 星期名全称(本地的) 
%a 3个字符的星期名(本地的) 
%B 月份名的全称(本地的) 
%b 3个字符的月份名(本地的) 
%c 日期和时间(本地的) 
%d 2位数的一个月中的日期数 
%H 2位数的小时数(24小时制) 
%I 2位数的小时数(12小时制) 
%j 3位数的一年中的日期数 
%M 2位数的分钟数 
%m 2位数的月份数 
%p am/pm 12小时制的上下午(本地的) 
%S 2位数的秒数 
%U 2位数的一年中的星期数(星期天为一周的第一天) 
%W 2位数的一年中的星期数(星期一为一周的第一天) 
%w 1位数的星期几(星期天为一周的第一天) 
%X 时间 (本地的) 
%x 日期 (本地的) 
%Y 4位数的年份 
%y 2位数的年份 
%Z 时区名 
%% 符号`%'本身

二、cronolog

在apache的FAQ中,推荐了经过近2年发展已经比较成熟的一个工具cronolog:安装很简单:configure=> make=> make install

他的一个配置的例子会让你了解它有多么适合日志按天轮循:对httpd.conf做一个很小的修改就能实现:
TransferLog "|/usr/sbin/cronolog /var/log/%Y%m%d.log"
ErrorLog "|/usr/sbin/cronolog /var/%Y%m%d-errors.log"

然后:日志将写入
/var/log/20090901.log
/var/log/20090901-errors.log
目录如果不存在的话,将自动创建

三、大哥用的方法,perl脚本控制,贴出来大家拿来玩,缺点太多,以至于大哥不打算用了,优点是你可以随便分割你需要的日志,看完别忘了给大哥烧纸(http://51runaway.blog.163.com/)。

#!/usr/bin/perl
my (%month,%date_log);
BEGIN{
  $month{Jan}="01";$month{Feb}="02";$month{Mar}="03";
  $month{Apr}="04";$month{May}="05";$month{Jun}="06";
  $month{Jul}="07";$month{Aug}="08";$month{Sep}="09";
  $month{Oct}="10";$month{Nov}="11";$month{Dec}="12";

my $hostip=GetIp();
my ($hostname)=$hostip=~/\.(\d+)$/;
my $localt;
#print "$hostip\t$hostname\n";
my ($file,$flag)=('',0);
my $tmpfile="";
while(1){
 system("mkdir /var/apache_log") unless(-e '/var/apache_log');
 system("mkdir /var/tmp") unless(-e '/var/tmp'); 
 my ($sec,$min,$hour,$day,$mon,$year) = localtime();
  $mon++;$year -= 100;
  $year="0$year" if($year<10);
  $mon="0$mon" if($mon<10);
  $day="0$day" if($day<10);
 $tmpfile=$hostname.'_'.$year.$mon.$day.".log";
 if($hour>0 && $hour<2){
  if($flag==0){
   $file='';
   my $back_file="_$year$mon$day";
   system("cp /usr/local/apache2/logs/access_log /var/tmp/$back_file");
   split_apache_log($hostname,'/usr/local/apache2/logs/access_log');
   $flag=1;
  }
 }else{
  #print $flag,"\n";
  $flag=0 if($flag==1);
 }
 sleep(1800);
}
#----------------------------------------------------------------------------------------------------------------
sub split_apache_log{
 my ($hostid,$access_log)=@_;
 my %hash=();
 my $flag_new_log=0;
 my $localt = localtime();
 printlog("\nsplit apapche log at $localt\n\n");
 open(LIST,"$access_log")||die "$!";  
 while(<LIST>){
  chomp;                                                                          
  my (@array)=$_=~/^(\S+)\s+\S+\s+\S+\s+\[(\S+).+\]\s+\"(.+)\"\s+(\d+)\s+\S+/;
  next if($array[3] eq '404');
  my $time_str=$array[1];
  $time_str=~s/\//\t/g;
  $time_str=~s/:/\t/g;
  my @t_s=split(/\t/,$time_str);
  next if(not exists $t_s[2]);  
  $t_s[2]=substr($t_s[2],2,2);
  my $date_tmp="$t_s[2]$month{$t_s[1]}$t_s[0]";
  $file=$hostid.'_'."$date_tmp.log";
  push(@{$hash{$file}},$_);
  if(scalar(@{$hash{$file}})>=20000){
    my @arraytmp=splice(@{$hash{$file}}, 0, 20000);
    my $line=join("\n",@arraytmp);     
    if(-e "/var/log/apache/$file"){
     open(OUT,">>/var/log/apache/$file");
     print OUT "$line\n"; 
     close (OUT);      
      
     }else{
      $flag_new_log=1 if($flag_new_log==0);
     open(OUT,">>/var/apache_log/$file");
     print OUT "$line\n"; 
     close (OUT);      
     }
  }  
 }
 close(LIST);
 foreach $file(sort {$a cmp $b} keys %hash){
  next if(not exists ${$hash{$file}}[0]);
  my $line=join("\n",@{$hash{$file}});
  if(-e "/var/log/apache/$file"){
     open(OUT,">>/var/log/apache/$file");
     print OUT "$line\n"; 
     close (OUT);      
      
  }else{
      $flag_new_log=1 if($flag_new_log==0);
     open(OUT,">>/var/apache_log/$file");
     print OUT "$line\n"; 
     close (OUT);
  }
 }
 if($flag_new_log==1){ 
  system("cat /var/apache_log/$tmpfile>$access_log");
  system("rm -fr /var/apache_log/$tmpfile");
  system("mv /var/apache_log/* /var/log/apache/");
 }
 $localt = localtime();
 printlog("\nFinished split apapche log at $localt\n\n"); 
}
#-----------------------------------------------------------------------------------------------------------------
sub GetIp{
 my $ifc=`ifconfig`;
 my ($ip,$ip1,$ip2); 
 if($ifc=~/eth0/){
  my $str=`ifconfig eth0` ;
  ($ip1)=$str=~/inet addr:(\S+)/ ;
  $ip=$ip1;
 }
 if($ifc=~/eth1/){
  my $str=`ifconfig eth1` ;
  ($ip2)=$str=~/inet addr:(\S+)/ ;
  if($ip1 && ($ip1 ne $ip2)){
   $ip="$ip1 $ip2" ;
  }else{
   $ip=$ip2;
  }
 }
 return $ip;
}
#-------------------------------------------------------------------------------------------------------------------
sub printlog{
 my $str=shift;
 open(LOGOUT,">>/var/lib/SplitApacheLog.log");
 print LOGOUT "$str";
 close LOGOUT; 

posted @ 2013-01-23 11:55  hasayaki  阅读(447)  评论(0编辑  收藏  举报