File Coverage

blib/lib/Dancer2/Logger/Capture.pm
Criterion Covered Total %
statement 7 7 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 10 100.0


line stmt bran cond sub pod time code
1             # ABSTRACT: Capture dancer logs
2             $Dancer2::Logger::Capture::VERSION = '0.400000';
3             use Moo;
4 10     10   158024 use Dancer2::Logger::Capture::Trap;
  10         6195  
  10         63  
5 10     10   8134  
  10         30  
  10         1252  
6             with 'Dancer2::Core::Role::Logger';
7              
8             has trapper => (
9             is => 'ro',
10             lazy => 1,
11             builder => '_build_trapper',
12             );
13              
14              
15 9     9   197 my ( $self, $level, $message ) = @_;
16              
17             $self->trapper->store(
18             $level, $message, $self->format_message( $level => $message )
19             );
20              
21             return;
22             }
23              
24             1;
25              
26              
27             =pod
28              
29             =encoding UTF-8
30              
31             =head1 NAME
32              
33             Dancer2::Logger::Capture - Capture dancer logs
34              
35             =head1 VERSION
36              
37             version 0.400000
38              
39             =head1 SYNOPSIS
40              
41             The basics:
42              
43             set logger => "capture";
44              
45             my $trap = dancer_app->logger_engine->trapper;
46             my $logs = $trap->read;
47              
48             A worked-out real-world example:
49              
50             use Test::More tests => 2;
51             use Dancer2;
52              
53             set logger => 'capture';
54              
55             warning "Danger! Warning!";
56             debug "I like pie.";
57              
58             my $trap = dancer_app->logger_engine->trapper;
59              
60             is_deeply $trap->read, [
61             { level => "warning", message => "Danger! Warning!" },
62             { level => "debug", message => "I like pie.", }
63             ];
64              
65             # each call to read cleans the trap
66             is_deeply $trap->read, [];
67              
68             =head1 DESCRIPTION
69              
70             This is a logger class for L<Dancer2> which captures all logs to an object.
71              
72             Its primary purpose is for testing. Here is an example of a test:
73              
74             use strict;
75             use warnings;
76             use Test::More;
77             use Plack::Test;
78             use HTTP::Request::Common;
79             use Ref::Util qw<is_coderef>;
80              
81             {
82             package App;
83             use Dancer2;
84              
85             set log => 'debug';
86             set logger => 'capture';
87              
88             get '/' => sub {
89             log(debug => 'this is my debug message');
90             log(core => 'this should not be logged');
91             log(info => 'this is my info message');
92             };
93             }
94              
95             my $app = Dancer2->psgi_app;
96             ok( is_coderef($app), 'Got app' );
97              
98             test_psgi $app, sub {
99             my $cb = shift;
100              
101             my $res = $cb->( GET '/' );
102              
103             my $trap = App->dancer_app->logger_engine->trapper;
104              
105             is_deeply $trap->read, [
106             { level => 'debug', message => 'this is my debug message' },
107             { level => 'info', message => 'this is my info message' },
108             ];
109              
110             is_deeply $trap->read, [];
111             };
112              
113             done_testing;
114              
115             =head1 METHODS
116              
117             =head2 trapper
118              
119             Returns the L<Dancer2::Logger::Capture::Trap> object used to capture
120             and read logs.
121              
122             =head1 SEE ALSO
123              
124             L<Dancer2::Core::Role::Logger>, L<Dancer2::Logger::Capture::Trap>
125              
126             =head1 AUTHOR
127              
128             Dancer Core Developers
129              
130             =head1 COPYRIGHT AND LICENSE
131              
132             This software is copyright (c) 2022 by Alexis Sukrieh.
133              
134             This is free software; you can redistribute it and/or modify it under
135             the same terms as the Perl 5 programming language system itself.
136              
137             =cut