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