line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Mojo::Role::Log; |
2
|
1
|
|
|
1
|
|
701
|
use Mojo::Base -role, -signatures; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
8
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.2.1'; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
around 'new' => sub { |
6
|
|
|
|
|
|
|
my $orig = shift; |
7
|
|
|
|
|
|
|
my $self = $orig->(@_); |
8
|
|
|
|
|
|
|
my $log = $self->app->log; |
9
|
|
|
|
|
|
|
if ( $log->level eq 'fatal' ){ |
10
|
|
|
|
|
|
|
$log->unsubscribe( |
11
|
|
|
|
|
|
|
message => $log->subscribers('message')->[0] |
12
|
|
|
|
|
|
|
); |
13
|
|
|
|
|
|
|
if ($log->can('trace')){ |
14
|
|
|
|
|
|
|
$log->level('trace'); |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
else { |
17
|
|
|
|
|
|
|
$log->level('debug'); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
$log->on(message => sub { |
21
|
|
|
|
|
|
|
my $log = shift; |
22
|
|
|
|
|
|
|
push @{$self->logCache}, \@_; |
23
|
|
|
|
|
|
|
}); |
24
|
|
|
|
|
|
|
return $self; |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
around '_build_ok' => sub { |
29
|
|
|
|
|
|
|
my $orig = shift; |
30
|
|
|
|
|
|
|
my $self = $_[0]; |
31
|
|
|
|
|
|
|
$self->logCache([]); |
32
|
|
|
|
|
|
|
return $orig->(@_); |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has logCache => sub { |
36
|
|
|
|
|
|
|
[]; |
37
|
|
|
|
|
|
|
}; |
38
|
|
|
|
|
|
|
|
39
|
18
|
|
|
18
|
|
31
|
sub _log_test ($self,$rx,$level,$like,$desc=undef) { |
|
18
|
|
|
|
|
26
|
|
|
18
|
|
|
|
|
22
|
|
|
18
|
|
|
|
|
37
|
|
|
18
|
|
|
|
|
28
|
|
|
18
|
|
|
|
|
31
|
|
|
18
|
|
|
|
|
23
|
|
40
|
18
|
100
|
33
|
|
|
154
|
$desc //= "log ". |
|
|
100
|
|
|
|
|
|
41
|
|
|
|
|
|
|
(defined $level ? "level=$level ":""). |
42
|
|
|
|
|
|
|
($like eq 'like' ? "" : "un")."like $rx"; |
43
|
18
|
50
|
|
|
|
45
|
my $ok = $like ? 0 : 1; |
44
|
18
|
|
|
|
|
25
|
my $logs = ''; |
45
|
18
|
|
|
|
|
31
|
for my $entry (@{$self->logCache}){ |
|
18
|
|
|
|
|
45
|
|
46
|
180
|
|
|
|
|
405
|
my ($l,@msg) = @$entry; |
47
|
180
|
100
|
100
|
|
|
488
|
if (not defined $level or $l eq $level){ |
48
|
66
|
|
|
|
|
167
|
$logs .= join("\n",@msg)."\n"; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
18
|
|
|
|
|
54
|
return $self->test($like,$logs,$rx,Test::Mojo::_desc($desc)); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
3
|
|
|
3
|
1
|
5437
|
sub log_like ($self,$rx,$desc=undef) { |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
4
|
|
55
|
3
|
|
|
|
|
12
|
return $self->_log_test($rx,undef,'like',$desc); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
1
|
|
|
1
|
0
|
315
|
sub log_unlike ($self,$rx,$desc=undef) { |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1
|
|
59
|
1
|
|
|
|
|
5
|
return $self->_log_test($rx,undef,'unlike',$desc); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
2
|
|
|
2
|
1
|
693
|
sub log_trace_like ($self,$rx,$desc=undef) { |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
3
|
|
63
|
2
|
|
|
|
|
8
|
return shift->_log_test($rx,'trace','like',$desc); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
1
|
1
|
314
|
sub log_debug_like ($self,$rx,$desc=undef) { |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
67
|
1
|
|
|
|
|
7
|
return shift->_log_test($rx,'debug','like',$desc); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
1
|
1
|
319
|
sub log_info_like ($self,$rx,$desc=undef) { |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
3
|
|
71
|
1
|
|
|
|
|
4
|
return shift->_log_test($rx,'info','like',$desc); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
1
|
1
|
326
|
sub log_warn_like ($self,$rx,$desc=undef) { |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1
|
|
75
|
1
|
|
|
|
|
9
|
return shift->_log_test($rx,'warn','like',$desc); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
1
|
|
|
1
|
1
|
312
|
sub log_error_like ($self,$rx,$desc=undef) { |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2
|
|
79
|
1
|
|
|
|
|
5
|
return shift->_log_test($rx,'error','like',$desc); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
1
|
|
|
1
|
1
|
320
|
sub log_fatal_like ($self,$rx,$desc=undef) { |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2
|
|
83
|
1
|
|
|
|
|
5
|
return shift->_log_test($rx,'fatal','like',$desc); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
1
|
|
|
1
|
0
|
337
|
sub log_trace_unlike ($self,$rx,$desc=undef) { |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2
|
|
87
|
1
|
|
|
|
|
4
|
return shift->_log_test($rx,'trace','unlike',$desc); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
2
|
|
|
2
|
0
|
637
|
sub log_debug_unlike ($self,$rx,$desc=undef) { |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
4
|
|
91
|
2
|
|
|
|
|
6
|
return shift->_log_test($rx,'debug','unlike',$desc); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
1
|
|
|
1
|
0
|
312
|
sub log_info_unlike ($self,$rx,$desc=undef) { |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
3
|
|
95
|
1
|
|
|
|
|
6
|
return shift->_log_test($rx,'info','unlike',$desc); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
1
|
|
|
1
|
0
|
330
|
sub log_warn_unlike ($self,$rx,$desc=undef) { |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2
|
|
99
|
1
|
|
|
|
|
6
|
return shift->_log_test($rx,'warn','unlike',$desc); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
1
|
|
|
1
|
0
|
316
|
sub log_error_unlike ($self,$rx,$desc=undef) { |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
|
1
|
|
|
|
|
5
|
|
103
|
1
|
|
|
|
|
5
|
return shift->_log_test($rx,'error','unlike',$desc); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
1
|
|
|
1
|
0
|
313
|
sub log_fatal_unlike ($self,$rx,$desc=undef) { |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
4
|
|
107
|
1
|
|
|
|
|
5
|
return shift->_log_test($rx,'fatal','unlike',$desc); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=encoding utf8 |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 NAME |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Test::Mojo::Role::Log - test mojo log messages |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 SYNOPSIS |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
use Test::Mojo; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
my $t = Test::Mojo->with_roles('+Log')->new('MyApp'); |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
$t->get_ok('/gugus') |
126
|
|
|
|
|
|
|
->log_like(qr{GET "/gugus"}) |
127
|
|
|
|
|
|
|
->log_debug_like(qr{GET "/gugus"}) |
128
|
|
|
|
|
|
|
->log_info_unlike(qr{GET "/gugus"}) |
129
|
|
|
|
|
|
|
->log_debug_like(qr{200 OK.+s.+/s}) |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
done_testing(); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 DESCRIPTION |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
The L role enhances L with additional methods to check log output. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 logCache |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Points to an array with all the log messages issued since the last request. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 METHODS |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
The role L adds the following new methods to L ones. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 log_like($rx,$desc) |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
$t->get_ok('/hello') |
150
|
|
|
|
|
|
|
->log_like(undef,qr{/hello not found},"Request got logged") |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Check if the given log message has been issued. All the log messages issued since the start of the current request will get checked. |
153
|
|
|
|
|
|
|
If $logLevel is set to undef the logLevel does not get checked. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 log_trace_like($rx,$desc) (if mojo supports it) |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Find a trace level log message matching the given $rx. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head2 log_debug_like($rx,$desc) |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Find a debug level log message matching the given $rx. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 log_info_like($rx,$desc) |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Find a info level log message matching the given $rx. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 log_warn_like($rx,$desc) |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Find a warn level log message matching the given $rx. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 log_error_like($rx,$desc) |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Find a error level log message matching the given $rx. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 log_fatal_like($rx,$desc) |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Find a fatal level log message matching the given $rx. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head2 *_unlike |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
For each of the methods above there is ac coresponding |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head1 AUTHOR |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Tobias Oetiker Etobi@oetiker.chE |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 COPYRIGHT |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Copyright 2020, OETIKER+PARTNER AG |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 LICENSE |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Perl Artistic License |