From 03893a2309e7972fe5d30daa47b8bb574c9284b6 Mon Sep 17 00:00:00 2001 From: Mario Fetka Date: Fri, 16 Feb 2018 16:07:18 +0100 Subject: [PATCH] Imported Upstream version 0.03 --- Changes | 7 ++ MANIFEST | 15 +++ META.json | 44 +++++++++ META.yml | 25 +++++ Makefile.PL | 23 +++++ README | 55 +++++++++++ README.md | 9 ++ ignore.txt | 12 +++ lib/Log/Log4perl/Layout/GELF.pm | 165 ++++++++++++++++++++++++++++++++ t/00-init.t | 13 +++ t/01-log_to_graylog.t | 42 ++++++++ t/02-log_to_screen.t | 34 +++++++ t/manifest.t | 13 +++ t/pod-coverage.t | 18 ++++ t/pod.t | 12 +++ 15 files changed, 487 insertions(+) create mode 100644 Changes create mode 100644 MANIFEST create mode 100644 META.json create mode 100644 META.yml create mode 100644 Makefile.PL create mode 100644 README create mode 100644 README.md create mode 100644 ignore.txt create mode 100644 lib/Log/Log4perl/Layout/GELF.pm create mode 100644 t/00-init.t create mode 100644 t/01-log_to_graylog.t create mode 100644 t/02-log_to_screen.t create mode 100644 t/manifest.t create mode 100644 t/pod-coverage.t create mode 100644 t/pod.t diff --git a/Changes b/Changes new file mode 100644 index 0000000..1b0055d --- /dev/null +++ b/Changes @@ -0,0 +1,7 @@ +Revision history for Log-Log4perl-Layout-GELF + +0.02 2011-07-19 12:00 + Fixed dependencies and test files +0.01 2011-07-19 08:00 + First version, released on an unsuspecting world. + diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..b823d55 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,15 @@ +Changes +ignore.txt +lib/Log/Log4perl/Layout/GELF.pm +Makefile.PL +MANIFEST This list of files +README +README.md +t/00-init.t +t/01-log_to_graylog.t +t/02-log_to_screen.t +t/manifest.t +t/pod-coverage.t +t/pod.t +META.yml Module YAML meta-data (added by MakeMaker) +META.json Module JSON meta-data (added by MakeMaker) diff --git a/META.json b/META.json new file mode 100644 index 0000000..fc16218 --- /dev/null +++ b/META.json @@ -0,0 +1,44 @@ +{ + "abstract" : "Log4perl for graylog2", + "author" : [ + "Jason Pope " + ], + "dynamic_config" : 1, + "generated_by" : "ExtUtils::MakeMaker version 6.59, CPAN::Meta::Converter version 2.112150", + "license" : [ + "perl_5" + ], + "meta-spec" : { + "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", + "version" : "2" + }, + "name" : "Log-Log4perl-Layout-GELF", + "no_index" : { + "directory" : [ + "t", + "inc" + ] + }, + "prereqs" : { + "build" : { + "requires" : { + "ExtUtils::MakeMaker" : 0 + } + }, + "configure" : { + "requires" : { + "ExtUtils::MakeMaker" : 0 + } + }, + "runtime" : { + "requires" : { + "IO::Compress::Gzip" : "2.00", + "JSON::XS" : "2.30", + "Log::Log4perl" : "1.33", + "Test::More" : 0 + } + } + }, + "release_status" : "stable", + "version" : "0.03" +} diff --git a/META.yml b/META.yml new file mode 100644 index 0000000..4f12fce --- /dev/null +++ b/META.yml @@ -0,0 +1,25 @@ +--- +abstract: 'Log4perl for graylog2' +author: + - 'Jason Pope ' +build_requires: + ExtUtils::MakeMaker: 0 +configure_requires: + ExtUtils::MakeMaker: 0 +dynamic_config: 1 +generated_by: 'ExtUtils::MakeMaker version 6.59, CPAN::Meta::Converter version 2.112150' +license: perl +meta-spec: + url: http://module-build.sourceforge.net/META-spec-v1.4.html + version: 1.4 +name: Log-Log4perl-Layout-GELF +no_index: + directory: + - t + - inc +requires: + IO::Compress::Gzip: 2.00 + JSON::XS: 2.30 + Log::Log4perl: 1.33 + Test::More: 0 +version: 0.03 diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..d8cdbe0 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,23 @@ +use 5.006; +use strict; +use warnings; +use ExtUtils::MakeMaker; + +WriteMakefile( + NAME => 'Log::Log4perl::Layout::GELF', + AUTHOR => q{Jason Pope }, + VERSION_FROM => 'lib/Log/Log4perl/Layout/GELF.pm', + ABSTRACT_FROM => 'lib/Log/Log4perl/Layout/GELF.pm', + ($ExtUtils::MakeMaker::VERSION >= 6.3002 + ? ('LICENSE'=> 'perl') + : ()), + PL_FILES => {}, + PREREQ_PM => { + 'Test::More' => '0', + 'JSON::XS' => '2.30', + 'IO::Compress::Gzip' => '2.00', + 'Log::Log4perl' => '1.33' + }, + dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, + clean => { FILES => 'Log-Log4perl-Layout-GELF-*' }, +); diff --git a/README b/README new file mode 100644 index 0000000..8c27279 --- /dev/null +++ b/README @@ -0,0 +1,55 @@ +Log-Log4perl-Layout-GELF + +The README is used to introduce the module and provide instructions on +how to install the module, any machine dependencies it may have (for +example C compilers and installed libraries) and any other information +that should be provided before the module is installed. + +A README file is required for CPAN modules since CPAN extracts the README +file from a module distribution so that people browsing the archive +can use it to get an idea of the module's uses. It is usually a good idea +to provide version information here so that people can decide whether +fixes for the module are worth downloading. + + +INSTALLATION + +To install this module, run the following commands: + + perl Makefile.PL + make + make test + make install + +SUPPORT AND DOCUMENTATION + +After installing, you can find documentation for this module with the +perldoc command. + + perldoc Log::Log4perl::Layout::GELF + +You can also look for information at: + + RT, CPAN's request tracker (report bugs here) + http://rt.cpan.org/NoAuth/Bugs.html?Dist=Log-Log4perl-Layout-GELF + + AnnoCPAN, Annotated CPAN documentation + http://annocpan.org/dist/Log-Log4perl-Layout-GELF + + CPAN Ratings + http://cpanratings.perl.org/d/Log-Log4perl-Layout-GELF + + Search CPAN + http://search.cpan.org/dist/Log-Log4perl-Layout-GELF/ + + +LICENSE AND COPYRIGHT + +Copyright (C) 2011 Jason Pope + +This program is free software; you can redistribute it and/or modify it +under the terms of either: the GNU General Public License as published +by the Free Software Foundation; or the Artistic License. + +See http://dev.perl.org/licenses/ for more information. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..9a2c4f6 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +Log::Log4perl::Layout::GELF (Log4perl in graylog format) +======================================================================== + +## Summary + +[Log4perl]("http://search.cpan.org/~mschilli/Log-Log4perl-1.33/lib/Log/Log4perl.pm") +is great. [Graylog]("http://graylog2.org/") is awesome. This layout when used with +Log::Log4perl::Appender::Socket allows you to log directly to graylog. + diff --git a/ignore.txt b/ignore.txt new file mode 100644 index 0000000..6125045 --- /dev/null +++ b/ignore.txt @@ -0,0 +1,12 @@ +blib* +Makefile +Makefile.old +Build +Build.bat +_build* +pm_to_blib* +*.tar.gz +.lwpcookies +cover_db +pod2htm*.tmp +Log-Log4perl-Layout-GELF-* diff --git a/lib/Log/Log4perl/Layout/GELF.pm b/lib/Log/Log4perl/Layout/GELF.pm new file mode 100644 index 0000000..6ed7f60 --- /dev/null +++ b/lib/Log/Log4perl/Layout/GELF.pm @@ -0,0 +1,165 @@ +################################################## +package Log::Log4perl::Layout::GELF; +################################################## + +use 5.006; +use strict; +use warnings; + +use JSON::XS; +use IO::Compress::Gzip qw( gzip $GzipError ); +use Log::Log4perl; + +use base qw(Log::Log4perl::Layout::PatternLayout); + +# We need to define our own cspecs +$Log::Log4perl::ALLOW_CODE_IN_CONFIG_FILE = 1; + +=head1 NAME + +Log::Log4perl::Layout::GELF - Log4perl for graylog2 + +=head1 VERSION + +Version 0.03 + +=cut + +our $VERSION = '0.03'; + +=head1 SYNOPSIS + +Log4perl implementation of GELF. When used with +Log::Log4perl::Appender::Socket you can log directly +to a graylog2 server. + +=cut + +=head1 What is graylog? + +Graylog is log management server that can be used to run analytics, +alerting, monitoring and perform powerful searches over your whole +log base. Need to debug a failing request? Just run a quick filter +search to find it and see what errors it produced. Want to see all +messages a certain API consumer is consuming in real time? Create +streams for every consumer and have them always only one click away. + +=cut + +=head1 Configuration Sample + +Code snippet. Replace the ip with your graylog server. + + use Log::Log4perl + my $logger_conf = { + 'log4perl.logger.graylog' => "DEBUG, SERVER", + 'log4perl.appender.SERVER' => "Log::Log4perl::Appender::Socket", + 'log4perl.appender.SERVER.PeerAddr' => '10.211.1.94', + 'log4perl.appender.SERVER.PeerPort' => "12201", + 'log4perl.appender.SERVER.Proto' => "udp", + 'log4perl.appender.SERVER.layout' => "GELF" + }; + Log::Log4perl->init( $logger_conf ); + my $LOGGER = Log::Log4perl->get_logger('graylog'); + $LOGGER->debug("Debug log"); + ... +=cut + +=head1 SUBROUTINES/METHODS + +=head2 new + + Can take most of options that Log::Log4perl::Layout::PatternLayout can. + + Additional Options: + PlainText - outputs plaintext and not gzipped files. + +=cut +sub new { + my $class = shift; + $class = ref ($class) || $class; + + my $options = ref $_[0] eq "HASH" ? shift : {}; + + # Creating object to make changes easier + my $gelf_format = { + "version" => "1.0", + "host" => "%H", + "short_message" => "%m{chomp}", + "timestamp" => "%Z", # custom cspec + "level"=> "%Y", # custom cspec + "facility"=> "%M", + "file"=> "%F", + "line"=> "%L", + "_pid" => "%P", + }; + # make a JSON string + my $conversion_pattern = encode_json($gelf_format); + + $options->{ConversionPattern} = { value => $conversion_pattern } ; + + # Since we are building on top of PatternLayout, we can define our own + # own patterns using a "cspec". + $options->{cspec} = { + 'Z' => { value => sub {return time } }, + 'Y' => { value => \&_level_converter } , + }; + + my $self = $class->SUPER::new($options); + + # to help with debugging. you can skip the bzipping. + $self->{PlainText} = 0; + if(defined $options->{PlainText}->{value} ){ + $self->{PlainText} = $options->{PlainText}->{value}; + } + return $self; +} + + +# Maps over the syslog levels from Log4perl levels. + +# Syslog Levels for Reference +# 0 Emergency: system is unusable +# 1 Alert: action must be taken immediately +# 2 Critical: critical conditions +# 3 Error: error conditions +# 4 Warning: warning conditions +# 5 Notice: normal but significant condition +# 6 Informational: informational messages +# 7 Debug: debug-level messages +sub _level_converter { + my ($layout, $message, $category, $priority, $caller_level) = @_; + # TODO Replace with a case statement + my $levels = { + "DEBUG" => 7, + "INFO" => 6, + "NOTICE"=> 5, + "WARN" => 4, + "ERROR" => 3, + "FATAL" => 2 + }; + return $levels->{$priority}; +} + +=head2 render + + Wraps the Log::Log4perl::Layout::PatternLayout return value so we can + gzip the JSON string. + +=cut + +sub render { + my($self, $message, $category, $priority, $caller_level) = @_; + my $encoded_message = $self->SUPER::render($message, $category, $priority, $caller_level); + + # makes debugging easier + if( defined $self->{PlainText} && $self->{PlainText} ){ + return $encoded_message; + } + + # Graylog2 servers require gzipped messesages. + my $gzipped_message; + gzip \$encoded_message => \$gzipped_message or die "gzip failed: $GzipError\n"; + return $gzipped_message; +} +1; diff --git a/t/00-init.t b/t/00-init.t new file mode 100644 index 0000000..f737d98 --- /dev/null +++ b/t/00-init.t @@ -0,0 +1,13 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More tests => 3; + +BEGIN { use_ok( 'Log::Log4perl::Layout::GELF' ); } + +my $layout = Log::Log4perl::Layout::GELF->new(); +isa_ok($layout, "Log::Log4perl::Layout::GELF"); + +can_ok($layout, ("render")); diff --git a/t/01-log_to_graylog.t b/t/01-log_to_graylog.t new file mode 100644 index 0000000..7f14171 --- /dev/null +++ b/t/01-log_to_graylog.t @@ -0,0 +1,42 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More tests => 4; + +BEGIN { use_ok( 'Log::Log4perl::Layout::GELF' ); } +BEGIN { use_ok( 'Log::Log4perl' ); } + +my $layout = Log::Log4perl::Layout::GELF->new(); +isa_ok($layout, "Log::Log4perl::Layout::GELF"); + +can_ok($layout, ("render")); + +_init_logger(); + + +sub _init_logger +{ + + my %logger_conf = ( + 'log4perl.logger.test.screen' => "DEBUG, DEFAULT", + 'log4perl.appender.DEFAULT' => "Log::Log4perl::Appender::Screen", + 'log4perl.appender.DEFAULT.stderr' => "0", + 'log4perl.appender.DEFAULT.layout' => "GELF", + 'log4perl.logger.test.server' => "DEBUG, SERVER", + 'log4perl.appender.SERVER' => "Log::Log4perl::Appender::Socket", + 'log4perl.appender.SERVER.PeerAddr' => '10.211.1.94', + 'log4perl.appender.SERVER.PeerPort' => "12201", + 'log4perl.appender.SERVER.Proto' => "udp", + 'log4perl.appender.SERVER.layout' => "GELF" + ); + + Log::Log4perl->init( \%logger_conf ); + my $LOGGER = Log::Log4perl->get_logger('test.server'); + $LOGGER->debug("debug test"); + $LOGGER->error("error test"); + $LOGGER->info("info test"); + $LOGGER->warn("warn test"); + $LOGGER->fatal("fatal test"); +} diff --git a/t/02-log_to_screen.t b/t/02-log_to_screen.t new file mode 100644 index 0000000..803c5cb --- /dev/null +++ b/t/02-log_to_screen.t @@ -0,0 +1,34 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More tests => 2; + +BEGIN { use_ok( 'Log::Log4perl::Layout::GELF' ); } +BEGIN { use_ok( 'Log::Log4perl' ); } + + +_init_logger(); + + + +sub _init_logger +{ + + my %logger_conf = ( + 'log4perl.logger.test.screen' => "DEBUG, DEFAULT", + 'log4perl.appender.DEFAULT' => "Log::Log4perl::Appender::Screen", + 'log4perl.appender.DEFAULT.stderr' => "0", + 'log4perl.appender.DEFAULT.layout' => "GELF", + 'log4perl.appender.DEFAULT.layout.PlainText' => "1", + ); + + Log::Log4perl->init( \%logger_conf ); + my $LOGGER = Log::Log4perl->get_logger('test.screen'); + $LOGGER->debug("debug test"); + $LOGGER->error("error test"); + $LOGGER->info("info test"); + $LOGGER->warn("warn test"); + $LOGGER->fatal("fatal test"); +} diff --git a/t/manifest.t b/t/manifest.t new file mode 100644 index 0000000..45eb83f --- /dev/null +++ b/t/manifest.t @@ -0,0 +1,13 @@ +#!perl -T + +use strict; +use warnings; +use Test::More; + +unless ( $ENV{RELEASE_TESTING} ) { + plan( skip_all => "Author tests not required for installation" ); +} + +eval "use Test::CheckManifest 0.9"; +plan skip_all => "Test::CheckManifest 0.9 required" if $@; +ok_manifest(); diff --git a/t/pod-coverage.t b/t/pod-coverage.t new file mode 100644 index 0000000..fc40a57 --- /dev/null +++ b/t/pod-coverage.t @@ -0,0 +1,18 @@ +use strict; +use warnings; +use Test::More; + +# Ensure a recent version of Test::Pod::Coverage +my $min_tpc = 1.08; +eval "use Test::Pod::Coverage $min_tpc"; +plan skip_all => "Test::Pod::Coverage $min_tpc required for testing POD coverage" + if $@; + +# Test::Pod::Coverage doesn't require a minimum Pod::Coverage version, +# but older versions don't recognize some common documentation styles +my $min_pc = 0.18; +eval "use Pod::Coverage $min_pc"; +plan skip_all => "Pod::Coverage $min_pc required for testing POD coverage" + if $@; + +all_pod_coverage_ok(); diff --git a/t/pod.t b/t/pod.t new file mode 100644 index 0000000..ee8b18a --- /dev/null +++ b/t/pod.t @@ -0,0 +1,12 @@ +#!perl -T + +use strict; +use warnings; +use Test::More; + +# Ensure a recent version of Test::Pod +my $min_tp = 1.22; +eval "use Test::Pod $min_tp"; +plan skip_all => "Test::Pod $min_tp required for testing POD" if $@; + +all_pod_files_ok();