| 1 | package Log::Message::Structured::Component::AttributesFilter; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use MooseX::Role::Parameterized; | ||
| 6 | use namespace::autoclean; | ||
| 7 | use Moose::Util::TypeConstraints; | ||
| 8 | |||
| 9 | use MooseX::Types::Structured qw(Dict Optional); | ||
| 10 | |||
| 11 | use List::MoreUtils qw(notall any); | ||
| 12 | |||
| 13 | parameter filter => ( | ||
| 14 | isa => enum([qw(in out)]), | ||
| 15 | default => sub { 'in' }, | ||
| 16 | ); | ||
| 17 | |||
| 18 | parameter type => ( | ||
| 19 | isa => enum([qw(public private)]), | ||
| 20 | required => 0, | ||
| 21 | ); | ||
| 22 | |||
| 23 | parameter name => ( | ||
| 24 | isa => 'RegexpRef|CodeRef', | ||
| 25 | required => 0, | ||
| 26 | ); | ||
| 27 | |||
| 28 | parameter value => ( | ||
| 29 | isa => 'RegexpRef|CodeRef', | ||
| 30 | required => 0, | ||
| 31 | ); | ||
| 32 | |||
| 33 | parameter custom => ( | ||
| 34 | isa => 'CodeRef', | ||
| 35 | required => 0, | ||
| 36 | ); | ||
| 37 | |||
| 38 | role { | ||
| 39 | my ($p) = @_; | ||
| 40 | my $in = $p->filter eq 'in'; | ||
| 41 | my @functions = $in ? sub { 1 } : sub { 0 } ; | ||
| 42 | |||
| 43 | if (defined( my $r = $p->type)) { | ||
| 44 | my $r = $p->type eq 'public' ? qr/^[^_]/ : qr/^_/; | ||
| 45 | push @functions, | ||
| 46 | sub { $_[0]->name =~ /$r/ } | ||
| 47 | } | ||
| 48 | if (defined(my $cond = $p->name)) { | ||
| 49 | my $f = ref($cond) eq 'CODE' | ||
| 50 | ? $cond : sub { /$cond/ }; | ||
| 51 | push @functions, | ||
| 52 | sub { local $_ = $_[0]->name; $f->() } | ||
| 53 | } | ||
| 54 | if (defined (my $cond = $p->value)) { | ||
| 55 | my $f = ref($cond) eq 'CODE' | ||
| 56 | ? $cond : sub { /$cond/ }; | ||
| 57 | push @functions, | ||
| 58 | sub { local $_ = $_[0]->get_value($_[1]); $f->() } | ||
| 59 | } | ||
| 60 | if (defined (my $custom = $p->custom)) { | ||
| 61 | push @functions, $custom; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $filter = $in | ||
| 65 | ? # filtering in | ||
| 66 | sub { | ||
| 67 | my ($self, $hash) = @_; | ||
| 68 | delete @{$hash}{ | ||
| 69 | map { $_->name } | ||
| 70 | grep { my $attr = $_; notall { $_->($attr, $self) } @functions | ||
| 71 | } $self->meta->get_all_attributes | ||
| 72 | }; | ||
| 73 | $hash; | ||
| 74 | } | ||
| 75 | : # filtering out | ||
| 76 | sub { | ||
| 77 | my ($self, $hash) = @_; | ||
| 78 | delete @{$hash}{ | ||
| 79 | map { $_->name } | ||
| 80 | grep { my $attr = $_; any { $_->($attr, $self) } @functions | ||
| 81 | } $self->meta->get_all_attributes | ||
| 82 | }; | ||
| 83 | $hash; | ||
| 84 | }; | ||
| 85 | |||
| 86 | around 'as_hash' => sub { | ||
| 87 | my $orig = shift; | ||
| 88 | my $self = shift; | ||
| 89 | my $r = $filter->($self, $self->$orig(@_)); | ||
| 90 | $r; | ||
| 91 | } | ||
| 92 | |||
| 93 | }; | ||
| 94 | |||
| 95 | 1; | ||
| 96 | |||
| 97 | =pod | ||
| 98 | |||
| 99 | =head1 NAME | ||
| 100 | |||
| 101 | Log::Message::Structured::Component::AttributesFilter | ||
| 102 | |||
| 103 | =head1 SYNOPSIS | ||
| 104 | |||
| 105 | package MyLogEvent; | ||
| 106 | use Moose; | ||
| 107 | use namespace::autoclean; | ||
| 108 | |||
| 109 | with ('Log::Message::Structured', | ||
| 110 | 'Log::Message::Structured::Stringify::AsJSON', | ||
| 111 | 'Log::Message::Structured::Component::AttributesFilter' => { | ||
| 112 | filter => 'out', | ||
| 113 | name => qr /^foo/, | ||
| 114 | }); | ||
| 115 | |||
| 116 | has [qw/ foo bar /] => ( is => 'ro', required => 1 ); | ||
| 117 | |||
| 118 | ... elsewhere ... | ||
| 119 | |||
| 120 | use aliased 'My::Log::Event'; | ||
| 121 | |||
| 122 | $logger->log(Event->new( foo => "ONE MILLION", bar => "ONE BILLION" )); | ||
| 123 | # Logs: | ||
| 124 | {"__CLASS__":"MyLogEvent","foo":"ONE MILLION"} | ||
| 125 | # note that bar is not included in the structure | ||
| 126 | |||
| 127 | =head1 DESCRIPTION | ||
| 128 | |||
| 129 | Augments the C<as_string> method provided by L<Log::Message::Structured> as a | ||
| 130 | parameterised Moose role. | ||
| 131 | |||
| 132 | =head1 PARAMETERS | ||
| 133 | |||
| 134 | =head1 filter | ||
| 135 | |||
| 136 | Enum : in, out. Specifies if the other criterias are to be used to filter in or out. Defaults to 'in'. | ||
| 137 | |||
| 138 | =head2 type | ||
| 139 | |||
| 140 | Enum : public, private. Filters on the attribute type | ||
| 141 | |||
| 142 | =head2 name | ||
| 143 | |||
| 144 | a RegexpRef or a CodeRef. Used to filter on the attribute name. The CodeRef will recieve the attribute's name in $_ | ||
| 145 | |||
| 146 | =head2 value | ||
| 147 | |||
| 148 | a RegexpRef or a CodeRef. Used to filter on the attribute value. The CodeRef will recieve the attribute's value in $_ | ||
| 149 | |||
| 150 | =head2 custom | ||
| 151 | |||
| 152 | a CodeRef. Used to do custom filtering. Will recieve the L<Class::MOP::Attribute> as first argument, and C<$self> as second argument. | ||
| 153 | |||
| 154 | =head1 AUTHOR AND COPYRIGHT | ||
| 155 | |||
| 156 | Damien Krotkine (dams) C<< <dams@cpan.org> >>. | ||
| 157 | |||
| 158 | =head1 LICENSE | ||
| 159 | |||
| 160 | Licensed under the same terms as perl itself. | ||
| 161 | |||
| 162 | =cut | ||
| 163 |