script/parse-syslog-line.pl untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 85 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2 # PODNAME: parse-syslog-line.pl
3 # ABSTRACT: Parse a syslog message and display the structured data
4 use v5.16;
5 use warnings;
6  
7 use Data::Printer;
8 use Getopt::Long::Descriptive;
9 use Pod::Usage;
10 use JSON::MaybeXS;
11 use Parse::Syslog::Line;
12 use YAML::XS;
13  
14 my $enc;
15 my %formats = (
16 print => sub { p($_[0]) },
17 json => sub {
18 $enc ||= JSON->new->utf8->canonical;
19 printf "%s\n", $enc->encode($_[0]);
20 },
21 pretty => sub {
22 $enc ||= JSON->new->utf8->canonical->pretty;
23 print $enc->encode($_[0]);
24 },
25 yaml => sub {
26 print YAML::XS::Dump($_[0]);
27 },
28 );
29 my ($opt,$usage) = describe_options("%c %o",
30 ['Output Format'],
31 ['format' => 'hidden', {
32 one_of => [
33 [ 'print|ddp' => 'Format with Data::Printer' ],
34 [ 'json|jq|j' => 'Format as JSON, minified' ],
35 [ pretty => 'Format as JSON, pretty' ],
36 [ 'yaml|yml' => 'Format as YAML' ],
37 ],
38 default => 'print',
39 }],
40 [],
41 ['Field Control'],
42 ['disable-rfc-sdata|S', "Disables the built-in RFC5424-ish structured data extraction"],
43 ['strict-rfc-sdata|D', "Require strict adherence to RFC5424 for structured data extraction"],
44 ['sdata|s', 'Enable all SDATA detection (JSON and K/V pairs)'],
45 ['empty|e', 'Display fields with undef or blank string', ],
46 ['raw|r', 'Display the raw fields in the hash.',],
47 ['all|a', 'Display all fields.', { implies => [qw(raw empty)] }],
48 [],
49 ['help', 'Display this help', { shortcircuit => 1 }],
50 ['manual', 'Display the manual', { shortcircuit => 1 }],
51 );
52 if( $opt->help ) {
53 print $usage->text;
54 exit 0;
55 }
56 pod2usage(-verbose => 2, -exitval => 0 ) if $opt->manual;
57  
58  
59 # Configure
60 psl_enable_sdata() if $opt->sdata;
61 $Parse::Syslog::Line::PruneEmpty = !$opt->empty;
62 $Parse::Syslog::Line::PruneRaw = !$opt->raw;
63 $Parse::Syslog::Line::RFC5424StructuredData = !$opt->disable_rfc_sdata;
64 $Parse::Syslog::Line::RFC5424StructuredDataStrict = $opt->strict_rfc_sdata;
65  
66 while(<>) {
67 my $msg = parse_syslog_line($_);
68 $formats{$opt->format}->( $msg );
69 }
70  
71 __END__