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   19645 use strict;
  1         3  
  1         49  
2 1     1   6 use warnings;
  1         2  
  1         320  
3             package Log::Dispatch::Array;
4             {
5             $Log::Dispatch::Array::VERSION = '1.003';
6             }
7 1     1   1287 use parent qw(Log::Dispatch::Output);
  1         726  
  1         6  
8             # ABSTRACT: log events to an array (reference)
9              
10              
11             sub new {
12 1     1 1 1670 my ($class, %arg) = @_;
13 1   50     10 $arg{array} ||= [];
14              
15 1         3 my $self = { array => $arg{array} };
16              
17 1         3 bless $self => $class;
18              
19             # this is our duty as a well-behaved Log::Dispatch plugin
20 1         11 $self->_basic_init(%arg);
21              
22 1         132 return $self;
23             }
24              
25              
26 3     3 1 35 sub array { $_[0]->{array} }
27              
28              
29             sub log_message {
30 2     2 1 1657 my ($self, %p) = @_;
31 2         3 push @{ $self->array }, { %p };
  2         5  
32             }
33              
34             1;
35              
36             __END__
37              
38             =pod
39              
40             =encoding UTF-8
41              
42             =head1 NAME
43              
44             Log::Dispatch::Array - log events to an array (reference)
45              
46             =head1 VERSION
47              
48             version 1.003
49              
50             =head1 SYNOPSIS
51              
52             use Log::Dispatch;
53             use Log::Dispatch::Array;
54              
55             my $log = Log::Dispatch->new;
56              
57             my $target = [];
58              
59             $log->add(Log::Dispatch::Array->new(
60             name => 'text_table',
61             min_level => 'debug',
62             array => $target,
63             ));
64              
65             $log->warn($_) for @events;
66              
67             # now $target refers to an array of events
68              
69             =head1 DESCRIPTION
70              
71             This provides a Log::Dispatch log output system that appends logged events to
72             an array reference. This is probably only useful for testing the logging of
73             your code.
74              
75             =head1 METHODS
76              
77             =head2 new
78              
79             my $table_log = Log::Dispatch::Array->new(\%arg);
80              
81             This method constructs a new Log::Dispatch::Array output object. Valid
82             arguments are:
83              
84             array - a reference to an array to append to; defaults to an attr on
85             $table_log
86              
87             =head2 array
88              
89             This method returns a reference to the array to which logging is being
90             performed.
91              
92             =head2 log_message
93              
94             This is the method which performs the actual logging, as detailed by
95             Log::Dispatch::Output.
96              
97             =head1 AUTHOR
98              
99             Ricardo SIGNES <rjbs@cpan.org>
100              
101             =head1 COPYRIGHT AND LICENSE
102              
103             This software is copyright (c) 2008 by Ricardo SIGNES.
104              
105             This is free software; you can redistribute it and/or modify it under
106             the same terms as the Perl 5 programming language system itself.
107              
108             =cut