« blib/lib/Log/Abstraction/Backend/Sendmail.pm
↑ Summary
blib/lib/Log/Abstraction/Backend/Syslog.pm »
| 1 | package Log::Abstraction::Backend::LogAny; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # The log_any backend: send messages on to Log::Any | ||||||||||||||||||||
| 4 | 42 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 42 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 42 | ▶ | use parent 'Log::Abstraction::Backend'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 42 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 42 | ▶ | use Readonly (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 42 | ▶ | use Scalar::Util (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 42 | our $VERSION = '0.40'; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # The Log::Any method for each internal level name | ||||||||||||||||||||
| 16 | 42 | Readonly::Hash my %METHOD => ( | |||||||||||||||||||
| 17 | trace => 'trace', | ||||||||||||||||||||
| 18 | debug => 'debug', | ||||||||||||||||||||
| 19 | info => 'info', | ||||||||||||||||||||
| 20 | notice => 'notice', | ||||||||||||||||||||
| 21 | warn => 'warning', | ||||||||||||||||||||
| 22 | error => 'error', | ||||||||||||||||||||
| 23 | critical => 'critical', | ||||||||||||||||||||
| 24 | alert => 'alert', | ||||||||||||||||||||
| 25 | emergency => 'emergency', | ||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | =head1 NAME | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | Log::Abstraction::Backend::LogAny - send messages on to Log::Any | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | =head1 VERSION | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0.40 | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | Built for the C<log_any> key of a C<logger> hash, or a L<Log::Any> logger | ||||||||||||||||||||
| 39 | (C<Log::Any::Proxy>) as the C<logger>. A category name is looked up with | ||||||||||||||||||||
| 40 | C<Log::Any-E<gt>get_logger()> for the first message. The message goes to | ||||||||||||||||||||
| 41 | the method for its level (C<warn> becomes C<warning>), with the structured | ||||||||||||||||||||
| 42 | fields as Log::Any's trailing hash reference; with a C<format>, the | ||||||||||||||||||||
| 43 | formatted line instead. If Log::Any dies, it carps. | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | Nothing is sent while a message is already being forwarded (see | ||||||||||||||||||||
| 46 | C<$Log::Abstraction::Backend::FORWARDING>), so a message that comes back | ||||||||||||||||||||
| 47 | through L<Log::Any::Adapter::Abstraction> doesn't loop. | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | For use by L<Log::Abstraction> only; see L<Log::Abstraction::Backend>. | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =head1 METHODS | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =head2 new | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | my $backend = Log::Abstraction::Backend::LogAny->new( | ||||||||||||||||||||
| 56 | log_any => $proxy_or_category, level => $level, format => $format, | ||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 4 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 62 | 4 | my ($class, %args) = @_; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 4 | my $self = $class->SUPER::new(%args); | |||||||||||||||||||
| 65 | 4 | ▶ | $self->{'proxy'} = $args{'log_any'} if(Scalar::Util::blessed($args{'log_any'})); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 4 | return $self; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head2 emit | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | $backend->emit($logger, $event); | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 9 | ▶ | sub emit { | ||||||||||||||||||
|
Subroutine
emit: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||
| 76 | 9 | my ($self, $logger, $event) = @_; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 9 | ▶ | return if($Log::Abstraction::Backend::FORWARDING); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 7 | local $Log::Abstraction::Backend::FORWARDING = 1; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 7 | my $ok = eval { | |||||||||||||||||||
| 82 | # Proxies follow Log::Any::Adapter->set(), so one is enough | ||||||||||||||||||||
| 83 | 7 | ▶ | my $proxy = $self->{'proxy'} //= do { | ||||||||||||||||||
|
Condition66%2/3
$self->{"proxy"} //= do {
require Log::Any;
"Log::Any"->get_logger("category", $self->{"log_any"})
}
MC/DC0%0/2
$self->{"proxy"} //= do {
require Log::Any;
"Log::Any"->get_logger("category", $self->{"log_any"})
}
$self->{"proxy"}
do {
require Log::Any;
"Log::Any"->get_logger("category", $self->{"log_any"})
}
Truth table66%2/3
A //= B
A= $self->{"proxy"}B= do {
require Log::Any;
"Log::Any"->get_logger("category", $self->{"log_any"})
}
| |||||||||||||||||||||
| 84 | 2 | require Log::Any; | |||||||||||||||||||
| 85 | 2 | Log::Any->get_logger(category => $self->{'log_any'}); | |||||||||||||||||||
| 86 | }; | ||||||||||||||||||||
| 87 | 7 | my $method = $METHOD{$event->{'level'}}; | |||||||||||||||||||
| 88 | 7 | ▶ | if(defined($self->{'format'})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 1 | $proxy->$method($event->{'render'}->($self->{'format'})); | |||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||
| 91 | 6 | ▶ | $proxy->$method($event->{'message'}, ($event->{'fields'} ? ($event->{'fields'}) : ())); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | 7 | 1; | |||||||||||||||||||
| 94 | }; | ||||||||||||||||||||
| 95 | 7 | ▶ | Carp::carp(ref($logger), ": Log::Any failed: $@") if(!$ok); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =head1 AUTHOR | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | Nigel Horne C<njh@nigelhorne.com> | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =head1 SEE ALSO | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | L<Log::Abstraction>, L<Log::Abstraction::Backend> | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | Copyright (C) 2025-2026 Nigel Horne | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | Usage is subject to the GPL2 licence terms. | ||||||||||||||||||||
| 111 | If you use it, | ||||||||||||||||||||
| 112 | please let me know. | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 42 | 1; | |||||||||||||||||||