Acrux
view release on metacpan or search on metacpan
lib/Acme/Crux/Plugin/Log.pm view on Meta::CPAN
$app->log->emerg('System is unusable');
=head1 DESCRIPTION
The Acme::Crux plugin for logging in your application
=head1 OPTIONS
This plugin supports the following options
=head2 autoclean
$app->plugin(Log => undef, {autoclean => 1});
This option enables cleaning (closing handler or syslog) on DESTROY
Default: C<logautoclean> command line option or C<logautoclean> application argument
or C<LogAutoclean> configuration value or C<0> otherwise
=head2 color
$app->plugin(Log => undef, {color => 1});
This option enables colorize log messages with the available levels using L<Term::ANSIColor>
Default: C<logcolorize> command line option or C<logcolorize> application argument
or C<LogColorize> configuration value or C<0> otherwise
lib/Acme/Crux/Plugin/Log.pm view on Meta::CPAN
use Acrux::Log;
use Carp qw/croak/;
use Acrux::RefUtil qw/as_array_ref as_hash_ref is_code_ref is_true_flag is_ref/;
sub register {
my ($self, $app, $args) = @_;
my $has_config = $app->can('config') ? 1 : 0;
# Autoclean flag: PLGARGS || OPTS || ORIG || CONF || DEFS
my $autoclean = is_true_flag($args->{autoclean}) # From plugin arguments first
|| $app->getopt("logautoclean") # From command line options
|| $app->orig->{"logautoclean"} # From App arguments
|| ($has_config ? $app->config->get("/logautoclean") : 0); # From config file
# Colorize flag: PLGARGS || OPTS || ORIG || CONF || DEFS
my $colorize = is_true_flag($args->{color}) # From plugin arguments first
|| $app->getopt("logcolorize") # From command line options
|| $app->orig->{"logcolorize"} # From App arguments
|| ($has_config ? $app->config->get("/logcolorize") : 0); # From config file
# Log facility: PLGARGS || OPTS || ORIG || CONF || DEFS
my $facility = $args->{facility} # From plugin arguments first
|| $app->getopt("logfacility") # From command line options
lib/Acme/Crux/Plugin/Log.pm view on Meta::CPAN
my $provider = $args->{provider} # From plugin arguments first
|| $app->getopt("logprovider") # From command line options
|| $app->orig->{"logprovider"} # From App arguments
|| ($has_config ? $app->config->get("/logprovider") : '') || ''; # From config file
if ($provider eq 'syslog') { $file = $handle = $logger = undef }
elsif ($provider eq 'file') { $logger = $handle = undef }
elsif ($provider eq 'handle') { $logger = undef }
# Create instance
my $log = Acrux::Log->new(
autoclean => $autoclean,
color => $colorize, # !!
facility => $facility,
file => $file,
format => $frmt, # !!
handle => $handle,
ident => $ident,
level => $level,
logger => $logger,
logopt => $logopt,
short => $short,
lib/Acrux/Log.pm view on Meta::CPAN
Acrux::Log is a simple logger for Acrux logging
=head2 new
my $log = Acrux::Log->new(
logopt => 'ndelay,pid',
facility => 'user',
level => 'debug',
ident => 'test.pl',
autoclean => 1,
logopt => 'ndelay,pid',
);
With default attributes
use Mojo::Log;
my $log = Acrux::Log->new( logger => Mojo::Log->new );
$log->error("Test error message");
This is example with external loggers
=head1 ATTRIBUTES
This class implements the following attributes
=head2 autoclean
autoclean => 1
This attribute enables cleaning (closing handler or syslog) on DESTROY
=head2 color
color => 1
Colorize log messages with the available levels using L<Term::ANSIColor>, defaults to C<0>
=head2 facility
lib/Acrux/Log.pm view on Meta::CPAN
my $class = shift;
my $args = @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {};
$args->{facility} ||= Sys::Syslog::LOG_USER;
$args->{ident} ||= basename($0);
$args->{logopt} ||= LOGOPTS;
$args->{logger} ||= undef;
$args->{level} ||= 'debug';
$args->{file} ||= undef;
$args->{handle} ||= undef;
$args->{provider} = 'unknown';
$args->{autoclean} ||= 0;
$args->{prefix} ||= '';
$args->{format} ||= undef;
$args->{color} ||= 0;
# Check level
$args->{level} = lc($args->{level});
unless (exists $MAGIC{$args->{level}}) {
carp "Incorrect log level specified. Well be used debug log level by default";
$args->{level} = 'debug';
}
lib/Acrux/Log.pm view on Meta::CPAN
}
sub _color {
my $msg = _default(shift, my $level = shift, @_);
return $msg unless $COLORS{$level};
chomp $msg;
return color($COLORS{$level}, $msg) . "\n";
}
DESTROY {
my $self = shift;
if ($self->{autoclean}) {
undef $self->{handle} if $self->{file};
Sys::Syslog::closelog() if $self->{provider} eq "syslog";
}
}
1;
__END__
( run in 0.244 second using v1.01-cache-2.11-cpan-a5abf4f5562 )