Browse files

* Changed host/port parameters to an arrayref containing 'host:port' …

…strings

* Added new hook 'encode_message' to encode a log message before it's being sent to a Gearman worker
  • Loading branch information...
1 parent e0ed5af commit 22c74524746bd731736806f8b84febc69a0f5fa5 @plu committed Jul 15, 2009
Showing with 91 additions and 82 deletions.
  1. +1 −0 .gitignore
  2. +8 −2 Changes
  3. +20 −28 README
  4. +1 −1 examples/app.pl
  5. +8 −2 examples/app2.pl
  6. +40 −44 lib/Log/Handler/Output/Gearman.pm
  7. +13 −5 t/01-validate.t
View
1 .gitignore
@@ -11,3 +11,4 @@
/inc
/META.yml
/MANIFEST
+/MANIFEST.bak
View
10 Changes
@@ -1,6 +1,12 @@
Revision history for Log-Handler-Output-Gearman
- Fixed Log::Handler::add() integration
+0.01000_02 Wed Jul 15 2009
+ - Fixed Log::Handler::add() integration (this implies removing per
+ message basis options)
+ - Changed host/port parameters to an arrayref containing 'host:port'
+ strings
+ - Added new hook 'encode_message' to encode a log message before
+ it's being sent to a Gearman worker
0.01000_01 Tue Jul 14 2009
- Initial developer release.
+ - Initial developer release.
View
48 README
@@ -5,8 +5,8 @@ SYNOPSIS
use Log::Handler::Output::Gearman;
my $logger = Log::Handler::Output::Gearman->new(
- host => '127.0.0.1',
- worker => 'logger',
+ servers => ['127.0.0.1:4731'],
+ worker => 'logger',
);
my $message = 'This is a log message';
@@ -21,20 +21,22 @@ METHODS
new
Takes a number of arguments, following are mandatory:
- * host
+ * servers
- host => '127.0.0.1' # hostname / ip-address the B<gearmand> is running on
+ # hostname:port gearmand is running on
+ servers => [
+ '127.0.0.1:4731',
+ '192.168.0.1:4735',
+ '192.168.0.2' # uses default port (4730)
+ ]
* worker
- worker => 'logger' # name of the worker that should process the log messages
+ # name of the worker that should process the log messages
+ worker => 'logger'
Besides it takes also following optional arguments:
- * port (default: 4730)
-
- port => 4731 # port germand is listening to
-
* method (default: do_background)
method => 'do_high_background'
@@ -48,28 +50,18 @@ METHODS
* "do_high_background"
* "do_low_background"
- log
- Takes two arguments of which the second is optional:
-
- * $message - The log message
+ * encode_message
- * $options - Options to override default behaviour per log message
+ # encode log message before it's being sent as workload to Gearman
+ encode_message => sub {
+ my ($message) = @_;
+ return JSON::XS::encode({ message => $message });
+ }
- By default every log message is added to Gearman using
- "do_background". This default behaviour can be changed on
- instantiation by setting "method => '...'". In case you need to send
- single messages with higher priority you can override this per
- message:
-
- my $message = 'This is a HIGH PRIO log message';
- my $options = { method => 'do_high_background' };
- $logger->log( $message, $options );
-
- It's also possible to send single messages to other workers:
+ log
+ Takes one argument:
- my $message = 'This is a HIGH PRIO log message';
- my $options = { method => 'do_high_background', worker => 'some_other_worker' };
- $logger->log( $message, $options );
+ * $message - The log message
AUTHOR
Johannes Plunien <plu@cpan.org>
View
2 examples/app.pl
@@ -5,7 +5,7 @@
use lib "$FindBin::Bin/../lib";
use Log::Handler::Output::Gearman;
-my $logger = Log::Handler::Output::Gearman->new( host => '127.0.0.1', worker => 'logger' );
+my $logger = Log::Handler::Output::Gearman->new( servers => ['127.0.0.1'], worker => 'logger' );
for ( 1 .. 20 ) {
$logger->log("Hi, I am the log message number ${_}!");
View
10 examples/app2.pl
@@ -5,9 +5,15 @@
use lib "$FindBin::Bin/../lib";
use Log::Handler;
use Log::Handler::Output::Gearman;
+use JSON::XS;
-my $logger = Log::Handler->new();
-my $gearman = Log::Handler::Output::Gearman->new( host => '127.0.0.1', worker => 'logger' );
+my $json = JSON::XS->new();
+my $logger = Log::Handler->new();
+my $gearman = Log::Handler::Output::Gearman->new(
+ servers => ['127.0.0.1'],
+ worker => 'logger',
+ encode_message => sub { return $json->encode( { message => shift } ) }
+);
my %handler_options = (
maxlevel => 'debug',
View
84 lib/Log/Handler/Output/Gearman.pm
@@ -7,7 +7,7 @@ use Gearman::XS::Client;
use Gearman::XS qw(:constants);
use Params::Validate;
-our $VERSION = '0.01000_01';
+our $VERSION = '0.01000_02';
=head1 NAME
@@ -18,8 +18,8 @@ Log::Handler::Output::Gearman - Send log messages to Gearman workers.
use Log::Handler::Output::Gearman;
my $logger = Log::Handler::Output::Gearman->new(
- host => '127.0.0.1',
- worker => 'logger',
+ servers => ['127.0.0.1:4731'],
+ worker => 'logger',
);
my $message = 'This is a log message';
@@ -40,15 +40,21 @@ Takes a number of arguments, following are B<mandatory>:
=item *
-host
+servers
- host => '127.0.0.1' # hostname / ip-address the B<gearmand> is running on
+ # hostname:port gearmand is running on
+ servers => [
+ '127.0.0.1:4731',
+ '192.168.0.1:4735',
+ '192.168.0.2' # uses default port (4730)
+ ]
=item *
worker
- worker => 'logger' # name of the worker that should process the log messages
+ # name of the worker that should process the log messages
+ worker => 'logger'
=back
@@ -58,12 +64,6 @@ Besides it takes also following B<optional> arguments:
=item *
-port (default: 4730)
-
- port => 4731 # port germand is listening to
-
-=item *
-
method (default: do_background)
method => 'do_high_background'
@@ -86,6 +86,16 @@ This can be one of the following L<Gearman::XS::Client> methods:
=back
+=item *
+
+encode_message
+
+ # encode log message before it's being sent as workload to Gearman
+ encode_message => sub {
+ my ($message) = @_;
+ return JSON::XS::encode({ message => $message });
+ }
+
=back
=cut
@@ -96,15 +106,10 @@ sub new {
my %options = Params::Validate::validate(
@_,
{
- host => {
- type => Params::Validate::SCALAR,
+ servers => {
+ type => Params::Validate::ARRAYREF,
optional => 0,
},
- port => {
- type => Params::Validate::SCALAR,
- regex => qr/^\d+$/,
- default => 4730,
- },
worker => {
type => Params::Validate::SCALAR,
optional => 0,
@@ -114,6 +119,10 @@ sub new {
regex => qr/^(do|do_high|do_low|do_background|do_high_background|do_low_background)$/,
default => 'do_background',
},
+ encode_message => {
+ type => Params::Validate::CODEREF,
+ optional => 1,
+ },
}
);
@@ -126,49 +135,36 @@ sub new {
=head2 log
-Takes two arguments of which the second is optional:
+Takes one argument:
=over 4
=item *
C<$message> - The log message
-=item *
-
-C<$options> - Options to override default behaviour per log message
-
-By default every log message is added to Gearman using C<do_background>. This default behaviour can be changed
-on instantiation by setting C<< method => '...' >>. In case you need to send single messages with higher
-priority you can override this per message:
-
- my $message = 'This is a HIGH PRIO log message';
- my $options = { method => 'do_high_background' };
- $logger->log( $message, $options );
-
-It's also possible to send single messages to other workers:
-
- my $message = 'This is a HIGH PRIO log message';
- my $options = { method => 'do_high_background', worker => 'some_other_worker' };
- $logger->log( $message, $options );
-
=back
=cut
sub log {
- my ( $self, $message, $options ) = @_;
+ my ( $self, $message ) = @_;
return unless defined $message;
$message = $message->{message} if ref($message) eq 'HASH' and defined $message->{message};
- $options ||= {};
+ my $method = $self->{method};
+ my $worker = $self->{worker};
+ my $encoder = $self->{encode_message};
- my $method = $options->{method} || $self->{method};
- my $worker = $options->{worker} || $self->{worker};
+ my $workload = $message;
+
+ if ( ref($encoder) eq 'CODE' ) {
+ $workload = $encoder->($message);
+ }
- my ( $ret, $job_handle ) = $self->{client}->$method( $worker, $message );
+ my ( $ret, $job_handle ) = $self->{client}->$method( $worker, $workload );
if ( $ret != GEARMAN_SUCCESS ) {
croak( $self->{client}->error() );
}
@@ -179,7 +175,7 @@ sub log {
sub _setup_gearman {
my ($self) = @_;
my $client = Gearman::XS::Client->new;
- my $ret = $client->add_server( $self->{host}, $self->{port} );
+ my $ret = $client->add_servers( join( ',', @{ $self->{servers} } ) );
if ( $ret != GEARMAN_SUCCESS ) {
croak( $client->error() );
}
View
18 t/01-validate.t
@@ -1,13 +1,13 @@
use strict;
use warnings;
use Log::Handler::Output::Gearman;
-use Test::More tests => 3;
+use Test::More tests => 4;
eval { my $logger = Log::Handler::Output::Gearman->new(); };
like(
$@,
- qr/Mandatory parameters 'worker', 'host' missing in call to Log::Handler::Output::Gearman::new/,
+ qr/Mandatory parameters 'worker', 'servers' missing in call to Log::Handler::Output::Gearman::new/,
'Mandatory parameters missing'
);
@@ -19,10 +19,18 @@ like(
'Invalid Gearman::XS::Client method'
);
-eval { my $logger = Log::Handler::Output::Gearman->new( port => 'invalid' ); };
+eval { my $logger = Log::Handler::Output::Gearman->new( servers => 'invalid' ); };
like(
$@,
- qr/The 'port' parameter \("invalid"\) to Log::Handler::Output::Gearman::new did not pass regex check/,
- 'Invalid port parameter'
+ qr/The 'servers' parameter \("invalid"\) to Log::Handler::Output::Gearman::new was a 'scalar', which is not one of the allowed types: arrayref/,
+ 'Invalid servers parameter'
+);
+
+eval { my $logger = Log::Handler::Output::Gearman->new( servers => ['127.0.0.1:1234'], worker => 'logger', encode_message => 'invalid' ); };
+
+like(
+ $@,
+ qr/The 'encode_message' parameter \("invalid"\) to Log::Handler::Output::Gearman::new was a 'scalar', which is not one of the allowed types: coderef/,
+ 'Invalid encode_message parameter'
);

0 comments on commit 22c7452

Please sign in to comment.