| File: | blib/lib/Log/Abstraction/Backend/Sendmail.pm |
| Coverage: | 90.6% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | package Log::Abstraction::Backend::Sendmail; | |||||
| 2 | ||||||
| 3 | # The sendmail backend: email messages, throttled and optionally held in a digest | |||||
| 4 | 37 37 37 | 101 18 158 | use strict; | |||
| 5 | 37 37 37 | 29 17 316 | use warnings; | |||
| 6 | ||||||
| 7 | 37 37 37 | 26 25 40 | use parent 'Log::Abstraction::Backend'; | |||
| 8 | ||||||
| 9 | our $VERSION = '0.40'; | |||||
| 10 | ||||||
| 11 - 35 | =head1 NAME Log::Abstraction::Backend::Sendmail - email messages =head1 VERSION 0.40 =head1 DESCRIPTION Built for the C<sendmail> key of a C<logger> hash. Each message (or, with a C<format>, the formatted line) is emailed, unless C<min_interval> seconds have not passed since the last email; then it is dropped, or with C<digest> held for the next email. Log::Abstraction does the sending and holds the throttle and digest state, which C<flush()> also uses. For use by L<Log::Abstraction> only; see L<Log::Abstraction::Backend>. =head1 METHODS =head2 emit $backend->emit($logger, $event); =cut | |||||
| 36 | ||||||
| 37 | sub emit { | |||||
| 38 | 14 | 10 | my ($self, $logger, $event) = @_; | |||
| 39 | ||||||
| 40 | 14 | 9 | my $sm = $self->{'sendmail'}; | |||
| 41 | 14 | 14 | my $body = $self->text($event, $event->{'text'}); | |||
| 42 | ||||||
| 43 | # Honour the minimum-interval throttle | |||||
| 44 | 14 | 5 | my $throttled = 0; | |||
| 45 | 14 | 14 | if(my $interval = $sm->{'min_interval'}) { | |||
| 46 | 3 | 1 | my $last = $logger->{'_last_email_sent'}; | |||
| 47 | 3 | 3 | $throttled = defined($last) && ((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 | 14 | 14 | if(!$throttled) { | |||
| 53 | 12 | 13 | $event->{'send_email'}->($sm, $body); | |||
| 54 | } elsif($sm->{'digest'}) { | |||||
| 55 | 0 | $event->{'hold_email'}->($sm, $body); | ||||
| 56 | } | |||||
| 57 | } | |||||
| 58 | ||||||
| 59 - 75 | =head1 AUTHOR Nigel Horne C<njh@nigelhorne.com> =head1 SEE ALSO L<Log::Abstraction>, L<Log::Abstraction::Backend> =head1 COPYRIGHT AND LICENSE Copyright (C) 2025-2026 Nigel Horne Usage is subject to the GPL2 licence terms. If you use it, please let me know. =cut | |||||
| 76 | ||||||
| 77 | 1; | |||||