Acrux

 view release on metacpan or  search on metacpan

lib/Acme/Crux/Plugin/Log.pm  view on Meta::CPAN

Default: C<logprovider> command line option or C<logprovider> application argument
or C<LogProvider> configuration value or C<file> otherwise

=head2 short

    $app->plugin(Log => undef, {short => 1});

Generate short log messages without a timestamp but with log level prefix

Default: C<logshort> command line option or C<logshort> application argument
or C<LogShort> configuration value or C<0> otherwise

=head1 METHODS

This class inherits all methods from L<Acme::Crux::Plugin> and implements the following new ones

=head2 register

    $plugin->register($app, {file => '/var/log/app.log'});

Register plugin in Acme::Crux application

=head1 HELPERS

All helpers of this plugin are allows get access to logger object.
See L<Acrux::Log> for details

=head2 log

Returns L<Acrux::Log> object

=head1 TO DO

See C<TODO> file

=head1 SEE ALSO

L<Acme::Crux::Plugin>, L<Acrux::Log>

=head1 AUTHOR

Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>

=head1 COPYRIGHT

Copyright (C) 1998-2026 D&D Corporation

=head1 LICENSE

This program is distributed under the terms of the Artistic License Version 2.0

See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details

=cut

use parent 'Acme::Crux::Plugin';

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
      || $app->orig->{"logfacility"} # From App arguments
      || ($has_config ? $app->config->get("/logfacility") : ''); # From config file

    # Log file: PLGARGS || OPTS || CONF || ORIG || DEFS
    my $file = $args->{file}  # From plugin arguments first
      || $app->getopt("logfile") # From command line options
      || ($has_config ? $app->config->get("/logfile") : '') # From config file
      || $app->logfile; # From App arguments

    # Format: PLGARGS || DEFS
    my $frmt = $args->{format} || $app->orig->{"logformat"};
    if (defined($frmt) && length($frmt)) {
        croak(qq{Invalid log format coderef}) unless is_code_ref($frmt);
    }

    # Handle: PLGARGS || DEFS
    my $handle = $args->{handle} || $app->orig->{"loghandle"};
    if (defined $handle) {
        croak(qq{Invalid log handle}) unless is_ref($handle);
    }

    # Log ident: PLGARGS || OPTS || ORIG || CONF || DEFS
    my $ident = $args->{ident}  # From plugin arguments first
      || $app->getopt("logident") # From command line options
      || $app->orig->{"logident"} # From App arguments
      || ($has_config ? $app->config->get("/logident") : ''); # From config file

    # Log level: PLGARGS || OPTS || ORIG || CONF || DEFS
    my $level = $args->{level}  # From plugin arguments first
      || $app->getopt("loglevel") # From command line options
      || $app->orig->{"loglevel"} # From App arguments
      || ($has_config ? $app->config->get("/loglevel") : ''); # From config file

    # Logger: PLGARGS || DEFS
    my $logger = $args->{logger} || $app->orig->{"logger"};
    if (defined $logger) {
        croak(qq{Invalid logger object}) unless is_ref($logger);
    }

    # Log options: PLGARGS || OPTS || ORIG || CONF || DEFS
    my $logopt = $args->{logopt}  # From plugin arguments first
      || $app->getopt("logopt") # From command line options
      || $app->orig->{"logopt"} # From App arguments
      || ($has_config ? $app->config->get("/logopt") : ''); # From config file

    # Short flag: PLGARGS || OPTS || ORIG || CONF || DEFS
    my $short = is_true_flag($args->{short}) # From plugin arguments first
      || $app->getopt("logshort") # From command line options
      || $app->orig->{"logshort"} # From App arguments
      || ($has_config ? $app->config->get("/logshort") : 0); # From config file

    # Log prefix: PLGARGS || OPTS || ORIG || CONF || DEFS
    my $prefix = $args->{prefix}  # From plugin arguments first
      || $app->getopt("logprefix") # From command line options
      || $app->orig->{"logprefix"} # From App arguments
      || ($has_config ? $app->config->get("/logprefix") : ''); # From config file

    # Correct provider rules
    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,
        prefix => $prefix,
    );

    # Set log helper (method)
    $app->register_method(log => sub { $log });

    return $log;
}

1;

__END__



( run in 2.473 seconds using v1.01-cache-2.11-cpan-0bb4e1dffa6 )