| 1 | package eris::log::decoder::json; | ||||||||||
| 2 | # ABSTRACT: Decodes any detected JSON in a log line from then opening curly brace | ||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use JSON::MaybeXS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | with qw( | |||||||||
| 9 | eris::role::decoder | ||||||||||
| 10 | ); | ||||||||||
| 11 | |||||||||||
| 12 | 1 | our $VERSION = '0.008'; # VERSION | |||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | 0 | ▶ | sub _build_priority { 99; } | ||||||||
|
Subroutine
_build_priority: not called CC=1 SCAR=6.9
| |||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | sub decode_message { | ||||||||||
| 19 | 0 | ▶ | my ($self,$msg) = @_; | ||||||||
|
Subroutine
decode_message: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 20 | 0 | my $decoded; | |||||||||
| 21 | # JSON Docs will start with a '{', check for it. | ||||||||||
| 22 | 0 | my $start = index($msg, '{'); | |||||||||
| 23 | 0 | ▶ | if( $start >= 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | my $json_str = substr($msg, $start); | |||||||||
| 25 | 0 | eval { | |||||||||
| 26 | 0 | $decoded = decode_json( $json_str ); | |||||||||
| 27 | 0 | 1; | |||||||||
| 28 | }; | ||||||||||
| 29 | } | ||||||||||
| 30 | 0 | return $decoded; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | 1 | 1; | |||||||||
| 35 | |||||||||||
| 36 | __END__ | ||||||||||
| 37 | |||||||||||
| 38 | =pod | ||||||||||
| 39 | |||||||||||
| 40 | =encoding UTF-8 | ||||||||||
| 41 | |||||||||||
| 42 | =head1 NAME | ||||||||||
| 43 | |||||||||||
| 44 | eris::log::decoder::json - Decodes any detected JSON in a log line from then opening curly brace | ||||||||||
| 45 | |||||||||||
| 46 | =head1 VERSION | ||||||||||
| 47 | |||||||||||
| 48 | version 0.008 | ||||||||||
| 49 | |||||||||||
| 50 | =head1 SYNOPSIS | ||||||||||
| 51 | |||||||||||
| 52 | This decoder checks for the presence of an opening curly brace in the raw | ||||||||||
| 53 | message. If detected, it assumes the entire rest of the string is valid JSON | ||||||||||
| 54 | and attempted to decode. | ||||||||||
| 55 | |||||||||||
| 56 | This means the whole message doesn't need to be JSON, so you can syslog JSON | ||||||||||
| 57 | and the L<eris::log::decoder::syslog> will properly handle the syslog headers | ||||||||||
| 58 | and structure. This decoder will then grab that JSON hashref and parse it | ||||||||||
| 59 | correctly. | ||||||||||
| 60 | |||||||||||
| 61 | =head1 ATTRIBUTES | ||||||||||
| 62 | |||||||||||
| 63 | =head2 priority | ||||||||||
| 64 | |||||||||||
| 65 | Defaults to 99, run almost last | ||||||||||
| 66 | |||||||||||
| 67 | =head1 METHODS | ||||||||||
| 68 | |||||||||||
| 69 | =head2 decode_message | ||||||||||
| 70 | |||||||||||
| 71 | Takes a raw string. Find the first occurrence of an opening curly brace '{' and parses | ||||||||||
| 72 | from that point to the end of the message as if it were valid JSON. | ||||||||||
| 73 | |||||||||||
| 74 | =head1 SEE ALSO | ||||||||||
| 75 | |||||||||||
| 76 | L<eris::log::decoders>, L<eris::role::decoder>, L<JSON::MaybeXS> | ||||||||||
| 77 | |||||||||||
| 78 | =head1 AUTHOR | ||||||||||
| 79 | |||||||||||
| 80 | Brad Lhotsky <brad@divisionbyzero.net> | ||||||||||
| 81 | |||||||||||
| 82 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 83 | |||||||||||
| 84 | This software is Copyright (c) 2015 by Brad Lhotsky. | ||||||||||
| 85 | |||||||||||
| 86 | This is free software, licensed under: | ||||||||||
| 87 | |||||||||||
| 88 | The (three-clause) BSD License | ||||||||||
| 89 | |||||||||||
| 90 | =cut | ||||||||||