File Coverage

blib/lib/EventStore/Tiny/Logger.pm
Criterion Covered Total %
statement 21 21 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 34 34 100.0


line stmt bran cond sub pod time code
1             package EventStore::Tiny::Logger;
2              
3 6     6   37 use strict;
  6         8  
  6         143  
4 6     6   23 use warnings;
  6         8  
  6         280  
5              
6             use Class::Tiny {
7 3         209 print_target => sub {select}, # Selected output file handle
8 6     6   2674 };
  6         9208  
  6         34  
9              
10             sub log_event {
11 28     28 1 57 my ($self, $event) = @_;
12              
13             # Stringify
14 6     6   4015 use Data::Dump 'dump';
  6         28496  
  6         985  
15 28 100       444 my $data = $event->can('data') ? dump $event->data : 'NO DATA';
16 28         5891 my $output = $event->name . ": $data";
17              
18             # Print to given print handle
19 28         509 return $self->print_target->print("$output\n");
20             }
21              
22             sub log_cb {
23 10     10 1 14794 my ($self, @args) = @_;
24              
25             # Create a new logger if called as a package procedure
26 10 100       57 $self = EventStore::Tiny::Logger->new(@args) unless ref $self;
27              
28             # Create a logging callback function
29 10     25   279 return sub {$self->log_event(shift)};
  25         79  
30             }
31              
32             1;
33              
34             =pod
35              
36             =encoding utf-8
37              
38             =head1 NAME
39              
40             EventStore::Tiny::Logger
41              
42             =head1 REFERENCE
43              
44             EventStore::Tiny::Logger implements the following attributes and methods.
45              
46             =head2 print_target
47              
48             $log->print_target(*{STDERR});
49              
50             Set or get the print target of this logger. By default it uses the L<"select"|perlfunc/"select RBITS,WBITS,EBITS,TIMEOUT">ed file handle (normally STDOUT) but everything with a print method will do.
51              
52             =head2 log_event
53              
54             $log->log_event($event);
55              
56             Logs the type name together with a dump of the concrete data of the given event to its L.
57              
58             =head2 log_cb
59              
60             # As a method
61             $store->logger($log->log_cb);
62              
63             # As a procedure
64             $store->logger(EventStore::Tiny::log_cb);
65              
66             Generates a subref which can be used as a L of an event store. If called as a method, it returns a subref which uses the current Logger (together with the set L). If called as a procedure or class method, it returns a closure to the log method of a fresh anonymous Logger instance.
67              
68             =head1 SEE ALSO
69              
70             L
71              
72             =head1 COPYRIGHT AND LICENSE
73              
74             Copyright (c) 2018 Mirko Westermeier (mail: mirko@westermeier.de)
75              
76             Released under the MIT License (see LICENSE.txt for details).
77              
78             =cut