-
Notifications
You must be signed in to change notification settings - Fork 5
/
log-defer-viz
executable file
·544 lines (371 loc) · 14 KB
/
log-defer-viz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#!/usr/bin/env perl
use common::sense;
use JSON::XS;
use Getopt::Long;
use Term::ANSIColor;
use Term::Size;
use Try::Tiny;
use Log::Defer::Viz;
## List of terminal colours in order of preference (not all terminals support bright_*)
my $colour_spec = {
error => [qw/ bright_red red /],
warn => [qw/ bright_yellow yellow /],
info => [qw/ green /],
debug => [qw/ bright_blue blue /],
};
my @opt_spec = (
## INPUT FORMAT
'input-format=s',
## DATE
'date!',
'since-now',
'tz=s',
## FILTERING
'logs!',
'verbosity=i',
'error!',
'warn!',
'info!',
'debug!',
'quiet|q',
'verbose|v',
'colour!', 'color!',
## TIMERS
'timers!',
'timer-columns=i',
## DATA SECTION
'data!',
'data-format=s',
'data-only',
'count=s@',
## MISC
'help|h|?',
'grep|filter|g=s',
'map|m=s',
);
my $opt = {
'input-format' => 'json',
'date' => 1,
'since-now' => 0,
'logs' => 1,
'verbosity' => 30,
'quiet' => 0,
'verbose' => 0,
'timers' => 1,
'timer-columns' => 100,
'data-format' => 'json-pretty',
'data-only' => 0,
};
GetOptions($opt, @opt_spec) || die "GetOptions failed";
if ($opt->{help}) {
require Pod::Perldoc;
@ARGV = ('-F', $0);
Pod::Perldoc->run();
}
die "Only json input-format is currently supported" unless $opt->{'input-format'} eq 'json';
$opt->{colour} = $opt->{color} if exists $opt->{color};
if (!exists $opt->{colour}) {
$opt->{colour} = 1 if -t STDOUT;
}
if ($opt->{verbose} && $opt->{quiet}) {
die "--verbose and --quiet are incompatible";
} elsif ($opt->{verbose}) {
$opt->{verbosity} = 40;
$opt->{data} = 1 unless (defined $opt->{data} && !$opt->{data});
} elsif ($opt->{quiet}) {
$opt->{verbosity} = 20;
$opt->{timers} = 0 unless !$opt->{timers};
}
if ($opt->{'data-only'}) {
$opt->{date} = $opt->{timers} = $opt->{logs} = 0;
$opt->{data} = 1;
}
my $count = {};
foreach my $key (@{ $opt->{count} }) {
$count->{$key} = {};
}
my $columns = $opt->{'timer-columns'};
my ($term_cols, $term_rows) = Term::Size::chars(*STDOUT{IO});
$columns = $term_cols if $term_cols;
## The _ sub is a shortcut for use in --grep and --map expressions, ie --grep '_->{data}'
sub _ () { $_ }
if ($opt->{grep}) {
$opt->{grep} = eval('sub { local $_ = $_[0]; ' . $opt->{grep} . '}');
die "Error compiling --grep expression: $@" if $@;
}
if ($opt->{map}) {
$opt->{map} = eval('sub { local $_ = $_[0]; ' . $opt->{map} . '}');
die "Error compiling --map expression: $@" if $@;
}
unshift(@ARGV, '-') unless @ARGV;
while (my $file = shift) {
my $fh;
## FIXME: Do these with perl modules in case zcat/bzcat aren't available.
## This would also fix stupid single quote limitation below.
if ($file =~ /[.]gz$/) {
die qq{filename "$file" can't contain single quotes } if $file =~ /'/;
open($fh, "zcat '$file' |") || die "couldn't open $file with zcat: $!";
} elsif ($file =~ /[.]bz2$/) {
die qq{filename "$file" can't contain single quotes } if $file =~ /'/;
open($fh, "bzcat '$file' |") || die "couldn't open $file with bzcat: $!";
} elsif ($file eq '-') {
$fh = \*STDIN;
} else {
open($fh, '<', $file) || die "couldn't open $file: $!";
}
while(<$fh>) {
my $entry;
try {
$entry = decode_json($_);
} catch {
if ($opt->{verbose}) {
print STDERR "Error parsing log line: $_";
}
};
handle_entry($entry) if defined $entry;
}
close($fh);
}
if (scalar @{$opt->{count}}) {
print " Count:\n";
print output_data($count);
}
sub handle_entry {
my ($entry) = @_;
if ($opt->{grep}) {
my $result;
eval {
$result = $opt->{grep}->($entry);
};
if ($@) {
print STDERR "Run-time error in --grep expression, skipping record: $@";
return;
}
return unless $result;
}
if ($opt->{map}) {
my $output;
eval {
$output = $opt->{map}->($entry);
};
if ($@) {
print STDERR "Run-time error in --map expression, skipping record: $@";
return;
}
$output = "$output\n" unless $output =~ /\n$/;;
print $output;
return;
}
my $millis = '';
if ($entry->{start} =~ /[.](\d+)$/) {
$millis = ".$1";
}
if ($opt->{date}) {
my $formatted = '';
if ($opt->{'since-now'}) {
require Date::Calc;
my $elapsed = int(time - $entry->{start});
if ($elapsed <= 0) {
$formatted = abs($elapsed) . " seconds in the future.. clock is wrong?";
} else {
my ($Dd,$Dh,$Dm,$Ds) = Date::Calc::Normalize_DHMS(0, 0, 0, int(time - $entry->{start}));
$formatted .= "$Dd days " if $Dd;
$formatted .= "$Dh hours " if $Dh;
$formatted .= "$Dm minutes " if $Dm;
$formatted .= "$Ds seconds " if $Ds;
$formatted .= "ago";
}
} else {
require Date::Format;
$formatted = Date::Format::time2str("%Y-%m-%d %a %H:%M:%S$millis %Z", $entry->{start}, $opt->{tz});
}
my $date_header = "------ $formatted ($entry->{start}) ------";
$date_header = colored($date_header, 'black on_white') if $opt->{colour};
print "$date_header\n";
}
if ($opt->{logs}) {
foreach my $log (@{ $entry->{logs} }) {
next unless should_show_message($log->[1]);
my @log_message = @$log[2..(@$log-1)];
my $log_string = ' [' . sprintf("%5s", num_to_level($log->[1])) . ']';
if (!ref $log_message[0] && $log_message[0] !~ /\n/) {
$log_string .= " $log_message[0]";
shift @log_message;
}
$log_string .= ' ' . encode_json(\@log_message) if @log_message;
if ($opt->{colour}) {
my $colour = num_to_colour($log->[1]);
$log_string = colored($log_string, $colour) if defined $colour;
}
print ' | ' . format_time_offset($log->[0]) . $log_string . "\n";
}
print " |_" . format_time_offset($entry->{end}) . " [END]\n\n";
}
if ($opt->{timers}) {
print Log::Defer::Viz::render_timers(width => $columns-10, timers => $entry->{timers}) if $entry->{timers};
}
if ($entry->{data}) {
if ($opt->{data}) {
print " Data:\n" unless $opt->{'data-only'};
print output_data($entry->{data});
} else {
print " ** This log has associated data. See it with --data\n"
unless $opt->{quiet} || (defined $opt->{data} && !$opt->{data});
}
foreach my $key (@{ $opt->{count} }) {
if (exists $entry->{data}->{$key}) {
$count->{$key}->{$entry->{data}->{$key}} += 1;
}
}
}
print "\n";
}
sub num_to_level {
my $level = shift;
return "ERROR" if $level == 10;
return "WARN" if $level == 20;
return "INFO" if $level == 30;
return "DEBUG" if $level == 40;
return $level;
}
my $terminal_colour_name_cache = {};
sub get_colour_name {
my $name = shift;
return $terminal_colour_name_cache->{$name} if exists $terminal_colour_name_cache->{$name};
foreach my $colour_under_test (@{ $colour_spec->{$name} }) {
eval {
colored('junk', $colour_under_test);
};
return ($terminal_colour_name_cache->{$name} = $colour_under_test)
if !$@;
}
return ($terminal_colour_name_cache->{$name} = undef);
}
sub num_to_colour {
my $level = shift;
return get_colour_name("error") if $level <= 10;
return get_colour_name("warn") if $level <= 20;
return get_colour_name("info") if $level <= 30;
return get_colour_name("debug") if $level <= 40;
return;
}
sub format_time_offset {
my $offset = shift;
return sprintf("%.6f", $offset);
}
sub should_show_message {
my $level = shift;
return $opt->{error} if $level == 10 && defined $opt->{error};
return $opt->{warn} if $level == 20 && defined $opt->{warn};
return $opt->{info} if $level == 30 && defined $opt->{info};
return $opt->{debug} if $level == 40 && defined $opt->{debug};
return 1 if $level <= $opt->{verbosity};
return 0;
}
my $pretty_json_context;
sub output_data {
my $data = shift;
if ($opt->{'data-format'} eq 'json-pretty') {
$pretty_json_context ||= JSON::XS->new->ascii->pretty->allow_nonref;
return $pretty_json_context->encode($data);
} elsif ($opt->{'data-format'} eq 'json') {
return encode_json($data);
} elsif ($opt->{'data-format'} eq 'yaml') {
require YAML;
return YAML::Dump($data);
} elsif ($opt->{'data-format'} eq 'dumper') {
require Data::Dumper;
return Data::Dumper::Dumper($data);
}
die "Unknown data format: $opt->{'data-format'}";
}
__END__
=pod
=head1 NAME
log-defer-viz - command-line utility for rendering log messages created by L<Log::Defer>
=head1 DESCRIPTION
L<Log::Defer> is a module that creates structured logs. Read its documentation which explains how structured logging can help you.
This module installs a command-line script that parses these logs and displays them in a readable manner.
=head1 INPUT METHODS
$ cat file.log | log-defer-viz
$ log-defer-viz < file.log
$ log-defer-viz file.log
$ log-defer-viz file.log file2.log
$ log-defer-viz archived.log.gz more_logs.bz2
=head1 INPUT FORMAT
$ log-defer-viz --input-format=json ## default is newline separated JSON
$ log-defer-viz --input-format=sereal ## Sereal::Decoder (not impl)
$ log-defer-viz --input-format=messagepack ## Data::MessagePack (not impl)
$ log-defer-viz --input-format=storable ## Storable (not impl)
Note: The only input format currently implemented is newline-separated JSON.
=head1 LOG MESSAGES
$ log-defer-viz ## by default shows error, warn, and info logs
$ log-defer-viz -v ## verbose mode (adds debug logs and more)
$ log-defer-viz --debug ## show debug logs
$ log-defer-viz --quiet ## only errors and warnings
$ log-defer-viz --verbosity 25 ## numeric verbosity threshold
$ log-defer-viz --nowarn ## muffle warn logs (so show error and info)
$ log-defer-viz --nologs ## don't show log section
$ log-defer-viz --nocolour ## turn off terminal colours
=head1 TIMERS
$ log-defer-viz --timer-columns 80 ## width of timer chart
$ log-defer-viz --since-now ## show relative to now times
## like "34 minutes ago"
$ log-defer-viz --notimers ## don't show timer chart
$ log-defer-viz --tz UTC ## show times in UTC, not local
=head1 DATA SECTION
Data is extra embedded information in the log file. The available outputs are C<pretty-json>, C<json>, C<yaml>, and C<dumper>.
$ log-defer-viz --data ## show data section. default is pretty-json
$ log-defer-viz --data-format=json ## compact, not pretty
$ log-defer-viz --data-format=dumper ## Data::Dumper
$ log-defer-viz --data-only ## only show data
=head1 COUNT
The count parameter provides a method to count the number of times a specific key/value appears in the data section of the log file.
$ log-defer-viz --data --count ip_address ## Display how many log lines for each ip address
$ log-defer-viz --data --count ip_address --count user_id ## Display how many log lines for each ip address and each user_id
=head1 MISC
$ log-defer-viz --help ## the text you're reading now
$ log-defer-viz --grep '$_->{data}' ## grep for records that have a data section.
## $_ is the entire Log::Defer entry.
$ log-defer-viz --map '$_->{data}->{username}' ## Extract username from data
=head1 GREPING
As shown above, there is a C<--grep> command-line option. This lets you filter log messages using arbitrary perl code. If the expression returns true, the log message is processed and displayed as normal.
Being able to do this easily is an important advantage of structured logs. With unstructured logs it is often difficult to extract all of the information related to a request and nothing else.
For example, here is how to grep for all requests that took longer than 500 milliseconds:
$ log-defer-viz --grep '$_->{end} > .5' server.log
Depending on your underlying storage format, it may be meaningful to grep B<before> passing to C<log-defer-viz>. Currently the only supported storage format is newline-separated JSON which I<is> designed to be pre-grepable. If your search string appears anywhere in the object, the entire log message will be displayed:
$ grep 10.9.1.2 app.log | log-defer-viz
The final and most error-prone way to grep Log::Defer logs is to grep the unstructured output of C<log-defer-viz>:
$ log-defer-viz app.log | grep 10.9.1.2
=head1 MAPPING
Similar to C<--grep>, there is also a C<--map> command-line option. If this option is passed in, the only thing that is output is whatever your C<--map> expression returns.
For example, if you are putting the PID into the data section with C<< $log->data->{pid} = $$ >>, then you can extract the PID like so:
$ log-defer-viz --map '$_->{data}->{pid}' < app.log
9765
9768
9771
Join together fields with a pipe (also shows the special C<_> shortcut which is an abbreviation for C<$_>):
$ log-defer-viz --map 'join "|", _->{data}{pid}, _->{start}' < app.log
9765|1362166673.95104
9768|1362168038.85611
9771|1362169482.39561
Make dates readable (C<localtime> in scalar context is readable):
$ log-defer-viz --map 'join "|", _->{data}{pid}, "".localtime(_->{start})' < app.log
9765|Fri Mar 1 14:37:53 2013
9768|Fri Mar 1 15:00:38 2013
9771|Fri Mar 1 15:24:42 2013
As with C<--grep>, you have access to any perl functions you need. Also, you can combine C<--map> and C<--grep>. For example, here is how to do a "pass-through" grep where the output is another valid JSON-encoded L<Log::Defer> file:
$ log-defer-viz -g '_->{data}{username} eq "jimmy"' \
-m 'encode_json _' \
< app.log \
> jimmys-requests.log
Note that the above also takes advantage of the C<-g> and C<-m> option shortcuts for C<--grep> and C<--map> respectively.
=head1 SEE ALSO
L<Log::Defer>
L<Log::Defer::Viz github repo|https://github.com/hoytech/Log-Defer-Viz>
=head1 AUTHOR
Doug Hoyte, C<< <doug@hcsw.org> >>
=head1 COPYRIGHT & LICENSE
Copyright 2013 Doug Hoyte.
This module is licensed under the same terms as perl itself.
=cut