| 1 | package Log::Message::Structured::Stringify::Sprintf; | ||
| 2 | 0 | ▶ | use MooseX::Role::Parameterized; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 3 | use MooseX::Types::Moose qw/ ArrayRef /; | ||
| 4 | use MooseX::Types::Common::String qw/ NonEmptySimpleStr /; | ||
| 5 | use namespace::autoclean; | ||
| 6 | |||
| 7 | parameter format_string => ( | ||
| 8 | isa => NonEmptySimpleStr, | ||
| 9 | required => 1, | ||
| 10 | ); | ||
| 11 | |||
| 12 | parameter attributes => ( | ||
| 13 | isa => ArrayRef[NonEmptySimpleStr], | ||
| 14 | required => 1, | ||
| 15 | auto_deref => 1, | ||
| 16 | ); | ||
| 17 | |||
| 18 | role { | ||
| 19 | my $p = shift; | ||
| 20 | |||
| 21 | my $format_string = $p->format_string; | ||
| 22 | my @attributes = $p->attributes; | ||
| 23 | |||
| 24 | requires $_ for grep { $_ ne 'previous_string' } @attributes; | ||
| 25 | |||
| 26 | has previous_string => ( | ||
| 27 | isa => 'Str', | ||
| 28 | is => 'rw', | ||
| 29 | default => '', | ||
| 30 | ); | ||
| 31 | |||
| 32 | around 'as_string' => sub { | ||
| 33 | my $orig = shift; | ||
| 34 | my $self = shift; | ||
| 35 | $self->previous_string($self->$orig(@_)); | ||
| 36 | sprintf($format_string, map { defined() ? $_ : '<undef>' } @{$self->as_hash}{@attributes} ); | ||
| 37 | }; | ||
| 38 | |||
| 39 | }; | ||
| 40 | |||
| 41 | 1; | ||
| 42 | |||
| 43 | =pod | ||
| 44 | |||
| 45 | =head1 NAME | ||
| 46 | |||
| 47 | Log::Message::Structured::Stringify::Sprintf - Traditional style log lines | ||
| 48 | |||
| 49 | =head1 SYNOPSIS | ||
| 50 | |||
| 51 | package MyLogEvent; | ||
| 52 | use Moose; | ||
| 53 | use namespace::autoclean; | ||
| 54 | |||
| 55 | with 'Log::Message::Structured'; | ||
| 56 | |||
| 57 | has [qw/ foo bar /] => ( is => 'ro', required => 1 ); | ||
| 58 | |||
| 59 | # Note: you MUST compose these together and after defining your attributes! | ||
| 60 | with 'Log::Message::Structured::Stringify::Sprintf' => { | ||
| 61 | format_string => q{The value of foo is "%s" and the value of bar is "%s"}, | ||
| 62 | attributes => [qw/ foo bar /], | ||
| 63 | }, 'Log::Message::Structured'; | ||
| 64 | |||
| 65 | ... elsewhere ... | ||
| 66 | |||
| 67 | use aliased 'My::Log::Event'; | ||
| 68 | |||
| 69 | $logger->log(Event->new( foo => "ONE MILLION", bar => "ONE BILLION" )); | ||
| 70 | # Logs an object which will stringify to: | ||
| 71 | The value of foo is "ONE MILLION" and the value of bar is "ONE BILLION". | ||
| 72 | |||
| 73 | =head1 DESCRIPTION | ||
| 74 | |||
| 75 | Augments the C<as_string> method provided by L<Log::Message::Structured> as a | ||
| 76 | parameterised Moose role. | ||
| 77 | |||
| 78 | =head1 PARAMETERS | ||
| 79 | |||
| 80 | =head1 attributes | ||
| 81 | |||
| 82 | Array of attributes whos values will be interpolated into the format string. | ||
| 83 | |||
| 84 | =head2 format_string | ||
| 85 | |||
| 86 | This format string is fed to sprintf with the values from the attributes to | ||
| 87 | produce the output. | ||
| 88 | |||
| 89 | =head1 AUTHOR AND COPYRIGHT | ||
| 90 | |||
| 91 | Tomas Doran (t0m) C<< <bobtfish@bobtfish.net> >>. | ||
| 92 | Damien Krotkine (dams) C<< <dams@cpan.org> >>. | ||
| 93 | |||
| 94 | =head1 LICENSE | ||
| 95 | |||
| 96 | Licensed under the same terms as perl itself. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||