File Coverage

File:blib/lib/Log/Abstraction/Backend/LogDispatch.pm
Coverage:62.5%

linestmtbrancondsubtimecode
1package Log::Abstraction::Backend::LogDispatch;
2
3# The log_dispatch backend: send messages on to Log::Dispatch
4
37
37
37
114
22
170
use strict;
5
37
37
37
32
20
376
use warnings;
6
7
37
37
37
31
23
49
use parent 'Log::Abstraction::Backend';
8
9
37
37
37
485
28
113
use Carp ();
10
37
37
37
35
15
1835
use Readonly ();
11
12our $VERSION = '0.40';
13
14# The Log::Dispatch level name for each internal level name
15Readonly::Hash my %LEVEL => (
16        trace     => 'debug',    # Log::Dispatch has nothing below debug
17        debug     => 'debug',
18        info      => 'info',
19        notice    => 'notice',
20        warn      => 'warning',
21        error     => 'error',
22        critical  => 'critical',
23        alert     => 'alert',
24        emergency => 'emergency',
25);
26
27 - 55
=head1 NAME

Log::Abstraction::Backend::LogDispatch - send messages on to Log::Dispatch

=head1 VERSION

0.40

=head1 DESCRIPTION

Built for the C<log_dispatch> key of a C<logger> hash, or a
L<Log::Dispatch> object as the C<logger>.  Calls its C<log()> method with
the level (C<warn> becomes C<warning>, and C<trace>, which Log::Dispatch
doesn't have, C<debug>) and the message with the structured fields as
C<key=value> text, or with a C<format> the formatted line.  If
Log::Dispatch dies, it carps.

Nothing is sent while a message is already being forwarded (see
C<$Log::Abstraction::Backend::FORWARDING>).

For use by L<Log::Abstraction> only; see L<Log::Abstraction::Backend>.

=head1 METHODS

=head2 emit

  $backend->emit($logger, $event);

=cut
56
57sub emit {
58
0
        my ($self, $logger, $event) = @_;
59
60
0
        return if($Log::Abstraction::Backend::FORWARDING);
61
0
        local $Log::Abstraction::Backend::FORWARDING = 1;
62
63
0
        my $ok = eval {
64                $self->{'log_dispatch'}->log(
65                        level   => $LEVEL{$event->{'level'}},
66
0
                        message => $self->text($event, $event->{'text'}),
67                );
68
0
                1;
69        };
70
0
        Carp::carp(ref($logger), ": Log::Dispatch failed: $@") if(!$ok);
71}
72
73 - 89
=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
90
911;