| 1 | package Log::Abstraction::Backend::Sendmail; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # The sendmail backend: email messages, throttled and optionally held in a digest | ||||||||||||||||||||
| 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 | our $VERSION = '0.40'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | Log::Abstraction::Backend::Sendmail - email messages | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head1 VERSION | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 0.40 | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | Built for the C<sendmail> key of a C<logger> hash. Each message (or, with | ||||||||||||||||||||
| 22 | a C<format>, the formatted line) is emailed, unless C<min_interval> seconds | ||||||||||||||||||||
| 23 | have not passed since the last email; then it is dropped, or with | ||||||||||||||||||||
| 24 | C<digest> held for the next email. Log::Abstraction does the sending and | ||||||||||||||||||||
| 25 | holds the throttle and digest state, which C<flush()> also uses. | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | For use by L<Log::Abstraction> only; see L<Log::Abstraction::Backend>. | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =head1 METHODS | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =head2 emit | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | $backend->emit($logger, $event); | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 194 | ▶ | sub emit { | ||||||||||||||||||
|
Subroutine
emit: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 38 | 194 | my ($self, $logger, $event) = @_; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 194 | my $sm = $self->{'sendmail'}; | |||||||||||||||||||
| 41 | 194 | my $body = $self->text($event, $event->{'text'}); | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | # Honour the minimum-interval throttle | ||||||||||||||||||||
| 44 | 194 | my $throttled = 0; | |||||||||||||||||||
| 45 | 194 | ▶ | if(my $interval = $sm->{'min_interval'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 161 | my $last = $logger->{'_last_email_sent'}; | |||||||||||||||||||
| 47 | 161 | ▶ | $throttled = defined($last) && ((time() - $last) < $interval); | ||||||||||||||||||
|
Condition100%3/3
defined $last && time - $last < $interval
MC/DC100%2/2
defined $last && time - $last < $interval
defined $last
time - $last < $interval
Truth table100%3/3
A && B
A= defined $lastB= time - $last < $interval
| |||||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | # A throttled message is dropped, or with digest held for the next | ||||||||||||||||||||
| 51 | # email. A failed send doesn't stop the other backends (it carps) | ||||||||||||||||||||
| 52 | 194 | ▶ | if(!$throttled) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 53 | 61 | $event->{'send_email'}->($sm, $body); | |||||||||||||||||||
| 54 | } elsif($sm->{'digest'}) { | ||||||||||||||||||||
| 55 | 127 | $event->{'hold_email'}->($sm, $body); | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =head1 AUTHOR | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | Nigel Horne C<njh@nigelhorne.com> | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =head1 SEE ALSO | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | L<Log::Abstraction>, L<Log::Abstraction::Backend> | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | Copyright (C) 2025-2026 Nigel Horne | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | Usage is subject to the GPL2 licence terms. | ||||||||||||||||||||
| 72 | If you use it, | ||||||||||||||||||||
| 73 | please let me know. | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 42 | 1; | |||||||||||||||||||