File Coverage

blib/lib/LWP/ConsoleLogger/Easy.pm
Criterion Covered Total %
statement 58 59 98.3
branch 6 8 75.0
condition 4 5 80.0
subroutine 14 14 100.0
pod 2 2 100.0
total 84 88 95.4


line stmt bran cond sub pod time code
1             package LWP::ConsoleLogger::Easy;
2              
3 7     7   112990 use strict;
  7         47  
  7         200  
4 7     7   39 use warnings;
  7         12  
  7         289  
5              
6             our $VERSION = '1.000001';
7              
8 7     7   2325 use HTTP::Request ();
  7         110554  
  7         129  
9 7     7   2881 use HTTP::Response ();
  7         44122  
  7         140  
10 7     7   3557 use LWP::ConsoleLogger ();
  7         27  
  7         252  
11 7     7   3959 use Module::Load::Conditional qw( can_load );
  7         114928  
  7         499  
12 7     7   60 use Sub::Exporter -setup => { exports => ['debug_ua'] };
  7         16  
  7         109  
13 7     7   7158 use String::Trim qw( trim );
  7         4216  
  7         3644  
14              
15             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 23     23 1 519684 my $ua = shift;
28 23         63 my $level = shift;
29 23   100     257 $level //= 10;
30              
31 23         146 my %args = map { $_ => $VERBOSITY{$_} <= $level } keys %VERBOSITY;
  184         537  
32 23         586 my $console_logger = LWP::ConsoleLogger->new(%args);
33              
34 23         1778 add_ua_handlers( $ua, $console_logger );
35              
36 23 50       548 if ( can_load( modules => { 'HTML::FormatText::Lynx' => 23 } ) ) {
37             $console_logger->text_pre_filter(
38             sub {
39 15     15   7054 my $text = shift;
40 15         43 my $content_type = shift;
41 15         38 my $base_url = shift;
42              
43 15 100 66     200 return $text
44             unless $content_type && $content_type =~ m{html}i;
45              
46             return (
47 10         200 trim(
48             HTML::FormatText::Lynx->format_string(
49             $text,
50             base => $base_url,
51             )
52             ),
53             'text/plain'
54             );
55             }
56 23         285697 );
57             }
58              
59 23         901 return $console_logger;
60             }
61              
62             sub add_ua_handlers {
63 23     23 1 65 my $ua = shift;
64 23         88 my $console_logger = shift;
65              
66 23 50       323 if ( $ua->isa('Test::WWW::Mechanize::Mojo') ) {
67 0         0 $ua = $ua->tester->ua;
68             }
69 23 100       192 if ( $ua->isa('Mojo::UserAgent') ) {
70             $ua->on(
71             'start',
72             sub {
73 6     6   79459 my $the_ua = shift;
74 6         14 my $tx = shift;
75              
76 6         20 my $request = HTTP::Request->parse( $tx->req->to_string );
77 6         6669 $console_logger->request_callback(
78             $request,
79             $the_ua,
80             );
81              
82             $tx->on(
83             'finish',
84             sub {
85 1         17609 my $tx = shift;
86 1         6 my $res
87             = HTTP::Response->parse( $tx->res->to_string );
88 1         725 $res->request($request);
89 1         15 $console_logger->response_callback( $res, $the_ua );
90             }
91 6         158 );
92             }
93 7         99 );
94 7         81 return;
95             }
96              
97             $ua->add_handler(
98             'response_done',
99 18     18   395416 sub { $console_logger->response_callback(@_) }
100 16         150 );
101             $ua->add_handler(
102             'request_send',
103 18     18   171519 sub { $console_logger->request_callback(@_) }
104 16         565 );
105             }
106              
107             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.000001
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.