« blib/lib/HPC/Runner/Command/execute_job/Utils/Plugin.pm
↑ Summary
blib/lib/HPC/Runner/Command/stats/Logger/JSON/JSONOutput.pm »
| 1 | package HPC::Runner::Command::Utils::Log; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Log::Log4perl qw(:easy); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use IPC::Open3; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use IO::Select; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Symbol; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use DateTime; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use DateTime::Format::Duration; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Cwd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use File::Path qw(make_path); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use File::Slurp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | use MooseX::App::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use MooseX::Types::Path::Tiny qw/Path Paths AbsPath AbsFile/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use Path::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | =head1 HPC::Runner::Command::Utils::Log | ||||||||||
| 20 | |||||||||||
| 21 | Class for all logging attributes | ||||||||||
| 22 | |||||||||||
| 23 | =head2 Command Line Options | ||||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | =head3 logdir | ||||||||||
| 27 | |||||||||||
| 28 | Pattern to use to write out logs directory. Defaults to outdir/prunner_current_date_time/log1 .. log2 .. log3. | ||||||||||
| 29 | |||||||||||
| 30 | =cut | ||||||||||
| 31 | |||||||||||
| 32 | option 'logdir' => ( | ||||||||||
| 33 | is => 'rw', | ||||||||||
| 34 | isa => AbsPath, | ||||||||||
| 35 | coerce => 1, | ||||||||||
| 36 | lazy => 1, | ||||||||||
| 37 | required => 1, | ||||||||||
| 38 | default => sub { | ||||||||||
| 39 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 40 | 0 | $self->set_logdir; | |||||||||
| 41 | }, | ||||||||||
| 42 | documentation => 'Directory where logfiles are written.' | ||||||||||
| 43 | . ' Defaults to current_working_directory/prunner_current_date_time/log1 .. log2 .. log3', | ||||||||||
| 44 | trigger => sub { | ||||||||||
| 45 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 46 | 0 | my $val = shift; | |||||||||
| 47 | 0 | $self->_make_the_dirs($val); | |||||||||
| 48 | }, | ||||||||||
| 49 | 1 | ); | |||||||||
| 50 | |||||||||||
| 51 | =head3 show_process_id | ||||||||||
| 52 | |||||||||||
| 53 | Show process_id in each log file. This is useful for aggregating logs | ||||||||||
| 54 | |||||||||||
| 55 | =cut | ||||||||||
| 56 | |||||||||||
| 57 | 1 | option 'show_processid' => ( | |||||||||
| 58 | is => 'rw', | ||||||||||
| 59 | isa => 'Bool', | ||||||||||
| 60 | default => 0, | ||||||||||
| 61 | documentation => 'Show the process ID per logging message. ' | ||||||||||
| 62 | . 'This is useful when aggregating logs.', | ||||||||||
| 63 | ); | ||||||||||
| 64 | |||||||||||
| 65 | =head3 process_table | ||||||||||
| 66 | |||||||||||
| 67 | We also want to write all cmds and exit codes to a table | ||||||||||
| 68 | |||||||||||
| 69 | #TODO add a json format also | ||||||||||
| 70 | |||||||||||
| 71 | =cut | ||||||||||
| 72 | |||||||||||
| 73 | option 'process_table' => ( | ||||||||||
| 74 | isa => Path, | ||||||||||
| 75 | is => 'rw', | ||||||||||
| 76 | lazy => 1, | ||||||||||
| 77 | coerce => 1, | ||||||||||
| 78 | default => sub { | ||||||||||
| 79 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 80 | 0 | make_path( $self->logdir ); | |||||||||
| 81 | 0 | my $process_table = | |||||||||
| 82 | File::Spec->catdir( $self->logdir, "001-task_table.md" ); | ||||||||||
| 83 | 0 | $process_table = path($process_table); | |||||||||
| 84 | 0 | $process_table->touchpath; | |||||||||
| 85 | |||||||||||
| 86 | 0 | my $header = | |||||||||
| 87 | "|| Version || Scheduler Id || Jobname || Task Tags || ProcessID || ExitCode || Duration ||\n"; | ||||||||||
| 88 | 0 | ▶ | write_file( $process_table, { append => 1 }, $header ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | or $self->app_log->warn("Couldn't open process file! $!\n"); | ||||||||||
| 90 | |||||||||||
| 91 | 0 | return $process_table; | |||||||||
| 92 | }, | ||||||||||
| 93 | trigger => sub { | ||||||||||
| 94 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 95 | 0 | $self->process_table->touchpath; | |||||||||
| 96 | }, | ||||||||||
| 97 | 1 | ); | |||||||||
| 98 | |||||||||||
| 99 | =head3 metastr | ||||||||||
| 100 | |||||||||||
| 101 | JSON string passed from HPC::Runner::App::Scheduler. It describes the total number of jobs, processes, and job batches. | ||||||||||
| 102 | |||||||||||
| 103 | =cut | ||||||||||
| 104 | |||||||||||
| 105 | 1 | option 'metastr' => ( | |||||||||
| 106 | is => 'rw', | ||||||||||
| 107 | isa => 'Str', | ||||||||||
| 108 | documentation => q{Meta str passed from HPC::Runner::Command::Scheduler}, | ||||||||||
| 109 | required => 0, | ||||||||||
| 110 | ); | ||||||||||
| 111 | |||||||||||
| 112 | 1 | option 'logname' => ( | |||||||||
| 113 | isa => 'Str', | ||||||||||
| 114 | is => 'rw', | ||||||||||
| 115 | default => 'hpcrunner_logs', | ||||||||||
| 116 | required => 0, | ||||||||||
| 117 | ); | ||||||||||
| 118 | |||||||||||
| 119 | =head2 Internal Attributes | ||||||||||
| 120 | |||||||||||
| 121 | You shouldn't be calling these directly. | ||||||||||
| 122 | |||||||||||
| 123 | =cut | ||||||||||
| 124 | |||||||||||
| 125 | has 'dt' => ( | ||||||||||
| 126 | is => 'rw', | ||||||||||
| 127 | isa => 'DateTime', | ||||||||||
| 128 | 0 | ▶ | default => sub { return DateTime->now( time_zone => 'local' ); }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 129 | 1 | lazy => 1, | |||||||||
| 130 | ); | ||||||||||
| 131 | |||||||||||
| 132 | ##TODO Put this in its own class | ||||||||||
| 133 | ##Application log | ||||||||||
| 134 | has 'app_log' => ( | ||||||||||
| 135 | is => 'rw', | ||||||||||
| 136 | lazy => 1, | ||||||||||
| 137 | default => sub { | ||||||||||
| 138 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 139 | 0 | my $file_name = File::Spec->catdir( $self->logdir, 'main.log' ); | |||||||||
| 140 | 0 | $self->_make_the_dirs( $self->logdir ); | |||||||||
| 141 | 0 | my $log_conf = q( | |||||||||
| 142 | log4perl.category = INFO, FILELOG, Screen | ||||||||||
| 143 | log4perl.appender.Screen = \ | ||||||||||
| 144 | Log::Log4perl::Appender::ScreenColoredLevels | ||||||||||
| 145 | log4perl.appender.Screen.layout = \ | ||||||||||
| 146 | Log::Log4perl::Layout::PatternLayout | ||||||||||
| 147 | log4perl.appender.Screen.layout.ConversionPattern = \ | ||||||||||
| 148 | [%d] %m %n | ||||||||||
| 149 | log4perl.appender.FILELOG = Log::Log4perl::Appender::File | ||||||||||
| 150 | log4perl.appender.FILELOG.mode = append | ||||||||||
| 151 | log4perl.appender.FILELOG.layout = Log::Log4perl::Layout::PatternLayout | ||||||||||
| 152 | log4perl.appender.FILELOG.layout.ConversionPattern = %d %p %m %n | ||||||||||
| 153 | ); | ||||||||||
| 154 | 0 | $log_conf .= "log4perl.appender.FILELOG.filename = $file_name"; | |||||||||
| 155 | |||||||||||
| 156 | 0 | Log::Log4perl->init( \$log_conf ); | |||||||||
| 157 | 0 | return get_logger(); | |||||||||
| 158 | } | ||||||||||
| 159 | 1 | ); | |||||||||
| 160 | |||||||||||
| 161 | ##Submit Log | ||||||||||
| 162 | has 'log' => ( | ||||||||||
| 163 | is => 'rw', | ||||||||||
| 164 | 0 | ▶ | default => sub { my $self = shift; return $self->init_log }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 165 | 1 | lazy => 1 | |||||||||
| 166 | ); | ||||||||||
| 167 | |||||||||||
| 168 | 1 | has 'logfile' => ( | |||||||||
| 169 | traits => ['String'], | ||||||||||
| 170 | is => 'rw', | ||||||||||
| 171 | default => \&set_logfile, | ||||||||||
| 172 | handles => { | ||||||||||
| 173 | append_logfile => 'append', | ||||||||||
| 174 | prepend_logfile => 'prepend', | ||||||||||
| 175 | clear_logfile => 'clear', | ||||||||||
| 176 | } | ||||||||||
| 177 | ); | ||||||||||
| 178 | |||||||||||
| 179 | =head2 Subroutines | ||||||||||
| 180 | |||||||||||
| 181 | =head3 set_logdir | ||||||||||
| 182 | |||||||||||
| 183 | Set the log directory | ||||||||||
| 184 | |||||||||||
| 185 | =cut | ||||||||||
| 186 | |||||||||||
| 187 | sub set_logdir { | ||||||||||
| 188 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
set_logdir: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 189 | |||||||||||
| 190 | 0 | my $logdir = File::Spec->catdir($self->basedir, 'logs', $self->logname ); | |||||||||
| 191 | 0 | $logdir =~ s/\.log$//; | |||||||||
| 192 | 0 | make_path($logdir); | |||||||||
| 193 | |||||||||||
| 194 | 0 | return $logdir; | |||||||||
| 195 | } | ||||||||||
| 196 | |||||||||||
| 197 | =head3 set_logfile | ||||||||||
| 198 | |||||||||||
| 199 | Set logfile | ||||||||||
| 200 | |||||||||||
| 201 | =cut | ||||||||||
| 202 | |||||||||||
| 203 | sub set_logfile { | ||||||||||
| 204 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
set_logfile: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 205 | |||||||||||
| 206 | 0 | my $tt = DateTime->now( time_zone => 'local' )->ymd(); | |||||||||
| 207 | 0 | return "$tt"; | |||||||||
| 208 | } | ||||||||||
| 209 | |||||||||||
| 210 | =head3 init_log | ||||||||||
| 211 | |||||||||||
| 212 | Initialize Log4perl log | ||||||||||
| 213 | |||||||||||
| 214 | =cut | ||||||||||
| 215 | |||||||||||
| 216 | sub init_log { | ||||||||||
| 217 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
init_log: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 218 | |||||||||||
| 219 | 0 | Log::Log4perl->easy_init( | |||||||||
| 220 | { | ||||||||||
| 221 | level => $TRACE, | ||||||||||
| 222 | utf8 => 1, | ||||||||||
| 223 | mode => 'append', | ||||||||||
| 224 | file => ">>" . File::Spec->catdir( $self->logdir, $self->logfile ), | ||||||||||
| 225 | layout => '%d: %p %m%n ' | ||||||||||
| 226 | } | ||||||||||
| 227 | ); | ||||||||||
| 228 | |||||||||||
| 229 | 0 | my $log = get_logger(); | |||||||||
| 230 | 0 | return $log; | |||||||||
| 231 | } | ||||||||||
| 232 | |||||||||||
| 233 | sub log_main_messages { | ||||||||||
| 234 | 0 | ▶ | my ( $self, $level, $message ) = @_; | ||||||||
|
Subroutine
log_main_messages: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 235 | |||||||||||
| 236 | 0 | ▶ | return unless $message; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 237 | 0 | ▶ | $level = 'info' unless $level; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 238 | 0 | $self->log->$level($message); | |||||||||
| 239 | } | ||||||||||
| 240 | |||||||||||
| 241 | 1 | 1; | |||||||||