App-Sysadmin-Log-Simple
view release on metacpan or search on metacpan
lib/App/Sysadmin/Log/Simple/File.pm view on Meta::CPAN
package App::Sysadmin::Log::Simple::File;
use strict;
use warnings;
# ABSTRACT: a file-logger for App::Sysadmin::Log::Simple
our $VERSION = '0.009'; # VERSION
use Carp;
use Try::Tiny;
use autodie qw(:file :filesys);
use Path::Tiny;
sub new {
my $class = shift;
my %opts = @_;
my $app = $opts{app};
return bless {
logdir => path( $app->{logdir} || $opts{logdir} || qw(/ var log sysadmin) ),
index_preamble => $app->{index_preamble},
view_preamble => $app->{view_preamble},
date => $app->{date},
user => $app->{user},
do_file => $app->{do_file},
}, $class;
}
sub view {
my $self = shift;
my $year = $self->{date}->year;
my $month = $self->{date}->month;
my $day = $self->{date}->day;
require IO::Pager;
my $logfile = path($self->{logdir}, $year, $month, "$day.log");
die "No log for $year/$month/$day\n" unless $logfile->is_file;
my $logfh = $logfile->openr_utf8;
local $STDOUT = IO::Pager->new(*STDOUT)
unless $ENV{__PACKAGE__.' under test'};
say($self->{view_preamble}) if $self->{view_preamble};
print while (<$logfh>);
close $logfh;
return;
}
sub log {
my $self = shift;
my $line = shift;
return unless $self->{do_file};
$self->{logdir}->mkpath unless $self->{logdir}->is_dir;
my $year = $self->{date}->year;
my $month = $self->{date}->month;
my $day = $self->{date}->day;
my $dir = path($self->{logdir}, $year, $month);
$dir->mkpath unless $dir->is_dir;
my $logfile = path($self->{logdir}, $year, $month, "$day.log");
# Start a new log file if one doesn't exist already
unless ($logfile->is_file) {
open my $logfh, '>>', $logfile;
( run in 2.351 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )