| 1 | 1 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | package Log::Radis; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Radis is a graylog logging radio through a redis database | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Moo 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Redis 1.980; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use JSON 2.90 qw(encode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Time::HiRes 1.9726; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Sys::Hostname ; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | our $GELF_SPEC_VERSION = '1.1'; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | our $HOSTNAME = hostname(); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | our $VERSION = '0.003'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | has server => ( | |||||||||||||||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | default => 'localhost:6379', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | has reconnect => ( | |||||||||||||||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | default => 5, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | has every => ( | |||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | default => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | has queue => ( | |||||||||||||||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | default => 'graylog-radis:queue', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | has redis => ( | ||||||||||||||||||||||||||||||||||||||||||
| 47 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | isa => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=19.1
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | (blessed($_[0]) and ($_[0]->isa('Redis') or $_[0]->can('lpush'))) or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
builtin::blessed($_[0]) and $_[0]->isa("Redis") || $_[0]->can("lpush")
$_[0]->isa("Redis") or $_[0]->can("lpush")
MC/DC0%0/3
builtin::blessed($_[0]) and $_[0]->isa("Redis") || $_[0]->can("lpush")
builtin::blessed($_[0])
$_[0]->isa("Redis")
$_[0]->can("lpush")
Truth table25%1/4
A and B or C
A= builtin::blessed($_[0])B= $_[0]->isa("Redis")C= $_[0]->can("lpush")
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | croak "Must be an instance of Redis". | ||||||||||||||||||||||||||||||||||||||||||
| 51 | " or a blessed reference implementing the 'lpush' method"; | ||||||||||||||||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | builder => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_redis: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | return Redis->new( | |||||||||||||||||||||||||||||||||||||||||
| 56 | server => $self->server, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | reconnect => $self->reconnect, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | every => $self->every, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | my %levels = ( | |||||||||||||||||||||||||||||||||||||||||
| 65 | fatal => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 66 | emerg => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | emergency => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | alert => 2, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | crit => 2, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | critical => 3, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | error => 4, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | err => 4, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | warn => 5, | ||||||||||||||||||||||||||||||||||||||||||
| 78 | warning => 5, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | note => 6, | ||||||||||||||||||||||||||||||||||||||||||
| 81 | notice => 6, | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | info => 7, | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | debug => 8, | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | trace => 9, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | core => 9, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 26 | ▶ | sub log { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log: called CC=14 SCAR=26.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 26 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 26 | my ($level, $message, %gelf) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 26 | ▶ | croak "log message without level" unless defined $level; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 25 | ▶ | croak "log message without message" unless defined $message; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # replace level with numeric code, if needed | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 24 | ▶ | $level = $levels{lc($level)} unless $level =~ m{^\d$}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | # addiotional field are only allowed with a prefixed underscore | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # and strip off all unallowed chars | ||||||||||||||||||||||||||||||||||||||||||
| 104 | %gelf = map { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | m{^_[\w\.\-]+$}i | ||||||||||||||||||||||||||||||||||||||||||
| 106 | ? | ||||||||||||||||||||||||||||||||||||||||||
| 107 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 108 | lc($_) | ||||||||||||||||||||||||||||||||||||||||||
| 109 | , | ||||||||||||||||||||||||||||||||||||||||||
| 110 | $gelf{$_} | ||||||||||||||||||||||||||||||||||||||||||
| 111 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 112 | : | ||||||||||||||||||||||||||||||||||||||||||
| 113 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 114 | '_'.s{[^\w\.\-]+}{}gr | ||||||||||||||||||||||||||||||||||||||||||
| 115 | , | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 6 | ▶ | $gelf{$_} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 24 | } grep { defined $gelf{$_} } keys %gelf; | |||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | # graylog omit the id field automatically | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 24 | ▶ | if (exists $gelf{_id}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | carp "log message with id is not allowed"; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | delete $gelf{_id}; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | # preserve params, which are allowed by client | ||||||||||||||||||||||||||||||||||||||||||
| 127 | # including some mispelled ones | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 24 | ▶ | $gelf{host} = delete $gelf{_hostname} if defined $gelf{_hostname}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 24 | ▶ | $gelf{host} = delete $gelf{_host} if defined $gelf{_host}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 24 | ▶ | $gelf{timestamp} = delete $gelf{_time} if defined $gelf{_time}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 24 | ▶ | $gelf{timestamp} = delete $gelf{_timestamp} if defined $gelf{_timestamp}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 24 | ▶ | $gelf{full_message} = delete $gelf{_message} if defined $gelf{_message}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 24 | ▶ | $gelf{full_message} = delete $gelf{_full_message} if defined $gelf{_full_message}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | # hostname defaults to system hostname... | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 24 | ▶ | $gelf{host} //= $HOSTNAME; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$gelf{"host"} //= $Log::Radis::HOSTNAME
MC/DC0%0/2
$gelf{"host"} //= $Log::Radis::HOSTNAME
$gelf{"host"}
$Log::Radis::HOSTNAME
Truth table66%2/3
A //= B
A= $gelf{"host"}B= $Log::Radis::HOSTNAME
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | # ...and timestamp with milliseconds by default | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 24 | ▶ | $gelf{timestamp} //= Time::HiRes::time(); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$gelf{"timestamp"} //= &Time::HiRes::time()
MC/DC0%0/2
$gelf{"timestamp"} //= &Time::HiRes::time()
$gelf{"timestamp"}
&Time::HiRes::time()
Truth table66%2/3
A //= B
A= $gelf{"timestamp"}B= &Time::HiRes::time()
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | # graylog seems to have problems with float values in json | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # so force string, which works fine | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 24 | $gelf{timestamp} = ''.$gelf{timestamp}; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 24 | $gelf{short_message} = $message; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 24 | $gelf{version} = $GELF_SPEC_VERSION; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 24 | $gelf{level} = $level; | |||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 24 | $self->push(\%gelf); | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 25 | ▶ | sub push { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
push: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 25 | my ($self, $gelf) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 25 | ▶ | if (ref $gelf eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 24 | $gelf = encode_json($gelf); | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 25 | $self->redis->lpush($self->queue, $gelf); | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||||||||||||||||||||||||