App-loggrep

 view release on metacpan or  search on metacpan

bin/loggrep  view on Meta::CPAN

   [
      'time|t=s',
      'perl code to convert a log line into a Unix timestamp; '
        . 'this is an alternative to --date'
   ],
   [
      'exec|E=s',
      'perl code to call before printing a line; '
        . '@_ will contain the line, the line number, and whether it was a match;'
        . ' the value returned by this code is what will be printed'
   ],
   [
      'module|M=s@',
      'Perl module to load for use by --time or --exec code; repeatable'
   ],
   [],
   [ 'help|h|?', "print usage message and exit" ],
   [ 'version',  'print ' . prog_name() . "'s version number" ],
);

usage() if $opt->help;
require App::loggrep;
print( prog_name() . " $App::loggrep::VERSION\n" ), exit if $opt->version;

my @errors;
push @errors, 'you cannot specify both --warn and --die'
  if $opt->warn && $opt->die;

my $grepper = App::loggrep->new( $opt->log // shift, $opt );
push @errors, $grepper->init;

error() if @errors;

$grepper->grep;

# print errors and minimal usage information
sub error {
   return unless @errors;
   print STDERR "ERRORS\n\n";
   print STDERR "\t$_\n" for @errors;
   print STDERR "\n";

   print STDERR $usage->text;
   exit;
}

# print maximal usage information
sub usage {
   print $usage->text;
   print <<"END";

@{[prog_name()]} facilitates search for lines in a log file within a specified date range.
If you aren't interested in filtering by date range, use grep instead.

If multiple expression to include are provided, a line which matches any will be printed.
Likewise, if a line matches any of the exclusion expressions, it will be excluded.

@{[prog_name()]} uses Date::Parse to convert a timestamp into a Unix timestap (a number of
seconds since time zero). Date::Parse is pretty clever, but if it doesn't understand your
timestamps you'll have to use the --time option. If you want to use a particular module,
like Date::Parse or DateTime, in your --time code, you'll have to use or require it inside
your code or load it in with the --module option like so

  -M Date::Parse --time '(\$m=shift)=~s/(.*?) foo/\$1/;\$m eq "bar" ? 1 : str2time \$m'

END
   exit;
}

__END__

=pod

=encoding UTF-8

=head1 NAME

loggrep - quickly find relevant lines in a log searching by date

=head1 VERSION

version 0.002

=head1 SYNOPSIS

=over 8

  loggrep --start <date> --end <date> [ --include <pattern> ]+ [ --exclude <pattern> ]+ <file>

=back

=head1 DESCRIPTION

B<loggrep> allows one to search for lines in a file that match particular patterns. In this it is
like grep and ack and many other utilities. The functionality it adds is an ability to narrow the
search window to those lines that fall within temporal limits. It can find these limits quickly by
a variety of binary search, allowing one to search very large log files efficiently. This requires, 
of course, that the lines in the file (usually) have times stamps which (usually) are in sequence
and parsable in a common way.

Loggrep searches for an initial temporal limit by estimating the line offset for the line sought
based on the marginal timestamps of the search region and the assumption that lines are added at
a roughly constant rate. This candidate line is found; then the nearest line bearing a timestamp is
sought. This time is compared the to target time and the process is repeated within the new search
region until either the target time is found or the search region cannot be narrowed further.

=head1 OPTIONS

Run loggrep with the C<--help> option to see the option default values, if any.

=head2 Log

=over 8

=item -l I<file>, --log=I<file>

The log file to search may be provided either as the final argument or as the value of a C<--log>
option.

=back



( run in 1.151 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )