| 1 | package TaskPipe::LoggerManager; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 40 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 40 | with 'MooseX::ConfigCascade'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 40 | ▶ | use Log::Log4perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 40 | ▶ | use TaskPipe::PathSettings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use TaskPipe::LoggerManager::Settings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use Template::Nest; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 40 | ▶ | use TaskPipe::RunInfo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 40 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 40 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 40 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 40 | ▶ | use File::Path 'make_path'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 40 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | has path_settings => (is => 'ro', isa => 'TaskPipe::PathSettings', default => sub{ | ||||||||||||||||||||||||||||
| 18 | 1134 | ▶ | TaskPipe::PathSettings->new; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 40 | }); | |||||||||||||||||||||||||||
| 20 | has settings => (is => 'ro', isa => 'TaskPipe::LoggerManager::Settings', default => sub{ | ||||||||||||||||||||||||||||
| 21 | 1134 | ▶ | TaskPipe::LoggerManager::Settings->new; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 40 | }); | |||||||||||||||||||||||||||
| 23 | 40 | has foreground => (is => 'rw', isa => 'Bool', default => 1); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub init_logger{ | ||||||||||||||||||||||||||||
| 26 | 849 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
init_logger: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 849 | Log::Log4perl->init( $self->logging_settings ); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | has run_info => (is => 'rw', isa => 'TaskPipe::RunInfo', default => sub{ | ||||||||||||||||||||||||||||
| 33 | 1134 | ▶ | TaskPipe::RunInfo->new; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 40 | }); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | #has job_id => (is => 'rw', isa => 'Int'); | ||||||||||||||||||||||||||||
| 37 | #has run_id => (is => 'rw', isa => 'Int'); | ||||||||||||||||||||||||||||
| 38 | #has thread_id => (is => 'rw', isa => 'Int',default => 1); | ||||||||||||||||||||||||||||
| 39 | #has task_name => (is => 'rw', isa => 'Str'); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | has custom_cspecs => (is => 'ro', isa => 'HashRef', default => sub{{ | ||||||||||||||||||||||||||||
| 42 | 1134 | ▶ | J => 'job_id', | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | #i => 'run_id', | ||||||||||||||||||||||||||||
| 44 | h => 'thread_id', | ||||||||||||||||||||||||||||
| 45 | N => 'task_name' | ||||||||||||||||||||||||||||
| 46 | 40 | }}); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | #has name => (is => 'rw', isa => 'Maybe[Str]', lazy => 1, default => sub{ | ||||||||||||||||||||||||||||
| 49 | # my $self = shift; | ||||||||||||||||||||||||||||
| 50 | # my $tid = $self->path_settings->project->task_identifier; | ||||||||||||||||||||||||||||
| 51 | # my ($name) = ref($self) =~ /$tid(\w+)$/; | ||||||||||||||||||||||||||||
| 52 | # return $name; | ||||||||||||||||||||||||||||
| 53 | #}); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub logging_settings{ | ||||||||||||||||||||||||||||
| 59 | 849 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
logging_settings: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 849 | my %c; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 849 | my $c = $self->settings; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 849 | my %s; | |||||||||||||||||||||||||||
| 66 | 849 | $s{rootLogger} = $c->log_level; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 849 | my $log_methods = $self->get_log_methods; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 849 | ▶ | if ( $log_methods->{screen} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 849 | $s{'rootLogger'}.=', SCREEN'; | |||||||||||||||||||||||||||
| 73 | 849 | $s{'appender.SCREEN'} = 'Log::Log4perl::Appender::Screen'; | |||||||||||||||||||||||||||
| 74 | 849 | $s{'appender.SCREEN.color.DEBUG'} = 'white'; | |||||||||||||||||||||||||||
| 75 | 849 | $s{'appender.SCREEN.color.INFO'} = 'bold white'; | |||||||||||||||||||||||||||
| 76 | 849 | $s{'appender.SCREEN.stderr'} = '0'; | |||||||||||||||||||||||||||
| 77 | 849 | $s{'appender.SCREEN.layout'} = 'Log::Log4perl::Layout::ColoredPatternLayout'; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 849 | $s{'appender.SCREEN.layout.ConversionPattern'}=$c->log_screen_pattern; | |||||||||||||||||||||||||||
| 80 | 849 | $s{'appender.SCREEN.layout.ColorMap'} = $self->build_color_map; | |||||||||||||||||||||||||||
| 81 | 849 | $s{'appender.SCREEN.layout.ColorMap'} =~ s/\n//g; | |||||||||||||||||||||||||||
| 82 | 849 | $s{'appender.SCREEN.layout.ColorStyle'} = 'continuous'; | |||||||||||||||||||||||||||
| 83 | 849 | $self->add_custom_cspecs( \%s,'SCREEN' ); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 849 | ▶ | if ( $log_methods->{file} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | $s{'rootLogger'}.=', LOGFILE'; | |||||||||||||||||||||||||||
| 89 | 0 | $s{'appender.LOGFILE'}='Log::Log4perl::Appender::File'; | |||||||||||||||||||||||||||
| 90 | 0 | $s{'appender.LOGFILE.filename'}= $self->get_log_path; | |||||||||||||||||||||||||||
| 91 | 0 | $s{'appender.LOGFILE.mode'}= $c->log_file_access; | |||||||||||||||||||||||||||
| 92 | 0 | $s{'appender.LOGFILE.layout'}='PatternLayout'; | |||||||||||||||||||||||||||
| 93 | 0 | $s{'appender.LOGFILE.layout.ConversionPattern'} = $c->log_file_pattern; | |||||||||||||||||||||||||||
| 94 | 0 | $self->add_custom_cspecs( \%s, 'LOGFILE' ); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # print "s: ".Dumper( \%s )."\n"; | ||||||||||||||||||||||||||||
| 98 | 849 | return \%s; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub get_log_methods{ | ||||||||||||||||||||||||||||
| 104 | 849 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_log_methods: called CC=6 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 849 | my $log_mode = $self->settings->log_mode; | |||||||||||||||||||||||||||
| 107 | 849 | my $log_methods = {'screen' => 1 }; | |||||||||||||||||||||||||||
| 108 | 849 | ▶ | $log_methods = {'file' => 1 } if $log_mode eq 'file'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 849 | ▶ | $log_methods = {'file' => 1 } if ( $log_mode eq 'shell' && $self->run_info->shell eq 'background'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$log_mode eq "shell" and $self->run_info->shell eq "background"
MC/DC0%0/2
$log_mode eq "shell" and $self->run_info->shell eq "background"
$log_mode eq "shell"
$self->run_info->shell eq "background"
Truth table33%1/3
A and B
A= $log_mode eq "shell"B= $self->run_info->shell eq "background"
| |||||||||||||||||||||||||||||
| 110 | 849 | ▶ | $log_methods->{'file'} = 1 if $log_mode eq 'both'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 849 | ▶ | $log_methods = {} if $log_mode eq 'none'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 849 | return $log_methods; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub add_custom_cspecs{ | ||||||||||||||||||||||||||||
| 119 | 849 | ▶ | my ($self,$s,$appender) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_custom_cspecs: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 849 | foreach my $cspec (keys %{$self->custom_cspecs}){ | |||||||||||||||||||||||||||
| 122 | 2547 | my $method = $self->custom_cspecs->{$cspec}; | |||||||||||||||||||||||||||
| 123 | 2547 | ▶ | my $var = $self->run_info->$method || '?'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->run_info->$method || '?'
MC/DC100%1/1
$self->run_info->$method || '?'
$self->run_info->$method
Truth table100%2/2
A
A= $self->run_info->$method
| |||||||||||||||||||||||||||||
| 124 | 2547 | $s->{"appender.$appender.layout.cspec.$cspec"} = "sub{ return '$var'; }"; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub get_log_path{ | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_log_path: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | my $dt = DateTime->now; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | my $nest = Template::Nest->new( template_hash => { | |||||||||||||||||||||||||||
| 135 | log_dir_format => +$self->settings->log_dir_format, | ||||||||||||||||||||||||||||
| 136 | filename_format => +$self->settings->log_filename_format | ||||||||||||||||||||||||||||
| 137 | }); | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | my @cmd; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | my $dir = $self->path_settings->path( | ||||||||||||||||||||||||||||
| 142 | 'log', $dt->ymd, | ||||||||||||||||||||||||||||
| 143 | +$nest->render({ | ||||||||||||||||||||||||||||
| 144 | NAME => 'log_dir_format', | ||||||||||||||||||||||||||||
| 145 | job_id => $self->run_info->job_id, | ||||||||||||||||||||||||||||
| 146 | 0 | cmd => +join('-',@{$self->run_info->cmd}) | |||||||||||||||||||||||||||
| 147 | }) | ||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | if ( ! -d $dir ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | try { | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | make_path( $dir ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | } catch { | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | confess "Could not create directory $dir: $_"; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | }; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | my $filename = $nest->render({ | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->run_info->thread_id || 1
MC/DC0%0/1
$self->run_info->thread_id || 1
$self->run_info->thread_id
Truth table0%0/2
A
A= $self->run_info->thread_id
| |||||||||||||||||||||||||||||
| 165 | NAME => 'filename_format', | ||||||||||||||||||||||||||||
| 166 | thread_id => +$self->run_info->thread_id || 1 | ||||||||||||||||||||||||||||
| 167 | }); | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | my $log_path = +File::Spec->catdir( $dir, $filename ); | |||||||||||||||||||||||||||
| 170 | #print "log path is: ".$log_path."\n"; | ||||||||||||||||||||||||||||
| 171 | 0 | return $log_path; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | sub build_color_map{ | ||||||||||||||||||||||||||||
| 178 | 849 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
build_color_map: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 849 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 849 | my @kv; | |||||||||||||||||||||||||||
| 183 | 849 | my %sc = %{$self->settings->log_screen_colors}; | |||||||||||||||||||||||||||
| 184 | 849 | foreach my $cspec ( keys %sc ){ | |||||||||||||||||||||||||||
| 185 | 5943 | my $cspec_str; | |||||||||||||||||||||||||||
| 186 | 5943 | ▶ | if ( ref $sc{$cspec} eq ref {} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | $cspec_str = $cspec | ||||||||||||||||||||||||||||
| 188 | .' => sub { my($val) = @_; my $case = ' | ||||||||||||||||||||||||||||
| 189 | 849 | .Dumper( $sc{$cspec} ) | |||||||||||||||||||||||||||
| 190 | .'; return +$case->{ $val }; }'; | ||||||||||||||||||||||||||||
| 191 | 849 | $cspec_str =~ s/\n//gs; | |||||||||||||||||||||||||||
| 192 | } else { | ||||||||||||||||||||||||||||
| 193 | 5094 | $cspec_str = "'".$cspec."' => '".$sc{$cspec}."',"; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | 5943 | push @kv,$cspec_str; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 849 | my $color_map = 'sub { return { '.join( ', ', @kv ).'}}'; | |||||||||||||||||||||||||||
| 199 | 849 | return $color_map; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head1 NAME | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | TaskPipe::LoggerManager - Logging manager for TaskPipe | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | It is not recommended to use this module directly. See the general manpages for TaskPipe | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Tom Gracey <tomgracey@gmail.com> | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Copyright (c) Tom Gracey 2018 | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | TaskPipe is free software, licensed under | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | The GNU Public License Version 3 | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 40 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 226 | 40 | 1; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||