ConsoleLogger/Easy.pm

filter: Statementstmt 98.3% 61 / 62 Branchbran 75.0% 6 / 8 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 15 / 15 Podpod 100% 2 / 2 totaltotal 94.7% 90 / 95 CC 7 SCAR 6.2
CC
7
Cov
95%
__ANON__
3.0
debug_ua
3.0
add_ua_handlers
3.0
CRAP 1.9
« ConsoleLogger.pm ↑ Summary ConsoleLogger/Everywhere.pm »
1 package LWP::ConsoleLogger::Easy;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 our $VERSION = '1.000002';
7  
8 9 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use LWP::ConsoleLogger ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Module::Load::Conditional qw( can_load );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Sub::Exporter -setup => { exports => ['debug_ua'] };
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use String::Trim qw( trim );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 my %VERBOSITY = (
16 dump_content => 8,
17 dump_cookies => 6,
18 dump_headers => 5,
19 dump_params => 4,
20 dump_status => 2,
21 dump_text => 7,
22 dump_title => 3,
23 dump_uri => 1,
24 );
25  
26 sub debug_ua {
27 26 ▶ my $ua = shift;
Subroutine debug_ua: called CC=3 SCAR=11.0
Pod documented
28 26 my $level = shift;
29 26 ▶ $level //= 10;
Condition100%2/2
$level //= 10
l!l
323
MC/DC100%1/1
$level //= 10
$level
Truth table100%2/2
A
A= $level
Aresult
11
00
30  
31 26 my %args = map { $_ => $VERBOSITY{$_} <= $level } keys %VERBOSITY;
32 26 my $console_logger = LWP::ConsoleLogger->new(%args);
33  
34 26 add_ua_handlers( $ua, $console_logger );
35  
36 26 ▶ if ( can_load( modules => { 'HTML::FormatText::Lynx' => 23 } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_load("modules", {"HTML::FormatText::Lynx", 23})) 26 0 26
37 $console_logger->text_pre_filter(
38 sub {
39 18 ▶ my $text = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
40 18 my $content_type = shift;
41 18 my $base_url = shift;
42  
43 18 ▶ return $text
Branch: 1 branch
BranchTrueFalseTotal
unless $content_type and $content_type =~ /html/i 5 13 18
Condition66%2/3
$content_type and $content_type =~ /html/i
!ll&&!rl&&r
0513
MC/DC50%1/2
$content_type and $content_type =~ /html/i
$content_type $content_type =~ /html/i
Truth table66%2/3
A and B
A= $content_typeB= $content_type =~ /html/i
ABresult
0X0
100
111
44 unless $content_type && $content_type =~ m{html}i;
45  
46 return (
47 13 trim(
48 HTML::FormatText::Lynx->format_string(
49 $text,
50 base => $base_url,
51 )
52 ),
53 'text/plain'
54 );
55 }
56 26 );
57 }
58  
59 26 return $console_logger;
60 }
61  
62 sub add_ua_handlers {
63 26 ▶ my $ua = shift;
Subroutine add_ua_handlers: called CC=3 SCAR=11.0
Pod documented
64 26 my $console_logger = shift;
65  
66 26 ▶ if ( $ua->isa('Test::WWW::Mechanize::Mojo') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ua->isa("Test::WWW::Mechanize::Mojo")) 0 26 26
67 0 $ua = $ua->tester->ua;
68 }
69 26 ▶ if ( $ua->isa('Mojo::UserAgent') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ua->isa("Mojo::UserAgent")) 7 19 26
70 $ua->on(
71 'start',
72 sub {
73 6 ▶ my $the_ua = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
74 6 my $tx = shift;
75  
76 6 my $request = HTTP::Request->parse( $tx->req->to_string );
77 6 $console_logger->request_callback(
78 $request,
79 $the_ua,
80 );
81  
82 $tx->on(
83 'finish',
84 sub {
85 1 ▶ my $tx = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
86 1 my $res
87 = HTTP::Response->parse( $tx->res->to_string );
88 1 $res->request($request);
89 1 $console_logger->response_callback( $res, $the_ua );
90 }
91 6 );
92 }
93 7 );
94 7 return;
95 }
96  
97 $ua->add_handler(
98 'response_done',
99 21 ▶ sub { $console_logger->response_callback(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
100 19 );
101 $ua->add_handler(
102 'request_send',
103 21 ▶ sub { $console_logger->request_callback(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
104 19 );
105 }
106  
107 9 1;
108  
109 =pod
110  
111 =encoding UTF-8
112  
113 =head1 NAME
114  
115 LWP::ConsoleLogger::Easy - Easy LWP tracing and debugging
116  
117 =head1 VERSION
118  
119 version 1.000002
120  
121 =head1 SYNOPSIS
122  
123 use LWP::ConsoleLogger::Easy qw( debug_ua );
124 use WWW::Mechanize;
125  
126 my $mech = WWW::Mechanize->new;
127 my $logger = debug_ua( $mech );
128 $mech->get('https://google.com');
129  
130 # now watch the console for debugging output
131  
132 # ...
133 # stop dumping headers
134 $logger->dump_headers( 0 );
135  
136 # Redact sensitive data
137 $ENV{LWPCL_REDACT_HEADERS} = 'Authorization,Foo,Bar';
138 $ENV{LWPCL_REDACT_PARAMS} = 'seekrit,password,credit_card';
139  
140 my $quiet_logger = debug_ua( $mech, 1 );
141  
142 my $noisy_logger = debug_ua( $mech, 5 );
143  
144 =head1 DESCRIPTION
145  
146 This module gives you the easiest possible introduction to
147 L<LWP::ConsoleLogger>. It offers one wrapper around L<LWP::ConsoleLogger>:
148 C<debug_ua>. This function allows you to get up and running quickly with just
149 a couple of lines of code. It instantiates user-agent logging and also returns
150 a L<LWP::ConsoleLogger> object, which you may then tweak to your heart's
151 desire.
152  
153 If you're able to install L<HTML::FormatText::Lynx> then you'll get highly
154 readable HTML to text conversions.
155  
156 =head1 FUNCTIONS
157  
158 =head2 debug_ua( $ua, $verbosity )
159  
160 When called without a verbosity argument, this function turns on all logging.
161 I'd suggest going with this to start with and then turning down the verbosity
162 after that. This method returns an L<LWP::ConsoleLogger> object, which you
163 may tweak to your heart's desire.
164  
165 my $ua_logger = debug_ua( $ua );
166 $ua_logger->content_pre_filter( sub {...} );
167 $ua_logger->logger( Log::Dispatch->new(...) );
168  
169 $ua->get(...);
170  
171 C<$ua> may be one of several user-agents, including C<LWP::UserAgent>,
172 C<Mojo::UserAgent>, and C<WWW::Mechanize>.
173  
174 You can provide a verbosity level of 0 or more. (Currently 0 - 8 supported.)
175 This will turn up the verbosity on your output gradually. A verbosity of 0
176 will display nothing. 8 will display all available outputs.
177  
178 # don't get too verbose
179 my $ua_logger = debug_ua( $ua, 4 );
180  
181 =head2 add_ua_handlers
182  
183 This method sets up response and request handlers on your user agent. This is
184 done for you automatically if you're using C<debug_ua>.
185  
186 =head1 ENVIRONMENT VARIABLES
187  
188 =head2 LWPCL_REDACT_HEADERS
189  
190 A comma-separated list of header values to redact from output.
191  
192 $ENV{LWPCL_REDACT_HEADERS} = 'Authorization,Foo,Bar';
193  
194 Output will be something like:
195  
196 .----------------+------------------.
197 | Request Header | Value |
198 +----------------+------------------+
199 | Authorization | [REDACTED] |
200 | Content-Length | 0 |
201 | User-Agent | libwww-perl/6.15 |
202 '----------------+------------------'
203  
204 Use at the command line.
205  
206 LWPCL_REDACT_HEADERS='Authorization,Foo,Bar' perl script.pl
207  
208 =head2 LWPCL_REDACT_PARAMS
209  
210 A comma-separated list of parameter values to redact from output.
211  
212 $ENV{LWPCL_REDACT_PARAMS} = 'credit_card,foo,bar';
213  
214 Use at the command line.
215  
216 LWPCL_REDACT_PARAMS='credit_card,foo,bar' perl script.pl
217  
218 .-------------+------------.
219 | Key | Value |
220 +-------------+------------+
221 | credit_card | [REDACTED] |
222 '-------------+------------'
223  
224 =head2 CAVEATS
225  
226 Text formatting now defaults to attempting to use L<HTML::FormatText::Lynx> to
227 format HTML as text. If you do not have this installed, we'll fall back to
228 using HTML::Restrict to remove any HTML tags which you have not specifically
229 whitelisted.
230  
231 If you have L<HTML::FormatText::Lynx> installed, but you don't want to use it,
232 override the default filter:
233  
234 my $logger = debug_ua( $mech );
235 $logger->text_pre_filter( sub { return shift } );
236  
237 =head2 EXAMPLES
238  
239 Please see the "examples" folder in this distribution for more ideas on how to
240 use this module.
241  
242 =head1 AUTHOR
243  
244 Olaf Alders <olaf@wundercounter.com>
245  
246 =head1 COPYRIGHT AND LICENSE
247  
248 This software is Copyright (c) 2014 by MaxMind, Inc.
249  
250 This is free software, licensed under:
251  
252 The Artistic License 2.0 (GPL Compatible)
253  
254 =cut
255  
256 __END__
257  
258 # ABSTRACT: Easy LWP tracing and debugging
259  
260  
261 # ABSTRACT: Start logging your LWP useragent the easy way.
« ConsoleLogger.pm ↑ Summary ConsoleLogger/Everywhere.pm »