| 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.000003'; | |||||||||||||||||||||||||||
| 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
Condition66%2/3
exists $ENV{$key} and $ENV{$key}
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}
| |||||||||||||||||||||||||||||
| 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 | 11 | ▶ | my $code_injection = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 35 | 11 | my $orig = shift; | |||||||||||||||||||||||||||
| 36 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 11 | my $ua = $self->$orig(@_); | |||||||||||||||||||||||||||
| 39 | 11 | my $debug_ua = debug_ua($ua); | |||||||||||||||||||||||||||
| 40 | 11 | ▶ | $debug_ua->logger($dispatch_logger) if $dispatch_logger; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 11 | push @{$loggers}, $debug_ua; | |||||||||||||||||||||||||||
| 42 | 11 | return $ua; | |||||||||||||||||||||||||||
| 43 | 3 | }; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 3 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 3 | require_module('LWP::UserAgent'); | |||||||||||||||||||||||||||
| 47 | 3 | Class::Method::Modifiers::install_modifier( | |||||||||||||||||||||||||||
| 48 | 'LWP::UserAgent', 'around', | ||||||||||||||||||||||||||||
| 49 | 'new' => $code_injection | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | 3 | }; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 3 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 3 | require_module('Mojo::UserAgent'); | |||||||||||||||||||||||||||
| 55 | 3 | Class::Method::Modifiers::install_modifier( | |||||||||||||||||||||||||||
| 56 | 'Mojo::UserAgent', 'around', | ||||||||||||||||||||||||||||
| 57 | 'new' => $code_injection | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | 3 | }; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 3 | require_module('HTTP::Tiny'); | |||||||||||||||||||||||||||
| 63 | 3 | Class::Method::Modifiers::install_modifier( | |||||||||||||||||||||||||||
| 64 | 'HTTP::Tiny', 'around', | ||||||||||||||||||||||||||||
| 65 | 'new' => $code_injection | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | 3 | }; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 4 | ▶ | sub loggers { | ||||||||||||||||||||||||||
|
Subroutine
loggers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 4 | return $loggers; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 75 | 1 | my $setting = shift; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | foreach my $logger ( @{$loggers} ) { | |||||||||||||||||||||||||||
| 78 | 9 | $logger->$setting(@_); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 3 | 1; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =pod | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head1 NAME | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | LWP::ConsoleLogger::Everywhere - LWP tracing everywhere | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head1 VERSION | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | version 1.000003 | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | use LWP::ConsoleLogger::Everywhere; | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # somewhere deep down in the guts of your program | ||||||||||||||||||||||||||||
| 103 | # there is some other module that creates an LWP::UserAgent | ||||||||||||||||||||||||||||
| 104 | # and now it will tell you what it's up to | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # somewhere else you can access and fine-tune those loggers | ||||||||||||||||||||||||||||
| 107 | # individually: | ||||||||||||||||||||||||||||
| 108 | my $loggers = LWP::ConsoleLogger::Everywhere->loggers; | ||||||||||||||||||||||||||||
| 109 | $loggers->[0]->pretty(0); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # or all of them at once: | ||||||||||||||||||||||||||||
| 112 | LWP::ConsoleLogger::Everywhere->set( pretty => 1); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Redact sensitive data for all user agents | ||||||||||||||||||||||||||||
| 115 | $ENV{LWPCL_REDACT_HEADERS} = 'Authorization,Foo,Bar'; | ||||||||||||||||||||||||||||
| 116 | $ENV{LWPCL_REDACT_PARAMS} = 'seekrit,password,credit_card'; | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # Or observe without changing your code | ||||||||||||||||||||||||||||
| 119 | PERL5OPT="-MLWP::ConsoleLogger::Everywhere" carton install | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | perl -MLWP::ConsoleLogger::Everywhere my-script.pl | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | This module turns on L<LWP::ConsoleLogger::Easy> debugging for every L<LWP::UserAgent>, | ||||||||||||||||||||||||||||
| 126 | L<Mojo::UserAgent>, or L<HTTP::Tiny> | ||||||||||||||||||||||||||||
| 127 | based user agent anywhere in your code. It doesn't matter what package or class it is in, | ||||||||||||||||||||||||||||
| 128 | or if you have access to the object itself. All you need to do is C<use> this module | ||||||||||||||||||||||||||||
| 129 | anywhere in your code and it will work. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | You can access and configure the loggers individually after they have been created | ||||||||||||||||||||||||||||
| 132 | using the C<loggers> class method. To change all of them at once, use the C<set> class | ||||||||||||||||||||||||||||
| 133 | method instead. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | See | ||||||||||||||||||||||||||||
| 136 | L<https://www.olafalders.com/2021/12/01/observing-network-traffic-with-lwp-consolelogger-everywhere/> | ||||||||||||||||||||||||||||
| 137 | for a practical example of how to use this module. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head2 set( <setting> => <value> ) | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | LWP::ConsoleLogger::Everywhere->set( dump_content => 0 ); | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | This class method changes the given setting on all logger objects that have been created | ||||||||||||||||||||||||||||
| 146 | so far. The first argument is the accessor name of the setting you want to change, and the | ||||||||||||||||||||||||||||
| 147 | second argument is the new value. This cannot be used to access current values. See | ||||||||||||||||||||||||||||
| 148 | L<LWP::ConsoleLogger#SUBROUTINES/METHODS> for what those settings are. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 loggers | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | my $loggers = LWP::ConsoleLogger::Everywhere->loggers; | ||||||||||||||||||||||||||||
| 153 | foreach my $logger ( @{ $loggers } ) { | ||||||||||||||||||||||||||||
| 154 | # stop dumping headers | ||||||||||||||||||||||||||||
| 155 | $logger->dump_headers( 0 ); | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | This class method returns an array reference of all L<LWP::ConsoleLogger> objects that have | ||||||||||||||||||||||||||||
| 159 | been created so far, with the newest one last. You can use them to fine-tune settings. If there | ||||||||||||||||||||||||||||
| 160 | is more than one user agent in your application you will need to figure out which one is which. | ||||||||||||||||||||||||||||
| 161 | Since this is for debugging only, trial and error is a good strategy here. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head1 ENVIRONMENT VARIABLES | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head2 LWPCL_LOGFILE | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | By default all data will be dumped to your console (as the name of this module implies) using | ||||||||||||||||||||||||||||
| 168 | L<Log::Dispatch>. You may change this behavior though. The general approach is to do it from | ||||||||||||||||||||||||||||
| 169 | within your script, for example like this: | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | use LWP::ConsoleLogger::Everywhere; | ||||||||||||||||||||||||||||
| 172 | my $loggers = LWP::ConsoleLogger::Everywhere->loggers; | ||||||||||||||||||||||||||||
| 173 | my $log_dispatch = Log::Dispatch->new( | ||||||||||||||||||||||||||||
| 174 | outputs => [ | ||||||||||||||||||||||||||||
| 175 | [ 'File', min_level => 'debug', filename => 'log_file.txt' ], | ||||||||||||||||||||||||||||
| 176 | [ 'Screen', min_level => 'debug' ], | ||||||||||||||||||||||||||||
| 177 | ], | ||||||||||||||||||||||||||||
| 178 | ); | ||||||||||||||||||||||||||||
| 179 | foreach my $logger ( @{ $loggers } ) { | ||||||||||||||||||||||||||||
| 180 | $logger->logger($log_dispatch); | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | The second approach is simpler and is done via an environment variable, for example you can run | ||||||||||||||||||||||||||||
| 184 | your script like this: | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | LWPCL_LOGFILE=foo.log perl -MLWP::ConsoleLogger::Everywhere foo.pl | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | this will be equivalent to the first approach with the following Log::Dispatch logger: | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | my $log_dispatch = Log::Dispatch->new( | ||||||||||||||||||||||||||||
| 191 | outputs => [ [ 'File', min_level => 'debug', filename => 'foo.log' ] ], | ||||||||||||||||||||||||||||
| 192 | ); | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 CAVEATS | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | If there are several different user agents in your application, you will get debug | ||||||||||||||||||||||||||||
| 197 | output from all of them. This could be quite cluttered. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Since L<LWP::ConsoleLogger::Everywhere> does its magic during compile time it will | ||||||||||||||||||||||||||||
| 200 | most likely catch every user agent in your application, unless | ||||||||||||||||||||||||||||
| 201 | you C<use LWP::ConsoleLogger::Everywhere> inside a file that gets loaded at runtime. | ||||||||||||||||||||||||||||
| 202 | If the user agent you wanted to debug had already been created at that time it | ||||||||||||||||||||||||||||
| 203 | cannot hook into the constructor any more. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | L<LWP::ConsoleLogger::Everywhere> works by catching new user agents directly in | ||||||||||||||||||||||||||||
| 206 | L<LWP::UserAgent> when they are created. That way all properly implemented sub classes | ||||||||||||||||||||||||||||
| 207 | like L<WWW::Mechanize> will go through it. But if you encounter one that installs its | ||||||||||||||||||||||||||||
| 208 | own handlers into the user agent after calling C<new> in L<LWP::UserAgent> | ||||||||||||||||||||||||||||
| 209 | that might overwrite the ones L<LWP::ConsoleLogger> installed. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | L<LWP::ConsoleLogger::Everywhere> will keep references to all user agents that were | ||||||||||||||||||||||||||||
| 212 | ever created during for the lifetime of your application. If you have a lot of lexical | ||||||||||||||||||||||||||||
| 213 | user agents that you recycle all the time they will not actually go away and might | ||||||||||||||||||||||||||||
| 214 | consume memory. This is especially easy to hit with L<HTTP::Tiny>, where the common | ||||||||||||||||||||||||||||
| 215 | C<< HTTP::Tiny->new->get(...) >> one-shot idiom creates a fresh logger on every call. | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | L<HTTP::Tiny> traffic is only captured when the user agent is created via | ||||||||||||||||||||||||||||
| 218 | C<< HTTP::Tiny->new >>. Class-method calls such as C<< HTTP::Tiny->get(...) >> | ||||||||||||||||||||||||||||
| 219 | bypass the constructor and are not logged. Also, because C<< HTTP::Tiny->mirror >> | ||||||||||||||||||||||||||||
| 220 | streams the response body straight to a file, the body and text tables will be | ||||||||||||||||||||||||||||
| 221 | empty for C<mirror> requests. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | The L<HTTP::Tiny> path has a few further limitations that the L<LWP::UserAgent> | ||||||||||||||||||||||||||||
| 224 | and L<Mojo::UserAgent> paths do not share: | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =over 4 | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =item * | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Redirect chains are collapsed. L<HTTP::Tiny> follows redirects internally and | ||||||||||||||||||||||||||||
| 231 | only the final response is visible to the logger, so intermediate hops are not | ||||||||||||||||||||||||||||
| 232 | logged. The LWP and L<Mojo::UserAgent> paths log each hop separately. | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =item * | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | Some headers that L<HTTP::Tiny> adds at the transport layer are reconstructed | ||||||||||||||||||||||||||||
| 237 | on a best-effort basis and may not exactly match what goes out on the wire. For | ||||||||||||||||||||||||||||
| 238 | example the C<Connection> header and an C<Authorization> header synthesized from | ||||||||||||||||||||||||||||
| 239 | C<userinfo> in the request URL are not reflected in the logged request. | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =item * | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | Cookie values are B<not> redacted by the C<LWPCL_REDACT_*> environment | ||||||||||||||||||||||||||||
| 244 | variables. This is existing behaviour shared with the LWP and L<Mojo::UserAgent> paths and is | ||||||||||||||||||||||||||||
| 245 | noted here so the logged output is not mistaken for a redacted view. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =back | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | For more information or if you want more detailed control see L<LWP::ConsoleLogger>. | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | Olaf Alders <olaf@wundercounter.com> | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | This software is Copyright (c) 2014 by MaxMind, Inc. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | =cut | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | __END__ | ||||||||||||||||||||||||||||