Remote/Logging/Logger.pm

filter: Statementstmt 18.0% 15 / 83 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 15.0% 3 / 20 Podpod 0.0% 0 / 1 totaltotal 14.1% 18 / 127 CC 12 SCAR 21.7
CC
12
Cov
14%
_build__level_active
30.0
_render_remote
12.0
AUTOLOAD
9.4
CRAP 8.8
« Remote/FatNode.pm ↑ Summary Remote/Proxy.pm »
1 package Object::Remote::Logging::Logger;
2  
3 17 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #TODO sigh invoking a logger with a log level name the same
7 #as an attribute could happen - restrict attributes to _ prefix
8 #and restrict log levels to not start with out that prefix?
9 0 ▶has format => ( is => 'ro', required => 1, default => sub { '%l: %s' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
10 17 has level_names => ( is => 'ro', required => 1 );
11 0 ▶has min_level => ( is => 'ro', required => 1, default => sub { 'info' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
12 17 has max_level => ( is => 'lazy', required => 1 );
13 17 has _level_active => ( is => 'lazy' );
14  
15 #just a stub so it doesn't get to AUTOLOAD
16 0 ▶sub BUILD { }
Subroutine BUILD: not called CC=1 SCAR=0.0
Pod undocumented
17 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
18  
19 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=22.4
20 0 my $self = shift;
21 0 (my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
22  
23 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 0 ▶ if ($method =~ m/^_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^_/) 0 0 0
26 0 croak "invalid method name $method for " . ref($self);
27 }
28  
29 0 ▶ if ($method =~ m/^is_(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^is_(.+)/) 0 0 0
30 0 my $level_name = $1;
31 0 my $is_method = "is_$level_name";
32 0 ▶ *{$is_method} = sub { shift(@_)->_level_active->{$level_name} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 return $self->$is_method;
34 }
35  
36 0 my $level_name = $method;
37 0 ▶ *{$level_name} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
38 0 my $self = shift;
39 0 ▶ unless(exists($self->_level_active->{$level_name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_level_active->{$level_name}) 0 0 0
40 0 croak "$level_name is not a valid log level name";
41 }
42  
43 0 $self->_log($level_name, @_);
44 0 };
45  
46 0 return $self->$level_name(@_);
47 }
48  
49 0 ▶sub _build_max_level {
Subroutine _build_max_level: not called CC=1 SCAR=6.9
50 0 my ($self) = @_;
51 0 return $self->level_names->[-1];
52 }
53  
54 0 ▶sub _build__level_active {
Subroutine _build__level_active: not called CC=5 SCAR=34.0
55 0 my ($self) = @_;
56 0 my $should_log = 0;
57 0 my $min_level = $self->min_level;
58 0 my $max_level = $self->max_level;
59 0 my %active;
60  
61 0 foreach my $level (@{$self->level_names}) {
62 0 ▶ if($level eq $min_level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level eq $min_level) 0 0 0
63 0 $should_log = 1;
64 }
65  
66 0 $active{$level} = $should_log;
67  
68 0 ▶ if (defined $max_level && $level eq $max_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_level and $level eq $max_level) 0 0 0
Condition0%0/3
defined $max_level and $level eq $max_level
!ll&&!rl&&r
000
MC/DC0%0/2
defined $max_level and $level eq $max_level
defined $max_level $level eq $max_level
Truth table0%0/3
A and B
A= defined $max_levelB= $level eq $max_level
ABresult
0X0
100
111
69 0 $should_log = 0;
70 }
71 }
72  
73 0 return \%active;
74 }
75  
76 0 ▶sub _log {
Subroutine _log: not called CC=1 SCAR=6.9
77 0 my ($self, $level, $content, $metadata_in) = @_;
78 0 my %metadata = %$metadata_in;
79 0 my $rendered = $self->_render($level, \%metadata, @$content);
80 0 $self->_output($rendered);
81 }
82  
83 0 ▶sub _create_format_lookup {
Subroutine _create_format_lookup: not called CC=2 SCAR=17.9
84 0 my ($self, $level, $metadata, $content) = @_;
85 0 my $method = $metadata->{method};
86  
87 0 ▶ $method = '(none)' unless defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 0 0
88  
89 return {
90 '%' => '%', 'n' => "\n",
91 t => $self->_render_time($metadata->{timestamp}),
92 r => $self->_render_remote($metadata->{object_remote}),
93 s => $self->_render_log(@$content), l => $level,
94 c => $metadata->{exporter}, p => $metadata->{caller_package}, m => $method,
95 f => $metadata->{filename}, i => $metadata->{line},
96 h => $metadata->{hostname}, P => $metadata->{pid},
97 0 };
98 }
99  
100 0 ▶sub _get_format_var_value {
Subroutine _get_format_var_value: not called CC=2 SCAR=17.9
101 0 my ($self, $name, $data) = @_;
102 0 my $val = $data->{$name};
103 0 ▶ return $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
104 0 return '(undefined)';
105 }
106  
107 0 ▶sub _render_time {
Subroutine _render_time: not called CC=1 SCAR=6.9
108 0 my ($self, $time) = @_;
109 0 return scalar(localtime($time));
110 }
111  
112 0 ▶sub _render_remote {
Subroutine _render_remote: not called CC=3 SCAR=24.8
113 0 my ($self, $remote) = @_;
114 0 ▶ return 'local' unless defined $remote;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $remote 0 0 0
115 0 my $conn_id = $remote->{connection_id};
116 0 ▶ $conn_id = '(uninit)' unless defined $conn_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn_id 0 0 0
117 0 return "remote #$conn_id";
118 }
119  
120 0 ▶sub _render_log {
Subroutine _render_log: not called CC=1 SCAR=6.9
121 0 my ($self, @content) = @_;
122 0 return join('', @content);
123 }
124 0 ▶sub _render {
Subroutine _render: not called CC=1 SCAR=6.9
125 0 my ($self, $level, $metadata, @content) = @_;
126 0 my $var_table = $self->_create_format_lookup($level, $metadata, [@content]);
127 0 my $template = $self->format;
128  
129 0 $template =~ s/%([\w%])/$self->_get_format_var_value($1, $var_table)/ge;
130  
131 0 chomp($template);
132 0 $template =~ s/\n/\n /g;
133 0 $template .= "\n";
134 0 return $template;
135 }
136  
137 0 ▶sub _output {
Subroutine _output: not called CC=1 SCAR=6.9
138 0 my ($self, $content) = @_;
139 0 print STDERR $content;
140 }
141  
142 17 1;
143  
144 __END__
« Remote/FatNode.pm ↑ Summary Remote/Proxy.pm »