File Coverage

blib/lib/Log/Dispatch/Array.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition 1 2 50.0
subroutine 6 6 100.0
pod 3 3 100.0
total 29 30 96.6


line stmt bran cond sub pod time code
1 1     1   206239 use strict;
  1         2  
  1         25  
2 1     1   5 use warnings;
  1         2  
  1         36  
3             package Log::Dispatch::Array 1.005;
4 1     1   5 use parent qw(Log::Dispatch::Output);
  1         2  
  1         6  
5             # ABSTRACT: log events to an array (reference)
6              
7             #pod =head1 SYNOPSIS
8             #pod
9             #pod use Log::Dispatch;
10             #pod use Log::Dispatch::Array;
11             #pod
12             #pod my $log = Log::Dispatch->new;
13             #pod
14             #pod my $target = [];
15             #pod
16             #pod $log->add(Log::Dispatch::Array->new(
17             #pod name => 'text_table',
18             #pod min_level => 'debug',
19             #pod array => $target,
20             #pod ));
21             #pod
22             #pod $log->warn($_) for @events;
23             #pod
24             #pod # now $target refers to an array of events
25             #pod
26             #pod =head1 DESCRIPTION
27             #pod
28             #pod This provides a Log::Dispatch log output system that appends logged events to
29             #pod an array reference. This is probably only useful for testing the logging of
30             #pod your code.
31             #pod
32             #pod =method new
33             #pod
34             #pod my $table_log = Log::Dispatch::Array->new(\%arg);
35             #pod
36             #pod This method constructs a new Log::Dispatch::Array output object. Valid
37             #pod arguments are:
38             #pod
39             #pod array - a reference to an array to append to; defaults to an attr on
40             #pod $table_log
41             #pod
42             #pod =cut
43              
44             sub new {
45 1     1 1 832 my ($class, %arg) = @_;
46 1   50     8 $arg{array} ||= [];
47              
48 1         2 my $self = { array => $arg{array} };
49              
50 1         3 bless $self => $class;
51              
52             # this is our duty as a well-behaved Log::Dispatch plugin
53 1         8 $self->_basic_init(%arg);
54              
55 1         91 return $self;
56             }
57              
58             #pod =method array
59             #pod
60             #pod This method returns a reference to the array to which logging is being
61             #pod performed.
62             #pod
63             #pod =cut
64              
65 3     3 1 19 sub array { $_[0]->{array} }
66              
67             #pod =method log_message
68             #pod
69             #pod This is the method which performs the actual logging, as detailed by
70             #pod Log::Dispatch::Output.
71             #pod
72             #pod =cut
73              
74             sub log_message {
75 2     2 1 740 my ($self, %p) = @_;
76 2         3 push @{ $self->array }, { %p };
  2         5  
77             }
78              
79             1;
80              
81             __END__
82              
83             =pod
84              
85             =encoding UTF-8
86              
87             =head1 NAME
88              
89             Log::Dispatch::Array - log events to an array (reference)
90              
91             =head1 VERSION
92              
93             version 1.005
94              
95             =head1 SYNOPSIS
96              
97             use Log::Dispatch;
98             use Log::Dispatch::Array;
99              
100             my $log = Log::Dispatch->new;
101              
102             my $target = [];
103              
104             $log->add(Log::Dispatch::Array->new(
105             name => 'text_table',
106             min_level => 'debug',
107             array => $target,
108             ));
109              
110             $log->warn($_) for @events;
111              
112             # now $target refers to an array of events
113              
114             =head1 DESCRIPTION
115              
116             This provides a Log::Dispatch log output system that appends logged events to
117             an array reference. This is probably only useful for testing the logging of
118             your code.
119              
120             =head1 PERL VERSION
121              
122             This library should run on perls released even a long time ago. It should work
123             on any version of perl released in the last five years.
124              
125             Although it may work on older versions of perl, no guarantee is made that the
126             minimum required version will not be increased. The version may be increased
127             for any reason, and there is no promise that patches will be accepted to lower
128             the minimum required perl.
129              
130             =head1 METHODS
131              
132             =head2 new
133              
134             my $table_log = Log::Dispatch::Array->new(\%arg);
135              
136             This method constructs a new Log::Dispatch::Array output object. Valid
137             arguments are:
138              
139             array - a reference to an array to append to; defaults to an attr on
140             $table_log
141              
142             =head2 array
143              
144             This method returns a reference to the array to which logging is being
145             performed.
146              
147             =head2 log_message
148              
149             This is the method which performs the actual logging, as detailed by
150             Log::Dispatch::Output.
151              
152             =head1 AUTHOR
153              
154             Ricardo SIGNES <cpan@semiotic.systems>
155              
156             =head1 CONTRIBUTOR
157              
158             =for stopwords Ricardo Signes
159              
160             Ricardo Signes <rjbs@semiotic.systems>
161              
162             =head1 COPYRIGHT AND LICENSE
163              
164             This software is copyright (c) 2008 by Ricardo SIGNES.
165              
166             This is free software; you can redistribute it and/or modify it under
167             the same terms as the Perl 5 programming language system itself.
168              
169             =cut