« blib/lib/Clustericious/Command/apache.pm
↑ Summary
blib/lib/Clustericious/Command/configdebug.pm »
| 1 | package Clustericious::Log::CommandLine; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 47 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 47 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 47 | ▶ | use Log::Log4perl qw(get_logger :levels); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 47 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | # ABSTRACT: Simple Command Line Interface for Log4perl | ||||||||||||||||||||||||||||||||||||||||||
| 9 | 47 | our $VERSION = '1.29'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 47 | my %init; # logconfig, loginit, logfile, logcategory, noinit | |||||||||||||||||||||||||||||||||||||||||
| 13 | my %options; # options set on command line | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 47 | my %levelmap = | |||||||||||||||||||||||||||||||||||||||||
| 16 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 17 | q => 'off', | ||||||||||||||||||||||||||||||||||||||||||
| 18 | quiet => 'off', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | v => 'info', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | verbose => 'info', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | d => 'debug' | ||||||||||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | sub import | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 46 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=24 SCAR=31.8
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 46 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 46 | my $caller = caller; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 46 | my @getoptlist; | |||||||||||||||||||||||||||||||||||||||||
| 31 | my $next; | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 46 | foreach (@_) | |||||||||||||||||||||||||||||||||||||||||
| 33 | { | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 92 | ▶ | if ($next) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | { | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 20 | $init{$next} = $_; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 20 | $next = undef; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 20 | next; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 72 | ▶ | /^:(log(?:config|file|init|category))$/ and $next = $1; # Grab next arg | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 72 | ▶ | /^(?:trace|:levels|:all)$/ and push(@getoptlist, 'trace:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 72 | ▶ | /^(?:debug|:levels|:all)$/ and push(@getoptlist, 'debug:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 72 | ▶ | /^(?:info|:levels|:all)$/ and push(@getoptlist, 'info:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 72 | ▶ | /^(?:warn|:levels|:all)$/ and push(@getoptlist, 'warn:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 72 | ▶ | /^(?:error|:levels|:all)$/ and push(@getoptlist, 'error:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 72 | ▶ | /^(?:fatal|:levels|:all)$/ and push(@getoptlist, 'fatal:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 72 | ▶ | /^(?:off|:levels|:all)$/ and push(@getoptlist, 'off:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 72 | ▶ | /^(?:quiet|:long|:all)$/ and push(@getoptlist, 'quiet:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 72 | ▶ | /^(?:verbose|:long|:all)$/ and push(@getoptlist, 'verbose:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 72 | ▶ | /^(?:q|:short|:all)$/ and push(@getoptlist, 'q:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 72 | ▶ | /^(?:v|:short|:all)$/ and push(@getoptlist, 'v:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 72 | ▶ | /^(?:d|:short|:all)$/ and push(@getoptlist, 'd:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 72 | ▶ | /^(?:loglevel|:logopts|:all)$/ and push(@getoptlist, 'loglevel:s@'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | /^(?:logconfig|:logopts|:all)$/ and | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 72 | ▶ | push(@getoptlist, 'logconfig=s' => \$init{logconfig}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | /^(?:logfile|:logopts|:all)$/ and | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 72 | ▶ | push(@getoptlist, 'logfile=s' => \$init{logfile}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 47 | ▶ | { no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | /^handlelogoptions$/ and | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 72 | ▶ | *{"$caller\::handlelogoptions"} = *handlelogoptions; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 72 | ▶ | /^:noinit$/ and $init{noinit} = 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 46 | my $getopt = Getopt::Long::Parser->new | |||||||||||||||||||||||||||||||||||||||||
| 75 | ( config => [qw(pass_through no_auto_abbrev | ||||||||||||||||||||||||||||||||||||||||||
| 76 | no_ignore_case)] ); | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 46 | $getopt->getoptions(\%options, @getoptlist); | |||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | # Allow: --option --option foo --option foo,bar | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 46 | ▶ | while (my ($opt, $cats) = each %options) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 39 | ▶ | $options{$opt} = [ map { length $_ ? split(',') : '' } @$cats ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # --loglevel category=level or --loglevel level | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 46 | foreach (@{$options{loglevel}}) | |||||||||||||||||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my ($category, $level) = /^([^=]*?)=?([^=]+)$/; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | push(@{$options{$level}}, $category); | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 46 | delete $options{loglevel}; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 47 | ▶ | no warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | INIT | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 46 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
INIT: called CC=12 SCAR=25.4
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 47 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 46 | ▶ | return if $init{noinit}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 43 | ▶ | if (defined $init{logconfig} and -f $init{logconfig} and -r _) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $init{"logconfig"} and -f $init{"logconfig"} and -r _
defined $init{"logconfig"} and -f $init{"logconfig"}
MC/DC33%1/3
defined $init{"logconfig"} and -f $init{"logconfig"} and -r _
defined $init{"logconfig"}
-f $init{"logconfig"}
-r _
Truth table50%2/4
A and B and C
A= defined $init{"logconfig"}B= -f $init{"logconfig"}C= -r _
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 4 | Log::Log4perl->init($init{logconfig}); | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | else | ||||||||||||||||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 39 | ▶ | if ($init{loginit} and not ref $init{loginit}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$init{"loginit"} and not ref $init{"loginit"}
MC/DC100%2/2
$init{"loginit"} and not ref $init{"loginit"}
$init{"loginit"}
not ref $init{"loginit"}
Truth table100%3/3
A and not(B)
A= $init{"loginit"}B= not ref $init{"loginit"}
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 7 | Log::Log4perl->init(\$init{loginit}); | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | ▶ | elsif ($init{loginit} and ref $init{loginit} eq 'ARRAY') | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$init{"loginit"} and ref $init{"loginit"} eq "ARRAY"
MC/DC0%0/2
$init{"loginit"} and ref $init{"loginit"} eq "ARRAY"
$init{"loginit"}
ref $init{"loginit"} eq "ARRAY"
Truth table66%2/3
A and B
A= $init{"loginit"}B= ref $init{"loginit"} eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | Log::Log4perl->easy_init(@{$init{loginit}}); | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | else | ||||||||||||||||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 32 | ▶ | my $init = ref $init{loginit} eq 'HASH' ? $init{loginit} : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 32 | ▶ | $init->{level} ||= $ERROR; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$init->{"level"} ||= $Clustericious::Log::CommandLine::ERROR
MC/DC0%0/2
$init->{"level"} ||= $Clustericious::Log::CommandLine::ERROR
$init->{"level"}
$Clustericious::Log::CommandLine::ERROR
Truth table66%2/3
A ||= B
A= $init->{"level"}B= $Clustericious::Log::CommandLine::ERROR
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 32 | ▶ | $init->{layout} ||= '[%-5p] %m%n'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$init->{"layout"} ||= "[%-5p] %m%n"
MC/DC100%1/1
$init->{"layout"} ||= "[%-5p] %m%n"
$init->{"layout"}
Truth table100%2/2
A
A= $init->{"layout"}
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 32 | Log::Log4perl->easy_init($init); | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 43 | handlelogoptions(); | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 47 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | sub handlelogoptions | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 49 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
handlelogoptions: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 49 | ▶ | if ($init{logfile}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 2 | my $logfile = $init{logfile}; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 2 | my $layout = '%d %c %m%n'; | |||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 2 | ▶ | if ($logfile =~ s/\|(.*)$//) # "logfilename|logpattern" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | { | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | $layout = $1; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 2 | my $file_appender = Log::Log4perl::Appender->new( | |||||||||||||||||||||||||||||||||||||||||
| 144 | "Log::Log4perl::Appender::File", | ||||||||||||||||||||||||||||||||||||||||||
| 145 | name => 'logfile', | ||||||||||||||||||||||||||||||||||||||||||
| 146 | filename => $logfile); | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 2 | $file_appender->layout(Log::Log4perl::Layout::PatternLayout->new( | |||||||||||||||||||||||||||||||||||||||||
| 149 | $layout)); | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 2 | get_logger('')->add_appender($file_appender); | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 49 | ▶ | while (my ($level, $vals) = each %options) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 41 | ▶ | $level = $levelmap{$level} if exists $levelmap{$level}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 41 | my $level_id = Log::Log4perl::Level::to_priority(uc $level); | |||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 41 | foreach my $category (@$vals) | |||||||||||||||||||||||||||||||||||||||||
| 161 | { | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 41 | ▶ | if ($category eq '') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | { | ||||||||||||||||||||||||||||||||||||||||||
| 164 | $category = defined($init{logcategory}) | ||||||||||||||||||||||||||||||||||||||||||
| 165 | ? $init{logcategory} | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 30 | ▶ | : $level_id >= $INFO ? '' : 'main'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 41 | ▶ | $category = '' if $category eq 'root'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 41 | get_logger($category)->level($level_id); | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 47 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||||||||||||||||