App-Elog

 view release on metacpan or  search on metacpan

bin/alog  view on Meta::CPAN

        $prev = $c;
    }
    if (!@captures) {
        die "Unable to parse format: $format\n";
    }
    return \@captures;
}

sub parse_access_line_different {
    my ($line) = @_;
    # For lines like:
    # unique_id:"Y0gPiJCuUIKvl11H6djHxgAAAUM" remote_host:"184.94.203.3"      client_IP:"184.94.203.3"        X-Real-IP:"92.205.104.221"      ident:"-"       user:"-"        time:"[13/Oct/2022:08:15:52 -0500]"     req:"GET /foo.cgi?asdf=436 HTTP/1.1"...
    my $a = {};
    my %key_trans = (
        "time" => "datestr",
        "client_ip" => "ip",
        "ua" => "user_agent",
        "req" => "request",
        "initial_status" => "status",
        "final_status" => "status",
        "reqtime_usec" => "reqtime",
    );
    while ($line =~ m{\G\s*([\w-]+):("([^"]|\\")*"|\S*)}gc) {
        my $key = $1;
        my $value = $2;
        $value =~ s/^"|"$//g;
        $value =~ s/^\[|\]$//g;
        $key = lc $key;
        $key =~ s/-/_/g;
        if ($key_trans{$key}) {
            $key = $key_trans{$key};
        }
        $a->{$key} = $value;
    }
    if (!$a->{datestr}) {
        warn "Unable to parse: $line\n";
    }
    process_access($a);
    return $a;
}

sub process_access {
    my ($a) = @_;
    delete $a->{ident} if !$a->{ident} || $a->{ident} eq "-";
    delete $a->{user} if !$a->{user} || $a->{user} eq "-";
    delete $a->{user_agent} if !$a->{user_agent} || $a->{user_agent} eq "-";
    $a->{bytes} = 0 if $a->{bytes} && $a->{bytes} eq "-";
    delete $a->{referer} if $a->{referer} && $a->{referer} eq "-";
    $a->{request} ||= "";
    if ($a->{request} =~ /^ (\S+) \s+ (\S+) \s+ (\S+) $/x) {
        $a->{method} = $1;
        $a->{uri} = $2;
        $a->{protocol} = $3;
        $a->{loc} = $a->{uri};
        $a->{loc} =~ s/\?.*//;
        delete $a->{request};
    }
    else {
        $a->{method} = $a->{uri} = $a->{protocol} = $a->{loc} = "";
    }
    if ($a->{loc} =~ /\.(jpe?g|gif|png|ico)$/i) {
        $a->{type} = "image";
    }
    elsif ($a->{loc} =~ /\.(cgi)$/i) {
        $a->{type} = "cgi";
    }
    elsif ($a->{loc} =~ /\.(php)$/i) {
        $a->{type} = "php";
    }
    elsif ($a->{loc} =~ /\.(html?)$/i) {
        $a->{type} = "html";
    }
    elsif ($a->{loc} =~ /\.(js)$/i) {
        $a->{type} = "js";
    }
    elsif ($a->{loc} =~ /\.(css)$/i) {
        $a->{type} = "css";
    }
    elsif ($a->{loc} =~ m{/[^/\.]*$}i) {
        $a->{type} = "script";
    }
    else {
        $a->{type} = "other";
    }
    $a->{date} = parse_date($a->{datestr});
    $a->{datestr} = datestr($a->{date});
}

sub datestr {
    my ($date) = @_;
    return "" if !$date;
    my $datestr = POSIX::strftime("%a %b %d, %Y %I:%M:%S %p", localtime($date));
    return $datestr;
}

# returns a string like "17 hours 5 minutes 2 seconds"
sub time_diff_str2 {
    my ($date1, $date2) = @_;
    return "" if !$date1 || !$date2;
    my $obj = time_diff_obj($date1, $date2);

    my $str = "";
    for my $key ("year", "month", "day", "hour", "minute", "second") {
        if ($obj->{$key}) {
            if ($str) {
                $str .= " ";
            }
            $str .= "$obj->{$key} $key";
            if ($obj->{$key} != 1) {
                $str .= "s";
            }
        }
    }

    if ($date1 == $date2) {
        $str = "0 seconds";
    }
    return $str;
}

# returns a string like "17 hours ago" or "in 5 seconds"



( run in 2.177 seconds using v1.01-cache-2.11-cpan-df04353d9ac )