lib/ControlFreak/Logger.pm

filter: Statementstmt 61.4% 59 / 96 Branchbran 26.9% 7 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 62.5% 15 / 24 Podpod 0.0% 0 / 9 totaltotal 50.6% 81 / 160 CC 15 SCAR 14.5
CC
15
Cov
53%
proxy_log
12.0
unset
12.0
__ANON__
6.0
CRAP 4.3
« lib/ControlFreak/Service.pm ↑ Summary lib/ControlFreak/Console.pm »
1 package ControlFreak::Logger;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Log::Log4perl();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use Object::Tiny qw{ config_file };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Params::Util qw{ _STRING };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 our $CURRENT_SVC_PID;
13  
14 Log::Log4perl::Layout::PatternLayout::add_global_cspec(
15 0 ▶ 'S', sub { $ControlFreak::Logger::CURRENT_SVC_PID || "-" },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$ControlFreak::Logger::CURRENT_SVC_PID or "-" 0 0 0
16 8 );
17  
18 14 ▶sub new {
Subroutine new: called CC=3 SCAR=14.2
Pod undocumented
19 14 my $class = shift;
20 14 my $logger = $class->SUPER::new(@_);
21  
22 14 ▶ if (my $config_file = $logger->config_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config_file = $logger->config_file) { } 0 14 14
23 0 ▶ unless (-e $config_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $config_file) 0 0 0
24 0 croak "cannot find '$config_file'";
25 }
26 0 Log::Log4perl->init($config_file);
27 }
28 else {
29 14 Log::Log4perl->init( $logger->default_config );
30 }
31 14 return $logger;
32 }
33  
34 0 ▶sub safe_reinit {
Subroutine safe_reinit: not called CC=2 SCAR=14.2
Pod undocumented
35 0 my $logger = shift;
36 0 ▶ unless ($logger->config_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($logger->config_file) 0 0 0
37 0 $logger->warn("Ignored USR1, running with file-less config");
38 0 return;
39 }
40 0 $logger->info("Reloading log config");
41 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 Log::Log4perl->init($logger->config_file);
43 0 $logger->info("Log config reloaded");
44 }
45 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=1.2
46 ## damn ugly
47 0 warn "reloading config failed";
48 8 ▶ use Log::Log4perl::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 0 Log::Log4perl::Config->_init(undef, $Log::Log4perl::Config::OLD_CONFIG);
50 0 $logger->error("There is an error in my config. Aborting. ($_)");
51 0 };
52 }
53  
54 0 ▶sub default_config {
Subroutine default_config: not called CC=1 SCAR=6.9
Pod undocumented
55 0 return \<<'EOFC';
56 log4perl.rootLogger=INFO, ALL
57 log4perl.appender.ALL=Log::Log4perl::Appender::File
58 log4perl.appender.ALL.filename=sub { $ENV{CFKD_HOME} . "/cfkd.log" }
59 log4perl.appender.ALL.mode=append
60 log4perl.appender.ALL.layout=PatternLayout
61 # %S = service pid
62 log4perl.appender.ALL.layout.ConversionPattern=%S %p %L %c - %m%n
63 EOFC
64 }
65  
66 280 ▶sub log_handle {
Subroutine log_handle: called CC=1 SCAR=0.0
Pod undocumented
67 280 my $logger = shift;
68 280 Log::Log4perl->get_logger(@_);
69 }
70  
71 70 ▶sub svc_watcher {
Subroutine svc_watcher: called CC=2 SCAR=6.9
Pod undocumented
72 70 my $logger = shift;
73 ## type is 'out' our 'err'
74 70 my ($type, $svc) = @_;
75  
76 ## configurable?
77 70 ▶ my $logmethod = $type eq 'err' ? 'error' : 'info';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'err' ? : 35 35 70
78 77 ▶ my $watcher_cb = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
79 77 my $msg = shift;
80 77 ▶ return unless defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 66 11 77
81 11 ▶ chomp $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 11 0 11
82 11 return $logger->_svclog($logmethod, $type, $svc, $msg);
83 70 };
84 70 return $watcher_cb;
85 }
86  
87 11 ▶sub _svclog {
Subroutine _svclog: called CC=2 SCAR=7.0
88 11 my $logger = shift;
89 11 my ($logmethod, $type, $svc, $msg) = @_;
90  
91 11 my $svcname = $svc->name;
92 ## for 'S' cspec
93 11 local $CURRENT_SVC_PID = $svc->pid;
94 11 my $log_handle = $logger->log_handle("service.$svcname.$type");
95 11 ▶ chomp $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 11 0 11
96 11 $log_handle->$logmethod($msg);
97 11 return;
98 }
99  
100 8 for my $lvl (qw/trace debug info warn error fatal/) {
101 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
102 48 ▶ *{$lvl} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
103 268 local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
104 268 shift->log_handle->$lvl(@_);
105 }
106 48 }
107  
108 0 ▶sub proxy_log {
Subroutine proxy_log: not called CC=3 SCAR=24.8
Pod undocumented
109 0 my $logger = shift;
110 0 my ($data) = @_;
111 0 my ($type, $proxy, $msg) = @$data;
112  
113 0 my $proxyname = $proxy->name;
114 0 ▶ my $logmethod = $type eq 'err' ? 'error' : 'info';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'err' ? : 0 0 0
115  
116 0 my $log_handle = $logger->log_handle("proxy.$proxyname.$type");
117 0 ▶ chomp $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 0 0 0
118 0 $log_handle->$logmethod($msg);
119 }
120  
121 0 ▶sub proxy_svc_log {
Subroutine proxy_svc_log: not called CC=2 SCAR=17.9
Pod undocumented
122 0 my $logger = shift;
123 0 my ($data) = @_;
124 0 my ($type, $svc, $msg) = @$data;
125 0 ▶ my $logmethod = $type eq 'err' ? 'error' : 'info';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'err' ? : 0 0 0
126 0 $logger->_svclog($logmethod, $type, $svc, $msg);
127 }
128  
129 0 ▶sub set_config {
Subroutine set_config: not called CC=2 SCAR=17.9
Pod undocumented
130 0 my $logger = shift;
131 0 ▶ my $configfile = _STRING(shift) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $configfile = &_STRING(shift()) or return 0 0 0
132 ## reinit
133 0 Log::Log4perl->init($configfile);
134 0 return 1;
135 }
136  
137 0 ▶sub unset {
Subroutine unset: not called CC=3 SCAR=24.8
Pod undocumented
138 0 my $logger = shift;
139 0 my $what = _STRING(shift);
140 0 ▶ return unless $what or $what eq 'configfile';
Branch: 1 branch
BranchTrueFalseTotal
unless $what or $what eq "configfile" 0 0 0
Condition0%0/3
$what or $what eq "configfile"
l!l&&r!l&&!r
000
MC/DC0%0/2
$what or $what eq "configfile"
$what $what eq "configfile"
Truth table0%0/3
A or B
A= $whatB= $what eq "configfile"
ABresult
1X1
011
000
141 0 return 1;
142 }
143  
144 =head1 NAME
145  
146 ControlFreak::Logger - All about logging
147  
148 =cut
149  
150 =head1 SYNOPSIS
151  
152 =head1 AUTHOR
153  
154 Yann Kerherve <yannk@cpan.org>
155  
156 =cut
157  
158 8 "zog-zog";
« lib/ControlFreak/Service.pm ↑ Summary lib/ControlFreak/Console.pm »