File Coverage

blib/lib/LWP/ConsoleLogger/Everywhere.pm
Criterion Covered Total %
statement 28 28 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 39 39 100.0


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