zoukankan      html  css  js  c++  java
  • thinkphp日志分析

    #!/usr/bin/perl -w
    use strict;
    use warnings;
    use Tie::File;
    
    ####
    #   Thinkphp日志分析
    #       日志基本格式:{$now} ".$_SERVER['REQUEST_URI']." | {$level}: {$message}
    ", $type,$destination,$extra
    #   使用方法:
    #
    
    sub parse_log{
        my $file = shift;
        open my $fd , "<" , $file or die "open file error";
        my %hash ;  #字符串与访问次数哈希表
        
        while(<$fd>){
            #      2013-11-18  01:30:05+08:00    uri     info   tag-end  Runtime
            if( /[ (d{4}-d{2}-d{2})T(.+) ] (.+) | (.+) [ (.+) ]/ ){
                my $date = $1 ;
                my $time = $2 ;
                my $uri = $3 ;
                my $info = $4 ;
                my $runtime = $5 ;
                
                #下面解析 /APP/?s=Module/action 的情况
                if( $uri =~ /(/.*/?s=w+/w+)/ && $info eq "INFO: Tag[ view_end ] --END--" ){
                    #print "origin:$_
    $info 	 $uri
    " ;
                    my $count = 1 ;
                    my $date_key = "$date-$1" ; 
                    if( exists $hash{$date_key} ){
                        $count = $hash{$date_key} + 1 ;
                    }
                    $hash{$date_key} = $count;
                }
            }
        }
        #排序输出结果
        #my @keys = sort { $hash{$b} <=> $hash{$a} or $b cmp $a } keys(%hash);
        my @keys = sort keys(%hash);
        my @vals = @hash{@keys};
        
        foreach my $key (@keys)
        {
            print "$hash{$key} 	 $key
    ";
        }
    }
    
    sub read_file{
        my $file = shift ;
        parse_log($file);
    }
    
    sub read_dir{
        my $log_home = "/data2/log/202-log/mobiapi/1.0" ;
        chdir $log_home or die "chdir error ?!" ;
        my @files = <*> ;
        for my $file (@files){
            read_file($file);
        }
    }
    
    sub main
    {
        my $file = "/xxx/all.log" ;
        print "———————————————$file———————————————————
    ";
        read_file($file);
    }
    
    main();
  • 相关阅读:
    两线段是否相交模板
    树的距离
    Codeforces Round #369 (Div. 2)-D Directed Roads
    Codeforces Round #369 (Div. 2)-C Coloring Trees
    Codeforces Round #374 (Div. 2)-D Maxim and Array
    zstu-4243 牛吃草
    Codeforces Round #447 (Div. 2)
    zstu 4247-萌新的旅行
    CDQ分治求前缀和
    self.faceshowing = !self.facshowing无效,了,原来set
  • 原文地址:https://www.cnblogs.com/trying/p/3433289.html
Copyright © 2011-2022 走看看