| 1 | #!/usr/local/bin/perl | ||
| 2 | # PODNAME: eris-context.pl | ||
| 3 | # ABSTRACT: Utility for testing the logging contextualizer | ||
| 4 | ## no critic (RequireEndWithOne) | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use CLI::Helpers qw(:output); | ||
| 9 | use Data::Printer; | ||
| 10 | use Hash::Flatten qw(flatten); | ||
| 11 | use JSON::MaybeXS; | ||
| 12 | use Getopt::Long::Descriptive; | ||
| 13 | use YAML; | ||
| 14 | |||
| 15 | use eris::log::contextualizer; | ||
| 16 | use eris::schemas; | ||
| 17 | |||
| 18 | #------------------------------------------------------------------------# | ||
| 19 | # Argument Parsing | ||
| 20 | my ($opt,$usage) = describe_options( | ||
| 21 | "%c %o ", | ||
| 22 | [ 'sample|s=s', "Sample messages from the specified context" ], | ||
| 23 | ['schema|filtered|f', "Show the schema fitlered output from the schema match instead." ], | ||
| 24 | ['bulk|b', "Show the bulk output from the schema match instead." ], | ||
| 25 | ['json|j', "Show the structure are JSON." ], | ||
| 26 | ['flatten|F', "Flatten the hash keys, defaults to false."], | ||
| 27 | ['complete|C', "Use the complete object instead of just the uniqued context."], | ||
| 28 | [], | ||
| 29 | [ 'config|c=s', "eris config file", { | ||
| 30 | callbacks => { exists => sub { -f shift } } | ||
| 31 | }], | ||
| 32 | [ 'help' => 'Display this message and exit', { shortcircuit => 1 } ], | ||
| 33 | ); | ||
| 34 | if( $opt->help ) { | ||
| 35 | print $usage->text; | ||
| 36 | exit 0; | ||
| 37 | } | ||
| 38 | |||
| 39 | #------------------------------------------------------------------------# | ||
| 40 | # Main | ||
| 41 | my $cfg = $opt->config ? YAML::LoadFile($opt->config) : {}; | ||
| 42 | my $ctxr = eris::log::contextualizer->new( config => $cfg ); | ||
| 43 | my $schm = eris::schemas->new( $cfg->{schemas} ? %{ $cfg->{schemas} } : () ); | ||
| 44 | |||
| 45 | my @sampled = (); | ||
| 46 | foreach my $c ( @{ $ctxr->contexts->plugins } ) { | ||
| 47 | verbose({color=>'magenta'}, sprintf "Loaded context: %s", $c->name); | ||
| 48 | if( $opt->sample and lc $opt->sample eq lc $c->name ) { | ||
| 49 | push @sampled, $c->sample_messages; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | if( @sampled ) { | ||
| 54 | foreach my $msg ( @sampled ) { | ||
| 55 | dump_record($msg); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | # Use the Magic Diamond | ||
| 60 | while(<>) { | ||
| 61 | chomp; | ||
| 62 | verbose({color=>'cyan'}, $_); | ||
| 63 | dump_record($_); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | sub dump_record { | ||
| 68 | my $msg = shift; | ||
| 69 | my $l = $ctxr->parse($msg); | ||
| 70 | my $v = $opt->schema ? $schm->to_document($l) | ||
| 71 | : $l->as_doc( | ||
| 72 | $opt->flatten ? ( flatten => 1 ) : (), | ||
| 73 | $opt->complete ? ( complete => 1 ) : (), | ||
| 74 | ); | ||
| 75 | if( $opt->bulk ) { | ||
| 76 | output({data=>1}, $schm->as_bulk($l)); | ||
| 77 | } | ||
| 78 | elsif( $opt->json ) { | ||
| 79 | output({data=>1}, encode_json($v)); | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | p($v); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | __END__ | ||
| 87 | |||
| 88 | =pod | ||
| 89 | |||
| 90 | =encoding UTF-8 | ||
| 91 | |||
| 92 | =head1 NAME | ||
| 93 | |||
| 94 | eris-context.pl - Utility for testing the logging contextualizer | ||
| 95 | |||
| 96 | =head1 VERSION | ||
| 97 | |||
| 98 | version 0.008 | ||
| 99 | |||
| 100 | =head1 AUTHOR | ||
| 101 | |||
| 102 | Brad Lhotsky <brad@divisionbyzero.net> | ||
| 103 | |||
| 104 | =head1 COPYRIGHT AND LICENSE | ||
| 105 | |||
| 106 | This software is Copyright (c) 2015 by Brad Lhotsky. | ||
| 107 | |||
| 108 | This is free software, licensed under: | ||
| 109 | |||
| 110 | The (three-clause) BSD License | ||
| 111 | |||
| 112 | =cut |