blib/lib/Log/Any/Adapter/Sentry/Raven.pm

filter: Statementstmt 100% 69 / 69 Branchbran 85.0% 17 / 20 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 2 totaltotal 91.2% 114 / 125 CC 16 SCAR 14.8
CC
16
Cov
94%
structured
7.3
init
7.0
_get_stack_trace
4.0
CRAP 4.4
↑ Summary
1 package Log::Any::Adapter::Sentry::Raven;
2  
3 # ABSTRACT: Log::Any::Adapter for Sentry::Raven
4 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = 'v0.0.5'; # VERSION
6  
7 #pod =head1 SYNPOSIS
8 #pod
9 #pod use Log::Any::Adapter;
10 #pod Log::Any::Adapter->set('Sentry::Raven',
11 #pod sentry => Sentry::Raven->new(
12 #pod sentry_dsn => $dsn,
13 #pod environment => 'production',
14 #pod ...
15 #pod )
16 #pod );
17 #pod
18 #pod =head1 DESCRIPTION
19 #pod
20 #pod This is a backend to L<Log::Any> for L<Sentry::Raven>.
21 #pod
22 #pod When logging, it does its best to provide a L<Devel::StackTrace> to
23 #pod identify your message. To accomplish this, it uses L<Devel::StackTrace::Extract>
24 #pod to pull a trace from your message (if you pass multiple message arguments, it
25 #pod won't attempt this).
26 #pod Failing that, it will append a new C<Devel::StackTrace>.
27 #pod
28 #pod It takes two arguments:
29 #pod
30 #pod =over
31 #pod
32 #pod =item sentry (REQUIRED)
33 #pod
34 #pod An instantiated L<Sentry::Raven> object.
35 #pod Note that if you set any sentry-specific context directly through the sentry
36 #pod object, it will be picked up here eg.
37 #pod
38 #pod $sentry->add_context( Sentry::Raven->request_context($url, %p) )
39 #pod
40 #pod =item log_level (OPTIONAL)
41 #pod
42 #pod The minimum log_level to log. Defaults to C<trace> (everything).
43 #pod
44 #pod =back
45 #pod
46 #pod Any L<Log::Any/Log context data> will be sent to Sentry as tags.
47 #pod
48 #pod =head1 SEE ALSO
49 #pod
50 #pod L<Log::Any>, L<Sentry::Raven>
51 #pod
52 #pod =cut
53  
54 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 2 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use Devel::StackTrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use Devel::StackTrace::Extract qw(extract_stack_trace);
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use Log::Any::Adapter::Util qw(make_method numeric_level);
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use Sentry::Raven;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 2 ▶use base qw(Log::Any::Adapter::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 7 ▶sub init {
Subroutine init: called CC=7 SCAR=19.5
Pod undocumented
67 7 my $self = shift;
68  
69 7 my $sentry = $self->{sentry};
70 7 ▶ croak "An initialized Sentry::Raven object must be passed as the 'sentry' arg"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($sentry) and $sentry->isa("Sentry::Raven") 2 5 7
Condition100%3/3
builtin::blessed($sentry) and $sentry->isa("Sentry::Raven")
!ll&&!rl&&r
115
MC/DC100%2/2
builtin::blessed($sentry) and $sentry->isa("Sentry::Raven")
builtin::blessed($sentry) $sentry->isa("Sentry::Raven")
Truth table100%3/3
A and B
A= builtin::blessed($sentry)B= $sentry->isa("Sentry::Raven")
ABresult
0X0
100
111
71 unless blessed($sentry) && $sentry->isa('Sentry::Raven');
72  
73 # copied from Log::Any::Adapter::Stderr
74 5 ▶ if ( exists $self->{log_level} && $self->{log_level} =~ /\D/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"log_level"} and $self->{"log_level"} =~ /\D/) 3 2 5
Condition100%3/3
exists $self->{"log_level"} and $self->{"log_level"} =~ /\D/
!ll&&!rl&&r
113
MC/DC100%2/2
exists $self->{"log_level"} and $self->{"log_level"} =~ /\D/
exists $self->{"log_level"} $self->{"log_level"} =~ /\D/
Truth table100%3/3
A and B
A= exists $self->{"log_level"}B= $self->{"log_level"} =~ /\D/
ABresult
0X0
100
111
75 3 my $numeric_level = numeric_level( $self->{log_level} );
76 3 ▶ if ( !defined($numeric_level) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $numeric_level) 1 2 3
77 1 carp( sprintf 'Invalid log level "%s". Defaulting to "%s"', $self->{log_level}, 'trace' );
78 }
79 3 $self->{log_level} = $numeric_level;
80 }
81 5 ▶ if ( !defined $self->{log_level} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"log_level"}) 2 3 5
82 2 $self->{log_level} = numeric_level('trace');
83 }
84 }
85  
86 5 ▶sub structured {
Subroutine structured: called CC=7 SCAR=19.9
Pod undocumented
87 5 my ($self, $level, $category, @log_args) = @_;
88  
89 5 my $is_level = "is_$level";
90 5 ▶ return unless $self->$is_level;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$is_level 0 5 5
91  
92 5 my $log_any_context = {};
93 5 ▶ if ((ref $log_args[-1]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $log_args[-1] eq "HASH") 1 4 5
94 1 $log_any_context = pop @log_args;
95 }
96  
97 5 my $stack_trace = _get_stack_trace(@log_args);
98 5 my $log_message = join "\n" => @log_args;
99  
100 5 my $sentry_severity = $level;
101 5 for ($sentry_severity) {
102 5 ▶ s/trace/debug/ or
Branch: 1 branch
BranchTrueFalseTotal
s/trace/debug/ or s/notice/info/ or s/critical|alert|emergency/fatal/ 0 5 5
Condition33%1/3
s/trace/debug/ or s/notice/info/
l!l&&r!l&&!r
005
MC/DC0%0/2
s/trace/debug/ or s/notice/info/
s/trace/debug/ s/notice/info/
Truth table33%1/3
A or B
A= s/trace/debug/B= s/notice/info/
ABresult
1X1
011
000
103 s/notice/info/ or
104 s/critical|alert|emergency/fatal/
105 }
106  
107 5 my @message_args = (
108 $log_message,
109 level => $sentry_severity,
110 tags => $log_any_context,
111 );
112  
113 5 ▶ if ($stack_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack_trace) 5 0 5
114 5 push @message_args, Sentry::Raven->stacktrace_context($stack_trace);
115 }
116  
117 # https://docs.sentry.io/data-management/event-grouping/
118 5 $self->{sentry}->capture_message( @message_args );
119 }
120  
121 2 for my $method ( Log::Any->detection_methods() ) {
122 18 my $method_base = substr($method, 3); # chop of is_
123 18 my $method_level = numeric_level($method_base);
124 make_method(
125 $method,
126 18 ▶ sub { return $method_level <= $_[0]->{log_level} },
Subroutine __ANON__: called CC=1 SCAR=0.0
127 18 );
128 }
129  
130 5 ▶sub _get_stack_trace {
Subroutine _get_stack_trace: called CC=4 SCAR=13.9
131 5 my @message_parts = @_;
132  
133 5 my $trace;
134 5 ▶ if (@message_parts == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@message_parts == 1) 4 1 5
135 4 $trace = extract_stack_trace($message_parts[0]);
136 }
137 5 ▶ unless (blessed($trace) && $trace->isa('Devel::StackTrace')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($trace) and $trace->isa("Devel::StackTrace")) 4 1 5
Condition66%2/3
builtin::blessed($trace) and $trace->isa("Devel::StackTrace")
!ll&&!rl&&r
401
MC/DC50%1/2
builtin::blessed($trace) and $trace->isa("Devel::StackTrace")
builtin::blessed($trace) $trace->isa("Devel::StackTrace")
Truth table66%2/3
A and B
A= builtin::blessed($trace)B= $trace->isa("Devel::StackTrace")
ABresult
0X0
100
111
138 4 $trace = Devel::StackTrace->new;
139 }
140  
141 5 return $trace;
142 }
143  
144 2 1;
145  
146 __END__
↑ Summary