blib/lib/Log/Abstraction/Backend/Object.pm

filter: Statementstmt 100% 26 / 26 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 97.7% 43 / 44 CC 7 SCAR 15.7
CC
7
Cov
97%
emit
7.0
BEGIN
1.0
BEGIN
1.0
CRAP 4.8
« blib/lib/Log/Abstraction/Backend/Journald.pm ↑ Summary blib/lib/Log/Any/Adapter/Abstraction.pm »
1 package Log::Abstraction::Backend::Object;
2  
3 # The object backend: call the logger object's method for the level
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  
11 42 our $VERSION = '0.40';
12  
13 =head1 NAME
14  
15 Log::Abstraction::Backend::Object - call a logger object's method for each message
16  
17 =head1 VERSION
18  
19 0.40
20  
21 =head1 DESCRIPTION
22  
23 Built for any object given as the C<logger> other than a L<Log::Any> logger
24 or a L<Log::Dispatch> object (see L<Log::Abstraction::Backend::LogAny> and
25 L<Log::Abstraction::Backend::LogDispatch>), e.g. a L<Log::Log4perl> logger.
26 The method named after the level is called with the message's parts, and
27 the structured fields as C<key=value> text. An object without a
28 C<notice> method gets C<info>; one without C<critical>, C<alert> or
29 C<emergency> gets C<fatal>, or else C<error>. Otherwise it croaks.
30  
31 For use by L<Log::Abstraction> only; see L<Log::Abstraction::Backend>.
32  
33 =head1 METHODS
34  
35 =head2 emit
36  
37 $backend->emit($logger, $event);
38  
39 =cut
40  
41 23 ▶sub emit {
Subroutine emit: called CC=7 SCAR=19.5
Pod documented
42 23 my ($self, $logger, $event) = @_;
43  
44 23 my $object = $self->{'object'};
45 23 my $method = $event->{'level'};
46 23 ▶ if(!$object->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object->can($method)) 9 14 23
47 # Log::Log4perl has no notice(), critical(), alert() or
48 # emergency(); use the nearest method it does have
49 9 ▶ my @fallbacks = ($method eq 'notice') ? ('info')
Branch: 2 branches
BranchTrueFalseTotal
$method eq 'notice' ? : 3 6 9
$method =~ /^(?:critical|alert|emergency)$/ ? : 5 1 6
50 : ($method =~ /^(?:critical|alert|emergency)$/) ? ('fatal', 'error')
51 : ();
52 9 ($method) = grep { $object->can($_) } @fallbacks;
53 9 ▶ if(!defined($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $method) 3 6 9
54 3 Carp::croak(
55 ref($logger), ': ', ref($object),
56 " doesn't know how to deal with the $event->{level} message",
57 );
58 }
59 }
60 20 my $fields_text = $event->{'fields_text'};
61 $object->$method(
62 20 ▶ @{$event->{'parts'}},
Branch: 2 branches
BranchTrueFalseTotal
defined $fields_text ? : 1 19 20
length $event->{'message'} ? : 1 0 1
63 20 (defined($fields_text) ? ((length($event->{'message'}) ? ' ' : '') . $fields_text) : ()),
64 );
65 }
66  
67 =head1 AUTHOR
68  
69 Nigel Horne C<njh@nigelhorne.com>
70  
71 =head1 SEE ALSO
72  
73 L<Log::Abstraction>, L<Log::Abstraction::Backend>
74  
75 =head1 COPYRIGHT AND LICENSE
76  
77 Copyright (C) 2025-2026 Nigel Horne
78  
79 Usage is subject to the GPL2 licence terms.
80 If you use it,
81 please let me know.
82  
83 =cut
84  
85 42 1;
« blib/lib/Log/Abstraction/Backend/Journald.pm ↑ Summary blib/lib/Log/Any/Adapter/Abstraction.pm »