| 1 | package Log; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use autodie; | ||
| 5 | use 5.010; | ||
| 6 | use base qw(Object); | ||
| 7 | use Hashtable; | ||
| 8 | use Array; | ||
| 9 | use Geo::IP; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $pkg = shift; | ||
| 13 | return bless {}, $pkg; | ||
| 14 | } | ||
| 15 | |||
| 16 | =head1 process_log | ||
| 17 | my $logpath = '/opt/graphite/storage/log/webapp/access.log'; | ||
| 18 | process_log($logpath, sub { | ||
| 19 | my ($fields) = @_; # 第一个参数是 Array 类型 | ||
| 20 | say $fields->get(6); # 获取第6个字段 | ||
| 21 | }); | ||
| 22 | #}, '&'); | ||
| 23 | =cut | ||
| 24 | |||
| 25 | sub process_log { | ||
| 26 | my $logpath = shift; | ||
| 27 | my $cb = shift; | ||
| 28 | my $sep = shift || qr/\s+/; | ||
| 29 | open my $fh, '<', $logpath; | ||
| 30 | while (<$fh>) { | ||
| 31 | chomp; | ||
| 32 | my $lines = Array->new( split( $sep, $_ ) ); | ||
| 33 | $cb->($lines); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | 1; |