File Coverage

blib/lib/LWP/ConsoleLogger/Everywhere.pm
Criterion Covered Total %
statement 31 31 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 43 43 100.0


line stmt bran cond sub pod time code
1             use strict;
2 1     1   248027 use warnings;
  1         11  
  1         25  
3 1     1   4  
  1         2  
  1         33  
4             our $VERSION = '0.000044';
5              
6             use Class::Method::Modifiers ();
7 1     1   404 use LWP::ConsoleLogger::Easy qw( debug_ua );
  1         1373  
  1         21  
8 1     1   361 use LWP::UserAgent ();
  1         4  
  1         9  
9 1     1   202 use Module::Runtime qw( require_module );
  1         4  
  1         18  
10 1     1   5 use Try::Tiny qw( try );
  1         3  
  1         8  
11 1     1   49  
  1         2  
  1         36  
12             no warnings 'once';
13 1     1   5  
  1         2  
  1         269  
14             my $loggers;
15              
16             Class::Method::Modifiers::install_modifier(
17             'LWP::UserAgent',
18             'around',
19             'new' => sub {
20             my $orig = shift;
21             my $self = shift;
22              
23             my $ua = $self->$orig(@_);
24             push @{$loggers}, debug_ua($ua);
25              
26             return $ua;
27             }
28             );
29              
30             try {
31             require_module('Mojo::UserAgent');
32             Class::Method::Modifiers::install_modifier(
33             'Mojo::UserAgent',
34             'around',
35             'new' => sub {
36             my $orig = shift;
37             my $self = shift;
38              
39             my $ua = $self->$orig(@_);
40             push @{$loggers}, debug_ua($ua);
41              
42             return $ua;
43             }
44             );
45             };
46              
47             return $loggers;
48             }
49 2     2 1 2233  
50             my $class = shift;
51             my $setting = shift;
52              
53 1     1 1 714 foreach my $logger ( @{$loggers} ) {
54 1         2 $logger->$setting(@_);
55             }
56 1         2  
  1         4  
57 8         240 return;
58             }
59              
60 1         19 1;
61              
62             =pod
63              
64             =encoding UTF-8
65              
66             =head1 NAME
67              
68             LWP::ConsoleLogger::Everywhere - LWP tracing everywhere
69              
70             =head1 VERSION
71              
72             version 0.000044
73              
74             =head1 SYNOPSIS
75              
76             use LWP::ConsoleLogger::Everywhere;
77              
78             # somewhere deep down in the guts of your program
79             # there is some other module that creates an LWP::UserAgent
80             # and now it will tell you what it's up to
81              
82             # somewhere else you can access and fine-tune those loggers
83             # individually:
84             my $loggers = LWP::ConsoleLogger::Everywhere->loggers;
85             $loggers->[0]->pretty(0);
86              
87             # or all of them at once:
88             LWP::ConsoleLogger::Everywhere->set( pretty => 1);
89              
90             # Redact sensitive data for all user agents
91             $ENV{LWPCL_REDACT_HEADERS} = 'Authorization,Foo,Bar';
92             $ENV{LWPCL_REDACT_PARAMS} = 'seekrit,password,credit_card';
93              
94             # Or observe without changing your code
95             PERL5OPT="-MLWP::ConsoleLogger::Everywhere" carton install
96              
97             perl -MLWP::ConsoleLogger::Everywhere my-script.pl
98              
99             =head1 DESCRIPTION
100              
101             This module turns on L<LWP::ConsoleLogger::Easy> debugging for every L<LWP::UserAgent> or L<Mojo::UserAgent>
102             based user agent anywhere in your code. It doesn't matter what package or class it is in,
103             or if you have access to the object itself. All you need to do is C<use> this module
104             anywhere in your code and it will work.
105              
106             You can access and configure the loggers individually after they have been created
107             using the C<loggers> class method. To change all of them at once, use the C<set> class
108             method instead.
109              
110             See
111             L<https://www.olafalders.com/2021/12/01/observing-network-traffic-with-lwp-consolelogger-everywhere/>
112             for a practical example of how to use this module.
113              
114             =head1 CLASS METHODS
115              
116             =head2 set( <setting> => <value> )
117              
118             LWP::ConsoleLogger::Everywhere->set( dump_content => 0 );
119              
120             This class method changes the given setting on all logger objects that have been created
121             so far. The first argument is the accessor name of the setting you want to change, and the
122             second argument is the new value. This cannot be used to access current values. See
123             L<LWP::ConsoleLogger#SUBROUTINES/METHODS> for what those settings are.
124              
125             =head2 loggers
126              
127             my $loggers = LWP::ConsoleLogger::Everywhere->loggers;
128             foreach my $logger ( @{ $loggers } ) {
129             # stop dumping headers
130             $logger->dump_headers( 0 );
131             }
132              
133             This class method returns an array reference of all L<LWP::ConsoleLogger> objects that have
134             been created so far, with the newest one last. You can use them to fine-tune settings. If there
135             is more than one user agent in your application you will need to figure out which one is which.
136             Since this is for debugging only, trial and error is a good strategy here.
137              
138             =head1 CAVEATS
139              
140             If there are several different user agents in your application, you will get debug
141             output from all of them. This could be quite cluttered.
142              
143             Since L<LWP::ConsoleLogger::Everywhere> does its magic during compile time it will
144             most likely catch every user agent in your application, unless
145             you C<use LWP::ConsoleLogger::Everywhere> inside a file that gets loaded at runtime.
146             If the user agent you wanted to debug had already been created at that time it
147             cannot hook into the constructor any more.
148              
149             L<LWP::ConsoleLogger::Everywhere> works by catching new user agents directly in
150             L<LWP::UserAgent> when they are created. That way all properly implemented sub classes
151             like L<WWW::Mechanize> will go through it. But if you encounter one that installs its
152             own handlers into the user agent after calling C<new> in L<LWP::UserAgent>
153             that might overwrite the ones L<LWP::ConsoleLogger> installed.
154              
155             L<LWP::ConsoleLogger::Everywhere> will keep references to all user agents that were
156             ever created during for the lifetime of your application. If you have a lot of lexical
157             user agents that you recycle all the time they will not actually go away and might
158             consume memory.
159              
160             =head1 SEE ALSO
161              
162             For more information or if you want more detailed control see L<LWP::ConsoleLogger>.
163              
164             =head1 AUTHOR
165              
166             Olaf Alders <olaf@wundercounter.com>
167              
168             =head1 COPYRIGHT AND LICENSE
169              
170             This software is Copyright (c) 2014 by MaxMind, Inc.
171              
172             This is free software, licensed under:
173              
174             The Artistic License 2.0 (GPL Compatible)
175              
176             =cut
177              
178              
179             # ABSTRACT: LWP tracing everywhere
180