| 1 | ################################################## | ||||||||||||||||||||||||||||
| 2 | package Log::Log4perl::Layout::GELF; | ||||||||||||||||||||||||||||
| 3 | ################################################## | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use IO::Compress::Gzip qw( gzip $GzipError ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Log::Log4perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use base qw(Log::Log4perl::Layout::PatternLayout); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # We need to define our own cspecs | ||||||||||||||||||||||||||||
| 16 | 4 | $Log::Log4perl::ALLOW_CODE_IN_CONFIG_FILE = 1; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Log::Log4perl::Layout::GELF - Log4perl for graylog2 | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 VERSION | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Version 0.03 | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Log4perl implementation of GELF. When used with | ||||||||||||||||||||||||||||
| 33 | Log::Log4perl::Appender::Socket you can log directly | ||||||||||||||||||||||||||||
| 34 | to a graylog2 server. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 What is graylog? | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Graylog is log management server that can be used to run analytics, | ||||||||||||||||||||||||||||
| 41 | alerting, monitoring and perform powerful searches over your whole | ||||||||||||||||||||||||||||
| 42 | log base. Need to debug a failing request? Just run a quick filter | ||||||||||||||||||||||||||||
| 43 | search to find it and see what errors it produced. Want to see all | ||||||||||||||||||||||||||||
| 44 | messages a certain API consumer is consuming in real time? Create | ||||||||||||||||||||||||||||
| 45 | streams for every consumer and have them always only one click away. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 Configuration Sample | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Code snippet. Replace the ip with your graylog server. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | use Log::Log4perl | ||||||||||||||||||||||||||||
| 54 | my $logger_conf = { | ||||||||||||||||||||||||||||
| 55 | 'log4perl.logger.graylog' => "DEBUG, SERVER", | ||||||||||||||||||||||||||||
| 56 | 'log4perl.appender.SERVER' => "Log::Log4perl::Appender::Socket", | ||||||||||||||||||||||||||||
| 57 | 'log4perl.appender.SERVER.PeerAddr' => '10.211.1.94', | ||||||||||||||||||||||||||||
| 58 | 'log4perl.appender.SERVER.PeerPort' => "12201", | ||||||||||||||||||||||||||||
| 59 | 'log4perl.appender.SERVER.Proto' => "udp", | ||||||||||||||||||||||||||||
| 60 | 'log4perl.appender.SERVER.layout' => "GELF" | ||||||||||||||||||||||||||||
| 61 | }; | ||||||||||||||||||||||||||||
| 62 | Log::Log4perl->init( $logger_conf ); | ||||||||||||||||||||||||||||
| 63 | my $LOGGER = Log::Log4perl->get_logger('graylog'); | ||||||||||||||||||||||||||||
| 64 | $LOGGER->debug("Debug log"); | ||||||||||||||||||||||||||||
| 65 | ... | ||||||||||||||||||||||||||||
| 66 | =cut | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head2 new | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Can take most of options that Log::Log4perl::Layout::PatternLayout can. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Additional Options: | ||||||||||||||||||||||||||||
| 75 | PlainText - outputs plaintext and not gzipped files. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =cut | ||||||||||||||||||||||||||||
| 78 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 5 | my $class = shift; | |||||||||||||||||||||||||||
| 80 | 5 | ▶ | $class = ref ($class) || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 5 | ▶ | my $options = ref $_[0] eq "HASH" ? shift : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Creating object to make changes easier | ||||||||||||||||||||||||||||
| 85 | 5 | my $gelf_format = { | |||||||||||||||||||||||||||
| 86 | "version" => "1.0", | ||||||||||||||||||||||||||||
| 87 | "host" => "%H", | ||||||||||||||||||||||||||||
| 88 | "short_message" => "%m{chomp}", | ||||||||||||||||||||||||||||
| 89 | "timestamp" => "%Z", # custom cspec | ||||||||||||||||||||||||||||
| 90 | "level"=> "%Y", # custom cspec | ||||||||||||||||||||||||||||
| 91 | "facility"=> "%M", | ||||||||||||||||||||||||||||
| 92 | "file"=> "%F", | ||||||||||||||||||||||||||||
| 93 | "line"=> "%L", | ||||||||||||||||||||||||||||
| 94 | "_pid" => "%P", | ||||||||||||||||||||||||||||
| 95 | }; | ||||||||||||||||||||||||||||
| 96 | # make a JSON string | ||||||||||||||||||||||||||||
| 97 | 5 | my $conversion_pattern = encode_json($gelf_format); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 5 | $options->{ConversionPattern} = { value => $conversion_pattern } ; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Since we are building on top of PatternLayout, we can define our own | ||||||||||||||||||||||||||||
| 102 | # own patterns using a "cspec". | ||||||||||||||||||||||||||||
| 103 | $options->{cspec} = { | ||||||||||||||||||||||||||||
| 104 | 10 | ▶ | 'Z' => { value => sub {return time } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 5 | 'Y' => { value => \&_level_converter } , | |||||||||||||||||||||||||||
| 106 | }; | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 5 | my $self = $class->SUPER::new($options); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # to help with debugging. you can skip the bzipping. | ||||||||||||||||||||||||||||
| 111 | 5 | $self->{PlainText} = 0; | |||||||||||||||||||||||||||
| 112 | 5 | ▶ | if(defined $options->{PlainText}->{value} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 1 | $self->{PlainText} = $options->{PlainText}->{value}; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 5 | return $self; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Maps over the syslog levels from Log4perl levels. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Syslog Levels for Reference | ||||||||||||||||||||||||||||
| 122 | # 0 Emergency: system is unusable | ||||||||||||||||||||||||||||
| 123 | # 1 Alert: action must be taken immediately | ||||||||||||||||||||||||||||
| 124 | # 2 Critical: critical conditions | ||||||||||||||||||||||||||||
| 125 | # 3 Error: error conditions | ||||||||||||||||||||||||||||
| 126 | # 4 Warning: warning conditions | ||||||||||||||||||||||||||||
| 127 | # 5 Notice: normal but significant condition | ||||||||||||||||||||||||||||
| 128 | # 6 Informational: informational messages | ||||||||||||||||||||||||||||
| 129 | # 7 Debug: debug-level messages | ||||||||||||||||||||||||||||
| 130 | 10 | ▶ | sub _level_converter { | ||||||||||||||||||||||||||
|
Subroutine
_level_converter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 131 | 10 | my ($layout, $message, $category, $priority, $caller_level) = @_; | |||||||||||||||||||||||||||
| 132 | # TODO Replace with a case statement | ||||||||||||||||||||||||||||
| 133 | 10 | my $levels = { | |||||||||||||||||||||||||||
| 134 | "DEBUG" => 7, | ||||||||||||||||||||||||||||
| 135 | "INFO" => 6, | ||||||||||||||||||||||||||||
| 136 | "NOTICE"=> 5, | ||||||||||||||||||||||||||||
| 137 | "WARN" => 4, | ||||||||||||||||||||||||||||
| 138 | "ERROR" => 3, | ||||||||||||||||||||||||||||
| 139 | "FATAL" => 2 | ||||||||||||||||||||||||||||
| 140 | }; | ||||||||||||||||||||||||||||
| 141 | 10 | return $levels->{$priority}; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head2 render | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Wraps the Log::Log4perl::Layout::PatternLayout return value so we can | ||||||||||||||||||||||||||||
| 147 | gzip the JSON string. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 10 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 10 | my($self, $message, $category, $priority, $caller_level) = @_; | |||||||||||||||||||||||||||
| 153 | 10 | my $encoded_message = $self->SUPER::render($message, $category, $priority, $caller_level); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # makes debugging easier | ||||||||||||||||||||||||||||
| 156 | 10 | ▶ | if( defined $self->{PlainText} && $self->{PlainText} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"PlainText"} and $self->{"PlainText"}
MC/DC50%1/2
defined $self->{"PlainText"} and $self->{"PlainText"}
defined $self->{"PlainText"}
$self->{"PlainText"}
Truth table66%2/3
A and B
A= defined $self->{"PlainText"}B= $self->{"PlainText"}
| |||||||||||||||||||||||||||||
| 157 | 5 | return $encoded_message; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # Graylog2 servers require gzipped messesages. | ||||||||||||||||||||||||||||
| 161 | 5 | my $gzipped_message; | |||||||||||||||||||||||||||
| 162 | 5 | ▶ | gzip \$encoded_message => \$gzipped_message or die "gzip failed: $GzipError\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 5 | return $gzipped_message; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 4 | 1; | |||||||||||||||||||||||||||