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 = '0.400001';
4 10     10   72 use Moo;
  10         27  
  10         54  
5 10     10   3726 use Dancer2::Core::Types;
  10         51  
  10         116  
6              
7             has storage => (
8             is => 'rw',
9             isa => ArrayRef,
10             default => sub { [] },
11             );
12              
13             sub store {
14 15     15 1 57 my ( $self, $level, $message, $fmt_string ) = @_;
15 15         36 push @{ $self->storage }, {
  15         346  
16             level => $level,
17             message => $message,
18             formatted => $fmt_string,
19             };
20             }
21              
22             sub read {
23 13     13 1 12084 my $self = shift;
24              
25 13         263 my $logs = $self->storage;
26 13         284 $self->storage( [] );
27 13         403 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 0.400001
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