ConsoleLogger/Everywhere.pm

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