ConsoleLogger/Everywhere.pm

filter: Statementstmt 100% 54 / 54 Branchbran 100% 4 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 97.4% 76 / 78 CC 3 SCAR 2.4
CC
3
Cov
98%
__ANON__
2.0
set
2.0
BEGIN
1.0
CRAP 1.3
« ConsoleLogger/Easy.pm ↑ Summary
1 package LWP::ConsoleLogger::Everywhere;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = '1.000002';
6  
7 3 ▶use Class::Method::Modifiers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use LWP::ConsoleLogger::Easy qw( debug_ua );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Module::Runtime qw( require_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Try::Tiny qw( try );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Log::Dispatch ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 my $loggers;
15 my $dispatch_logger;
16  
17 {
18 3 my $key = "LWPCL_LOGFILE";
19 3 ▶ if ( exists $ENV{$key} && $ENV{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{$key} and $ENV{$key}) 2 1 3
Condition66%2/3
exists $ENV{$key} and $ENV{$key}
!ll&&!rl&&r
102
MC/DC50%1/2
exists $ENV{$key} and $ENV{$key}
exists $ENV{$key} $ENV{$key}
Truth table66%2/3
A and B
A= exists $ENV{$key}B= $ENV{$key}
ABresult
0X0
100
111
20 2 my $filename = $ENV{$key};
21 2 $dispatch_logger = Log::Dispatch->new(
22 outputs => [
23 [
24 'File',
25 min_level => 'debug',
26 filename => $filename,
27 binmode => ':encoding(UTF-8)',
28 ],
29 ],
30 );
31 }
32 }
33  
34 my $code_injection = sub {
35 10 ▶ my $orig = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
36 10 my $self = shift;
37  
38 10 my $ua = $self->$orig(@_);
39 10 my $debug_ua = debug_ua($ua);
40 10 ▶ $debug_ua->logger($dispatch_logger) if $dispatch_logger;
Branch: 1 branch
BranchTrueFalseTotal
if $dispatch_logger 2 8 10
41 10 push @{$loggers}, $debug_ua;
42 10 return $ua;
43 3 };
44  
45 try {
46 3 ▶ require_module('LWP::UserAgent');
Subroutine __ANON__: called CC=1 SCAR=0.0
47 3 Class::Method::Modifiers::install_modifier(
48 'LWP::UserAgent', 'around',
49 'new' => $code_injection
50 );
51 3 };
52  
53 try {
54 3 ▶ require_module('Mojo::UserAgent');
Subroutine __ANON__: called CC=1 SCAR=0.0
55 3 Class::Method::Modifiers::install_modifier(
56 'Mojo::UserAgent', 'around',
57 'new' => $code_injection
58 );
59 3 };
60  
61 sub loggers {
62 2 ▶ return $loggers;
Subroutine loggers: called CC=1 SCAR=0.0
Pod documented
63 }
64  
65 sub set {
66 1 ▶ my $class = shift;
Subroutine set: called CC=2 SCAR=6.9
Pod documented
67 1 my $setting = shift;
68  
69 1 foreach my $logger ( @{$loggers} ) {
70 8 $logger->$setting(@_);
71 }
72  
73 1 return;
74 }
75  
76 3 1;
77  
78 =pod
79  
80 =encoding UTF-8
81  
82 =head1 NAME
83  
84 LWP::ConsoleLogger::Everywhere - LWP tracing everywhere
85  
86 =head1 VERSION
87  
88 version 1.000002
89  
90 =head1 SYNOPSIS
91  
92 use LWP::ConsoleLogger::Everywhere;
93  
94 # somewhere deep down in the guts of your program
95 # there is some other module that creates an LWP::UserAgent
96 # and now it will tell you what it's up to
97  
98 # somewhere else you can access and fine-tune those loggers
99 # individually:
100 my $loggers = LWP::ConsoleLogger::Everywhere->loggers;
101 $loggers->[0]->pretty(0);
102  
103 # or all of them at once:
104 LWP::ConsoleLogger::Everywhere->set( pretty => 1);
105  
106 # Redact sensitive data for all user agents
107 $ENV{LWPCL_REDACT_HEADERS} = 'Authorization,Foo,Bar';
108 $ENV{LWPCL_REDACT_PARAMS} = 'seekrit,password,credit_card';
109  
110 # Or observe without changing your code
111 PERL5OPT="-MLWP::ConsoleLogger::Everywhere" carton install
112  
113 perl -MLWP::ConsoleLogger::Everywhere my-script.pl
114  
115 =head1 DESCRIPTION
116  
117 This module turns on L<LWP::ConsoleLogger::Easy> debugging for every L<LWP::UserAgent> or L<Mojo::UserAgent>
118 based user agent anywhere in your code. It doesn't matter what package or class it is in,
119 or if you have access to the object itself. All you need to do is C<use> this module
120 anywhere in your code and it will work.
121  
122 You can access and configure the loggers individually after they have been created
123 using the C<loggers> class method. To change all of them at once, use the C<set> class
124 method instead.
125  
126 See
127 L<https://www.olafalders.com/2021/12/01/observing-network-traffic-with-lwp-consolelogger-everywhere/>
128 for a practical example of how to use this module.
129  
130 =head1 CLASS METHODS
131  
132 =head2 set( <setting> => <value> )
133  
134 LWP::ConsoleLogger::Everywhere->set( dump_content => 0 );
135  
136 This class method changes the given setting on all logger objects that have been created
137 so far. The first argument is the accessor name of the setting you want to change, and the
138 second argument is the new value. This cannot be used to access current values. See
139 L<LWP::ConsoleLogger#SUBROUTINES/METHODS> for what those settings are.
140  
141 =head2 loggers
142  
143 my $loggers = LWP::ConsoleLogger::Everywhere->loggers;
144 foreach my $logger ( @{ $loggers } ) {
145 # stop dumping headers
146 $logger->dump_headers( 0 );
147 }
148  
149 This class method returns an array reference of all L<LWP::ConsoleLogger> objects that have
150 been created so far, with the newest one last. You can use them to fine-tune settings. If there
151 is more than one user agent in your application you will need to figure out which one is which.
152 Since this is for debugging only, trial and error is a good strategy here.
153  
154 =head1 ENVIRONMENT VARIABLES
155  
156 =head2 LWPCL_LOGFILE
157  
158 By default all data will be dumped to your console (as the name of this module implies) using
159 L<Log::Dispatch>. You may change this behavior though. The general approach is to do it from
160 within your script, for example like this:
161  
162 use LWP::ConsoleLogger::Everywhere;
163 my $loggers = LWP::ConsoleLogger::Everywhere->loggers;
164 my $log_dispatch = Log::Dispatch->new(
165 outputs => [
166 [ 'File', min_level => 'debug', filename => 'log_file.txt' ],
167 [ 'Screen', min_level => 'debug' ],
168 ],
169 );
170 foreach my $logger ( @{ $loggers } ) {
171 $logger->logger($log_dispatch);
172 }
173  
174 The second approach is simpler and is done via an environment variable, for example you can run
175 your script like this:
176  
177 LWPCL_LOGFILE=foo.log perl -MLWP::ConsoleLogger::Everywhere foo.pl
178  
179 this will be equivalent to the first approach with the following Log::Dispatch logger:
180  
181 my $log_dispatch = Log::Dispatch->new(
182 outputs => [ [ 'File', min_level => 'debug', filename => 'foo.log' ] ],
183 );
184  
185 =head1 CAVEATS
186  
187 If there are several different user agents in your application, you will get debug
188 output from all of them. This could be quite cluttered.
189  
190 Since L<LWP::ConsoleLogger::Everywhere> does its magic during compile time it will
191 most likely catch every user agent in your application, unless
192 you C<use LWP::ConsoleLogger::Everywhere> inside a file that gets loaded at runtime.
193 If the user agent you wanted to debug had already been created at that time it
194 cannot hook into the constructor any more.
195  
196 L<LWP::ConsoleLogger::Everywhere> works by catching new user agents directly in
197 L<LWP::UserAgent> when they are created. That way all properly implemented sub classes
198 like L<WWW::Mechanize> will go through it. But if you encounter one that installs its
199 own handlers into the user agent after calling C<new> in L<LWP::UserAgent>
200 that might overwrite the ones L<LWP::ConsoleLogger> installed.
201  
202 L<LWP::ConsoleLogger::Everywhere> will keep references to all user agents that were
203 ever created during for the lifetime of your application. If you have a lot of lexical
204 user agents that you recycle all the time they will not actually go away and might
205 consume memory.
206  
207 =head1 SEE ALSO
208  
209 For more information or if you want more detailed control see L<LWP::ConsoleLogger>.
210  
211 =head1 AUTHOR
212  
213 Olaf Alders <olaf@wundercounter.com>
214  
215 =head1 COPYRIGHT AND LICENSE
216  
217 This software is Copyright (c) 2014 by MaxMind, Inc.
218  
219 This is free software, licensed under:
220  
221 The Artistic License 2.0 (GPL Compatible)
222  
223 =cut
224  
225 __END__
226  
227 # ABSTRACT: LWP tracing everywhere
228  
« ConsoleLogger/Easy.pm ↑ Summary