Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'topic/stringify_on_steroid'

  • Loading branch information...
commit 8d5d09553974602a3620e15b8cd667abd7c3c31b 2 parents a9698be + e093b98
@dams dams authored
View
2  Changes
@@ -1,3 +1,5 @@
+ - stringify is not required anymore, the class can compose zero or more stringifiers now.
+
0.006
- Pull date and hostname attributes out into their own components,
making them optional (dams).
View
2  README
@@ -6,8 +6,6 @@ SYNOPSIS
use Moose;
use namespace::autoclean;
- # Note: you MUST implement a 'stringify' method, or compose a role
- # that gives you a stringify method.
with qw/
Log::Message::Structured
Log::Message::Structured::Stringify::AsJSON
View
4 lib/Log/Message/Structured.pm
@@ -23,7 +23,7 @@ has epochtime => (
sub BUILD {}
-requires 'stringify';
+sub stringify { '' }
1;
@@ -41,8 +41,6 @@ Log::Message::Structured - Simple structured log messages
use Moose;
use namespace::autoclean;
- # Note: you MUST implement a 'stringify' method, or compose a role
- # that gives you a stringify method.
with qw/
Log::Message::Structured
Log::Message::Structured::Stringify::AsJSON
View
1  lib/Log/Message/Structured/Component/Date.pm
@@ -36,7 +36,6 @@ Log::Message::Structured::Component::Date
use Moose;
use namespace::autoclean;
- # Note: you MUST compose these together as they depend on methods in each other
with qw/
Log::Message::Structured
/;
View
8 lib/Log/Message/Structured/Event/ScriptRun.pm
@@ -18,12 +18,14 @@ has time => (
# FIXME - User running script?
+with qw( Log::Message::Structured );
+with qw( Log::Message::Structured::Component::Date
+ Log::Message::Structured::Component::Hostname
+ );
with 'Log::Message::Structured::Stringify::Sprintf' => {
format_string => q{Script %s run on %s for %ss},
attributes => [qw/ script_name hostname time /],
-}, 'Log::Message::Structured';
-with qw(Log::Message::Structured::Component::Date);
-with qw(Log::Message::Structured::Component::Hostname);
+};
=head1 NAME
View
9 lib/Log/Message/Structured/Stringify/AsJSON.pm
@@ -4,7 +4,13 @@ use namespace::autoclean;
requires 'freeze';
-sub stringify { shift->freeze }
+around 'stringify' => sub {
+ my $orig = shift;
+ my $self = shift;
+ $self->freeze
+};
+
+#sub stringify { shift->freeze }
1;
@@ -22,7 +28,6 @@ Log::Message::Structured::Stringify::AsJSON - JSON log lines
use Moose;
use namespace::autoclean;
- # Note: you MUST compose these together as they depend on methods in each other
with qw/
Log::Message::Structured
Log::Message::Structured::Stringify::AsJSON
View
23 lib/Log/Message/Structured/Stringify/Sprintf.pm
@@ -21,14 +21,27 @@ role {
my $format_string = $p->format_string;
my @attributes = $p->attributes->flatten;
- requires $_ for grep { ! /^(hostname|date|epochtime)$/ } @attributes;
+ requires $_ for grep { $_ ne 'previous_string' } @attributes;
- method stringify => sub {
+ has previous_string => (
+ isa => 'Str',
+ is => 'rw',
+ default => '',
+ );
+
+ around 'stringify' => sub {
+ my $orig = shift;
my $self = shift;
- # FIXME - Find the correct reader name rather than assuming
- # attribute name == accessor name.
- sprintf($format_string, map { my $val = $self->$_; $val = 'undef' unless defined $val; $val } @attributes);
+ $self->previous_string($self->$orig(@_));
+ sprintf($format_string, map { defined() ? $_ : '<undef>' } map { $self->$_ } @attributes);
};
+
+ # method stringify => sub {
+ # my $self = shift;
+ # # FIXME - Find the correct reader name rather than assuming
+ # # attribute name == accessor name.
+ # sprintf($format_string, map { defined() ? $_ : '<undef>' } map { $self->$_ } @attributes);
+ # };
};
1;
View
4 t/basic.t
@@ -6,8 +6,8 @@ use Test::More;
package TestEvent;
use Moose;
with qw(Log::Message::Structured);
- with qw(Log::Message::Structured::Component::Date);
- with qw(Log::Message::Structured::Component::Hostname);
+ with qw(Log::Message::Structured::Component::Date
+ Log::Message::Structured::Component::Hostname);
sub stringify { 'MOO' }
View
6 t/stringify_asjson.t
@@ -5,8 +5,10 @@ use Test::More;
{
package TestEventSprintf;
use Moose;
- with qw/Log::Message::Structured::Stringify::AsJSON Log::Message::Structured/;
- with qw(Log::Message::Structured::Component::Date);
+ with qw( Log::Message::Structured
+ Log::Message::Structured::Stringify::AsJSON
+ );
+ with qw( Log::Message::Structured::Component::Date);
has [qw/foo bar baz/] => ( is => 'ro', required => 1);
}
View
10 t/stringify_sprintf.t
@@ -8,15 +8,19 @@ use Test::More;
has [qw/foo bar baz/] => ( is => 'ro', required => 1);
+ with 'Log::Message::Structured';
with 'Log::Message::Structured::Stringify::Sprintf' => {
format_string => "%s lala %s baba %s caca",
attributes => [qw/ foo bar baz /],
- }, 'Log::Message::Structured';
- with qw(Log::Message::Structured::Component::Date);
+ }, ;
+ with 'Log::Message::Structured::Stringify::Sprintf' => {
+ format_string => "<PREFIX>%s",
+ attributes => [qw/ previous_string /],
+ };
}
-my $exp = '2 lala 3 baba 4 caca';
+my $exp = '<PREFIX>2 lala 3 baba 4 caca';
my $e = TestEventSprintf->new(foo => 2, bar => 3, baz => 4);
ok $e;
Please sign in to comment.
Something went wrong with that request. Please try again.