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