line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HPC::Runner::Command::Utils::Log; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1342
|
use Log::Log4perl qw(:easy); |
|
1
|
|
|
|
|
30390
|
|
|
1
|
|
|
|
|
4
|
|
4
|
1
|
|
|
1
|
|
709
|
use Data::Dumper; |
|
1
|
|
|
|
|
20
|
|
|
1
|
|
|
|
|
47
|
|
5
|
1
|
|
|
1
|
|
7
|
use IPC::Open3; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
46
|
|
6
|
1
|
|
|
1
|
|
5
|
use IO::Select; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
7
|
1
|
|
|
1
|
|
5
|
use Symbol; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
50
|
|
8
|
1
|
|
|
1
|
|
659
|
use DateTime; |
|
1
|
|
|
|
|
323511
|
|
|
1
|
|
|
|
|
43
|
|
9
|
1
|
|
|
1
|
|
440
|
use DateTime::Format::Duration; |
|
1
|
|
|
|
|
4197
|
|
|
1
|
|
|
|
|
44
|
|
10
|
1
|
|
|
1
|
|
7
|
use Cwd; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
55
|
|
11
|
1
|
|
|
1
|
|
6
|
use File::Path qw(make_path); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
12
|
1
|
|
|
1
|
|
5
|
use File::Spec; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
17
|
|
13
|
1
|
|
|
1
|
|
7
|
use File::Slurp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
54
|
|
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
6
|
use MooseX::App::Role; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
10
|
|
16
|
1
|
|
|
1
|
|
7177
|
use MooseX::Types::Path::Tiny qw/Path Paths AbsPath AbsFile/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
12
|
|
17
|
1
|
|
|
1
|
|
3169
|
use Path::Tiny; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
608
|
|
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
|
|
|
|
|
|
|
my $self = shift; |
40
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
my $self = shift; |
46
|
|
|
|
|
|
|
my $val = shift; |
47
|
|
|
|
|
|
|
$self->_make_the_dirs($val); |
48
|
|
|
|
|
|
|
}, |
49
|
|
|
|
|
|
|
); |
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my $self = shift; |
80
|
|
|
|
|
|
|
make_path( $self->logdir ); |
81
|
|
|
|
|
|
|
my $process_table = |
82
|
|
|
|
|
|
|
File::Spec->catdir( $self->logdir, "001-task_table.md" ); |
83
|
|
|
|
|
|
|
$process_table = path($process_table); |
84
|
|
|
|
|
|
|
$process_table->touchpath; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $header = |
87
|
|
|
|
|
|
|
"|| Version || Scheduler Id || Jobname || Task Tags || ProcessID || ExitCode || Duration ||\n"; |
88
|
|
|
|
|
|
|
write_file( $process_table, { append => 1 }, $header ) |
89
|
|
|
|
|
|
|
or $self->app_log->warn("Couldn't open process file! $!\n"); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
return $process_table; |
92
|
|
|
|
|
|
|
}, |
93
|
|
|
|
|
|
|
trigger => sub { |
94
|
|
|
|
|
|
|
my $self = shift; |
95
|
|
|
|
|
|
|
$self->process_table->touchpath; |
96
|
|
|
|
|
|
|
}, |
97
|
|
|
|
|
|
|
); |
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
default => sub { return DateTime->now( time_zone => 'local' ); }, |
129
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my $self = shift; |
139
|
|
|
|
|
|
|
my $file_name = File::Spec->catdir( $self->logdir, 'main.log' ); |
140
|
|
|
|
|
|
|
$self->_make_the_dirs( $self->logdir ); |
141
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
$log_conf .= "log4perl.appender.FILELOG.filename = $file_name"; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Log::Log4perl->init( \$log_conf ); |
157
|
|
|
|
|
|
|
return get_logger(); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
##Submit Log |
162
|
|
|
|
|
|
|
has 'log' => ( |
163
|
|
|
|
|
|
|
is => 'rw', |
164
|
|
|
|
|
|
|
default => sub { my $self = shift; return $self->init_log }, |
165
|
|
|
|
|
|
|
lazy => 1 |
166
|
|
|
|
|
|
|
); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
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
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
189
|
|
|
|
|
|
|
|
190
|
1
|
|
|
|
|
35
|
my $logdir = File::Spec->catdir($self->basedir, 'logs', $self->logname ); |
191
|
1
|
|
|
|
|
12
|
$logdir =~ s/\.log$//; |
192
|
1
|
|
|
|
|
174
|
make_path($logdir); |
193
|
|
|
|
|
|
|
|
194
|
1
|
|
|
|
|
35
|
return $logdir; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head3 set_logfile |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Set logfile |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=cut |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub set_logfile { |
204
|
2
|
|
|
2
|
1
|
5746
|
my $self = shift; |
205
|
|
|
|
|
|
|
|
206
|
2
|
|
|
|
|
42
|
my $tt = DateTime->now( time_zone => 'local' )->ymd(); |
207
|
2
|
|
|
|
|
6912
|
return "$tt"; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head3 init_log |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Initialize Log4perl log |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=cut |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
sub init_log { |
217
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
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
|
|
|
0
|
0
|
|
my ( $self, $level, $message ) = @_; |
235
|
|
|
|
|
|
|
|
236
|
0
|
0
|
|
|
|
|
return unless $message; |
237
|
0
|
0
|
|
|
|
|
$level = 'info' unless $level; |
238
|
0
|
|
|
|
|
|
$self->log->$level($message); |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
1; |