UnicornLogger.pm

filter: Statementstmt 96.3% 80 / 83 Branchbran 90.9% 20 / 22 Conditioncond 80.0% 12 / 15 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 100% 12 / 12 Podpod 75.0% 3 / 4 totaltotal 91.7% 133 / 145 CC 20 SCAR 14.8
CC
20
Cov
93%
print
8.0
_build_structured_logger
6.0
query_start
3.3
CRAP 4.4
↑ Summary UnicornLogger/FromProfile.pm »
1 package DBIx::Class::UnicornLogger;
2 5 $DBIx::Class::UnicornLogger::VERSION = '0.001004';
3 # ABSTRACT: Pretty Printing DebugObj with nicer logging features
4  
5 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 extends 'DBIx::Class::Storage::Statistics';
7  
8 5 ▶use SQL::Abstract::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Log::Structured;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Log::Sprintf;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 my %code_to_method = (
13 C => 'log_package',
14 c => 'log_category',
15 d => 'log_date',
16 F => 'log_file',
17 H => 'log_host',
18 L => 'log_line',
19 l => 'log_location',
20 M => 'log_subroutine',
21 P => 'log_pid',
22 p => 'log_priority',
23 r => 'log_milliseconds_since_start',
24 R => 'log_milliseconds_since_last_log',
25 T => 'log_stacktrace',
26 );
27  
28 8 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=3 SCAR=11.0
Pod undocumented
29 8 my ($self, @rest) = @_;
30  
31 my %args = (
32 @rest == 1
33 8 ▶ ? %{$rest[0]}
Branch: 1 branch
BranchTrueFalseTotal
@rest == 1 ? : 8 0 8
34 : @rest
35 );
36  
37 8 ▶ $args{_sqlat} = SQL::Abstract::Tree->new($args{tree} || {});
Condition100%2/2
$args{'tree'} || {}
l!l
62
MC/DC100%1/1
$args{'tree'} || {}
$args{'tree'}
Truth table100%2/2
A
A= $args{'tree'}
Aresult
11
00
38  
39 8 return \%args
40 }
41  
42 5 has _sqlat => (
43 is => 'ro',
44 );
45  
46 5 has _clear_line_str => (
47 is => 'ro',
48 init_arg => 'clear_line',
49 );
50  
51 5 has _executing_str => (
52 is => 'ro',
53 init_arg => 'executing',
54 );
55  
56 5 has _show_progress => (
57 is => 'ro',
58 init_arg => 'show_progress',
59 );
60  
61 has _last_sql => (
62 is => 'rw',
63 8 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 5 init_arg => undef,
65 );
66  
67 5 has _squash_repeats => (
68 is => 'ro',
69 init_arg => 'squash_repeats',
70 );
71  
72 5 has _structured_logger => (
73 is => 'rw',
74 lazy => 1,
75 builder => '_build_structured_logger',
76 );
77  
78 6 ▶sub _build_structured_logger {
Subroutine _build_structured_logger: called CC=6 SCAR=17.9
79 6 my $self = shift;
80  
81 6 ▶ if ($self->_format || $self->_multiline_format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_format or $self->_multiline_format) 3 3 6
Condition66%2/3
$self->_format or $self->_multiline_format
l!l&&r!l&&!r
303
MC/DC50%1/2
$self->_format or $self->_multiline_format
$self->_format $self->_multiline_format
Truth table66%2/3
A or B
A= $self->_formatB= $self->_multiline_format
ABresult
1X1
011
000
82 3 ▶ my $format = $self->_format || '%m';
Condition50%1/2
$self->_format || '%m'
l!l
30
MC/DC0%0/1
$self->_format || '%m'
$self->_format
Truth table50%1/2
A
A= $self->_format
Aresult
11
00
83  
84 3 my $log_sprintf = Log::Sprintf->new({ format => $format });
85  
86 3 ▶ my $per_line_log_sprintf = Log::Sprintf->new({
Branch: 1 branch
BranchTrueFalseTotal
if $self->_multiline_format 2 1 3
87 format => $self->_multiline_format
88 }) if $self->_multiline_format;
89  
90 3 my %formats = %{{
91 9 map { $_->{conversion} => 1 }
92 18 grep { ref $_ }
93 5 map @{$log_sprintf->_formatter->format_hunker->($log_sprintf, $_)},
94 grep $_,
95 $log_sprintf->{format},
96 $per_line_log_sprintf->{format}
97 3 }};
98  
99 my $sub = $self->_multiline_format
100 2 ▶ ? sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
101 2 my %struc = %{$_[1]};
102 2 my (@msg, undef) = split /\n/, delete $struc{message};
103 2 $self->debugfh->print($log_sprintf->sprintf({
104 %struc,
105 message => shift @msg,
106 }) . "\n");
107 $self->debugfh->print($per_line_log_sprintf->sprintf({
108 %struc,
109 message => $_,
110 2 }) . "\n") for @msg;
111 }
112 1 ▶ : sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
113 1 my %struc = %{$_[1]};
114 1 my (@msg, undef) = split /\n/, delete $struc{message};
115 $self->debugfh->print($log_sprintf->sprintf({
116 %struc,
117 message => $_,
118 1 }) . "\n") for @msg;
119 3 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$self->_multiline_format ? : 2 1 3
120 return
121 Log::Structured->new({
122 category => 'DBIC',
123 priority => 'TRACE',
124 caller_depth => 2,
125 log_event_listeners => [$sub],
126 3 map { $code_to_method{$_} => 1 }
127 3 grep { exists $code_to_method{$_} }
128 keys %formats
129 })
130 }
131 }
132  
133 5 has _format => (
134 is => 'ro',
135 init_arg => 'format',
136 );
137  
138 5 has _multiline_format => (
139 is => 'ro',
140 init_arg => 'multiline_format',
141 );
142  
143 11 ▶sub print {
Subroutine print: called CC=8 SCAR=20.8
Pod documented
144 11 my $self = shift;
145 11 my $string = shift;
146 11 ▶ my $bindargs = shift || [];
Condition50%1/2
shift() || []
l!l
110
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
147  
148 11 my ($lw, $lr);
149 11 ($lw, $string, $lr) = $string =~ /^(\s*)(.+?)(\s*)$/s;
150  
151 11 ▶ local $self->_sqlat->{fill_in_placeholders} = 0 if defined $bindargs->[0]
Branch: 1 branch
BranchTrueFalseTotal
if defined $bindargs->[0] and $bindargs->[0] eq "'__BULK_INSERT__'" 1 10 11
Condition100%3/3
defined $bindargs->[0] and $bindargs->[0] eq "'__BULK_INSERT__'"
!ll&&!rl&&r
191
MC/DC100%2/2
defined $bindargs->[0] and $bindargs->[0] eq "'__BULK_INSERT__'"
defined $bindargs->[0] $bindargs->[0] eq "'__BULK_INSERT__'"
Truth table100%3/3
A and B
A= defined $bindargs->[0]B= $bindargs->[0] eq "'__BULK_INSERT__'"
ABresult
0X0
100
111
152 && $bindargs->[0] eq q('__BULK_INSERT__');
153  
154 11 my $use_placeholders = !!$self->_sqlat->fill_in_placeholders;
155  
156 11 my $sqlat = $self->_sqlat;
157 11 my $formatted;
158 11 ▶ if ($self->_squash_repeats && $self->_last_sql eq $string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_squash_repeats and $self->_last_sql eq $string) { } 3 8 11
Condition100%3/3
$self->_squash_repeats and $self->_last_sql eq $string
!ll&&!rl&&r
263
MC/DC100%2/2
$self->_squash_repeats and $self->_last_sql eq $string
$self->_squash_repeats $self->_last_sql eq $string
Truth table100%3/3
A and B
A= $self->_squash_repeatsB= $self->_last_sql eq $string
ABresult
0X0
100
111
159 3 my ( $l, $r ) = @{ $sqlat->placeholder_surround };
160 3 $formatted = '... : ' . join(', ', map "$l$_$r", @$bindargs)
161 } else {
162 8 $self->_last_sql($string);
163 8 $formatted = $sqlat->format($string, $bindargs);
164 8 ▶ $formatted = "$formatted : " . join ', ', @{$bindargs}
Branch: 1 branch
BranchTrueFalseTotal
unless $use_placeholders 2 6 8
165 unless $use_placeholders;
166 }
167  
168 11 ▶ if ($self->_structured_logger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_structured_logger) { } 3 8 11
169 3 $self->_structured_logger->log_event({
170 message => "$lw$formatted$lr",
171 })
172 } else {
173 8 $self->next::method("$lw$formatted$lr", @_)
174 }
175 }
176  
177 11 ▶sub query_start {
Subroutine query_start: called CC=3 SCAR=12.0
Pod documented
178 11 my ($self, $string, @bind) = @_;
179  
180 11 ▶ if(defined $self->callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->callback) 0 11 11
181 0 $string =~ m/^(\w+)/;
182 0 $self->callback->($1, "$string: ".join(', ', @bind)."\n");
183 0 return;
184 }
185  
186 11 $string =~ s/\s+$//;
187  
188 11 $self->print("$string\n", \@bind);
189  
190 11 ▶ $self->debugfh->print($self->_executing_str) if $self->_show_progress
Branch: 1 branch
BranchTrueFalseTotal
if $self->_show_progress 1 10 11
191 }
192  
193 3 ▶sub query_end {
Subroutine query_end: called CC=2 SCAR=6.9
Pod documented
194 3 ▶ $_[0]->debugfh->print($_[0]->_clear_line_str) if $_[0]->_show_progress
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->_show_progress 1 2 3
195 }
196  
197 5 1;
198  
199 __END__
↑ Summary UnicornLogger/FromProfile.pm »