File Coverage

blib/lib/Dancer2/Logger/Capture/Trap.pm
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 2 2 100.0
total 19 19 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Logger::Capture::Trap;
2             # ABSTRACT: a place to store captured Dancer2 logs
3             $Dancer2::Logger::Capture::Trap::VERSION = '1.0.0';
4 10     10   71 use Moo;
  10         28  
  10         56  
5 10     10   4120 use Dancer2::Core::Types;
  10         67  
  10         156  
6              
7             has storage => (
8             is => 'rw',
9             isa => ArrayRef,
10             default => sub { [] },
11             );
12              
13             sub store {
14 15     15 1 60 my ( $self, $level, $message, $fmt_string ) = @_;
15 15         32 push @{ $self->storage }, {
  15         357  
16             level => $level,
17             message => $message,
18             formatted => $fmt_string,
19             };
20             }
21              
22             sub read {
23 13     13 1 13541 my $self = shift;
24              
25 13         272 my $logs = $self->storage;
26 13         309 $self->storage( [] );
27 13         451 return $logs;
28             }
29              
30             1;
31              
32             __END__
33              
34             =pod
35              
36             =encoding UTF-8
37              
38             =head1 NAME
39              
40             Dancer2::Logger::Capture::Trap - a place to store captured Dancer2 logs
41              
42             =head1 VERSION
43              
44             version 1.0.0
45              
46             =head1 SYNOPSIS
47              
48             my $trap = Dancer2::Logger::Capture::Trap->new;
49             $trap->store( $level, $message );
50             my $logs = $trap->read;
51              
52             =head1 DESCRIPTION
53              
54             This is a place to store and retrieve capture Dancer2 logs used by
55             L<Dancer2::Logger::Capture>.
56              
57             =head2 Methods
58              
59             =head3 new
60              
61             =head3 store
62              
63             $trap->store($level, $message);
64              
65             Stores a log $message and its $level.
66              
67             =head3 read
68              
69             my $logs = $trap->read;
70              
71             Returns the logs stored as an array ref and clears the storage.
72              
73             For example...
74              
75             [{ level => "warning", message => "Danger! Warning! Dancer2!" },
76             { level => "error", message => "You fail forever" }
77             ];
78              
79             =head1 SEE ALSO
80              
81             L<Dancer2::Logger::Capture>
82              
83             =head1 AUTHOR
84              
85             Dancer Core Developers
86              
87             =head1 COPYRIGHT AND LICENSE
88              
89             This software is copyright (c) 2023 by Alexis Sukrieh.
90              
91             This is free software; you can redistribute it and/or modify it under
92             the same terms as the Perl 5 programming language system itself.
93              
94             =cut