App-Sysadmin-Log-Simple
view release on metacpan or search on metacpan
lib/App/Sysadmin/Log/Simple.pm view on Meta::CPAN
package App::Sysadmin::Log::Simple;
use strict;
use warnings;
use v5.10.1;
# ABSTRACT: application class for managing a simple sysadmin log
our $VERSION = '0.009'; # VERSION
use autodie qw(:file :filesys);
use DateTime;
use Carp;
use Module::Pluggable
search_path => [__PACKAGE__],
instantiate => 'new';
sub new {
my $class = shift;
my %opts = @_;
my $today = DateTime->now;
if ($opts{date}) {
my ($in_year, $in_month, $in_day) = split(m{/}, $opts{date});
my $in_date = DateTime->new(
year => $in_year,
month => $in_month,
day => $in_day,
) or croak "Couldn't understand your date - use YYYY/MM/DD\n";
croak "Cannot use a date in the future\n" if $in_date > $today;
$today = $in_date;
}
return bless {
do_twitter => $opts{do_twitter} // 0,
do_file => $opts{do_file} // 1,
do_http => $opts{do_http} // 0,
do_udp => $opts{do_udp} // 1,
logdir => $opts{logdir},
date => $today,
user => $opts{user} || $ENV{SUDO_USER} || $ENV{USER},
in => $opts{read_from} || \*STDIN,
udp => $opts{udp},
http => $opts{http},
}, $class;
}
sub run {
my $self = shift;
my $cmd = shift;
$cmd ||= 'log';
$self->run_command($cmd);
return;
}
sub run_command {
my $self = shift;
my $cmd = shift;
my $s = $self->can("run_command_$cmd");
die "Unknown command '$cmd'" unless $s;
return $self->$s();
}
sub run_command_log {
my $self = shift;
say 'Log entry:';
my $in = $self->{in};
my $logentry = <$in>; # one line
chomp $logentry;
croak 'A log entry is needed' unless $logentry;
PLUGIN: foreach my $plugin ( $self->plugins(app => $self) ) {
next PLUGIN unless $plugin->can('log');
my $r = $plugin->log($logentry);
if ($r) {
my $name = ref $plugin;
my $re = __PACKAGE__ . '::';
$name =~ s/^$re//;
say sprintf '[%-8s] %s', $name, $r;
}
}
}
( run in 0.465 second using v1.01-cache-2.11-cpan-ceb78f64989 )