LWP::ConsoleLogger(3)
LWP::ConsoleLogger π
okay bestie, so this is LWP::ConsoleLogger β the LWP tracing and debugging queen π (you'll see her in your man pages as LWP::ConsoleLogger(3), iykyk).
β¨ VERSION β¨
version 1.000003 β yeah she's got lore π
πΈ SYNOPSIS πΈ
The simplest way to get started is by adding LWP::ConsoleLogger::Everywhere to your code and then just watching your output. No thoughts, head empty, just vibes.
use LWP::ConsoleLogger::Everywhere ();
If you need more control, look at LWP::ConsoleLogger::Easy. She's the lowkey responsible older sister π«Ά
use LWP::ConsoleLogger::Easy qw( debug_ua );
use WWW::Mechanize;
my $mech = WWW::Mechanize->new; # or LWP::UserAgent->new() etc
my $console_logger = debug_ua( $mech );
$mech->get( 'https://metacpan.org' );
# now watch the console for debugging output
# turn off header dumps
$console_logger->dump_headers( 0 );
$mech->get( $some_other_url );
To get down to the lowest level, use LWP::ConsoleLogger directly. Main character energy, no wrapper, raw and unfiltered β¨
my $ua = LWP::UserAgent->new( cookie_jar => {} );
my $console_logger = LWP::ConsoleLogger->new(
dump_content => 1,
dump_text => 1,
content_pre_filter => sub {
my $content = shift;
my $content_type = shift;
# mangle content here
# ...
return $content;
},
);
$ua->default_header(
'Accept-Encoding' => scalar HTTP::Message::decodable() );
$ua->add_handler( 'response_done',
sub { $console_logger->response_callback( @_ ) } );
$ua->add_handler( 'request_send',
sub { $console_logger->request_callback( @_ ) } );
# now watch debugging output to your screen
$ua->get( 'http://nytimes.com/' );
Sample output might look like this. Not me being obsessed with the little ASCII tables π they ate fr fr:
GET http://www.nytimes.com/2014/04/24/technology/fcc-new-net-neutrality-rules.html
GET params:
+-----+-------+
| Key | Value |
+-----+-------+
| _r | 1 |
| hp | |
+-----+-------+
+-----------------+--------------------------------+
| Request Header | Value |
+-----------------+--------------------------------+
| Accept-Encoding | gzip |
| Cookie2 | $Version="1" |
| Referer | http://www.nytimes.com?foo=bar |
| User-Agent | WWW-Mechanize/1.73 |
+-----------------+--------------------------------+
==> 200 OK
Title: The New York Times - Breaking News, World News & Multimedia
+--------------------------+-------------------------------+
| Response Header | Value |
+--------------------------+-------------------------------+
| Accept-Ranges | bytes |
| Age | 176 |
| Cache-Control | no-cache |
| Channels | NytNow |
| Client-Date | Fri, 30 May 2014 22:37:42 GMT |
| Client-Peer | 170.149.172.130:80 |
| Client-Response-Num | 1 |
| Client-Transfer-Encoding | chunked |
| Connection | keep-alive |
| Content-Encoding | gzip |
| Content-Type | text/html; charset=utf-8 |
| Date | Fri, 30 May 2014 22:37:41 GMT |
| NtCoent-Length | 65951 |
| Server | Apache |
| Via | 1.1 varnish |
| X-Cache | HIT |
| X-Varnish | 1142859770 1142854917 |
+--------------------------+-------------------------------+
+----------------------------------------------------------+
| Text |
+----------------------------------------------------------+
| F.C.C., in a Shift, Backs Fast Lanes for Web Traffic... |
+----------------------------------------------------------+
π DESCRIPTION π
ngl, debugging mechanize scripts can be hard β or at minimum soooo tedious π. LWP::Debug is deprecated, bestie, it's giving legacy. It suggests you write your own debugging handlers, set up a proxy, or install Wireshark. Those are all workable solutions, no cap β but this module exists to save you some of that work. The guts of this module are stolen from Plack::Middleware::DebugLogging, which in turn stole most of its internals from Catalyst. Petty theft lineage, iconic honestly π
If you're new to LWP::ConsoleLogger, I suggest getting started with the LWP::ConsoleLogger::Easy wrapper. This will get you up and running in minutes β literally. If you need to tweak the settings that LWP::ConsoleLogger::Easy chooses for you (or if you just want to be fancy πΈ), please read on.
Since this is a debugging library, I've left as much mutable state as possible, so that you can easily toggle output on and off and otherwise adjust how you deal with the output. Flip switches, slay, repeat π₯
π CONSTRUCTOR π
π new()
The following arguments can be passed to new(), although none are required β zero pressure, she's chill π«Ά. They can also be called as methods on an instantiated object. I'll list them here and discuss them in detail below.
dump_content => 0|1β dump the content or nahdump_cookies => 0|1β cookies, show them or hide themdump_headers => 0|1β headers, on or offdump_params => 0|1β params, in or outdump_status => 0|1β status, yes or nodump_text => 0|1β text dumping, toggleddump_title => 0|1β title dumping, toggleddump_text => 0|1β (yes,dump_textis listed a second time in the original, and we are respecting her choices, periodt)dump_uri => 0|1β URI dumping, toggledcontent_pre_filter => sub { ... }β a coderef to pre-filter contentheaders_to_redact => ['Authentication', 'Foo']β headers you want redacted, as an arrayrefparams_to_redact => ['token', 'password']β params you want redacted, as an arrayreftext_pre_filter => sub { ... }β a coderef to pre-filter texthtml_restrict => HTML::Restrict->new( ... )β anHTML::Restrictobjectlogger => Log::Dispatch->new( ... )β aLog::Dispatchobjectpretty => 0|1β pretty output, on or offterm_width => $integerβ terminal width, as an integer
bestie there's more, don't go anywhere π
β¨ SUBROUTINES/METHODS β¨
Okay bestie, this is the part where we go through every single knob you can twist. There's a LOT. Buckle up, we're doing this alphabetically-ish and thoroughly. π
π dump_content( 0|1 )
Boolean value, so it's a yes/no situation. If it's true, the actual content of your response (like HTML, JSON, whatever the vibe is) gets dumped straight to your screen. Defaults to false, so it's chillin' off by default. π
πͺ dump_cookies( 0|1 )
Boolean value. If true, the content of your cookies gets dumped to your screen. Defaults to false. Not me being nosy about your cookies... okay yes, exactly that. π
π dump_headers( 0|1 )
Boolean value. If true, both request and response headers get dumped to your screen. Defaults to true, so this one is already on, living its best life.
Headers are dumped in alphabetical order. Neat, organized, iconic. β¨
π dump_params( 0|1 )
Boolean value. If true, both GET and POST params get dumped to your screen. Defaults to true.
Params are dumped in alphabetical order too. We love consistency. π
π¦ dump_status( 0|1 )
Boolean value. If true, it dumps the HTTP response code for each page being visited. Defaults to true. So you'll know immediately if something's giving 404 or 500 or, god forbid, 418. π
π dump_text( 0|1 )
Boolean value. If true, it dumps the text of your page after both the content_pre_filter and text_pre_filters have been applied. Defaults to true. Order matters, bestie. π
π·οΈ dump_title( 0|1 )
Boolean value. If true, it dumps the titles of HTML pages β but only if your UserAgent has a "title" method and if it returns something useful. Defaults to true. No title? No drama, it just skips. π¦
π dump_uri( 0|1 )
Boolean value. If true, it dumps the URI of each page being visited. Defaults to true. Main character energy: every page announces itself. π
π pretty ( 0|1 )
Boolean value. If you disable it, request headers, response headers, content and text sections will be dumped without using tables. Super handy for copy/pasting JSON and stuff for faking responses later. Defaults to true. Tables are cute but sometimes you just need the raw goods. π
π§ content_pre_filter( sub { ... } )
Subroutine reference. This one lets you manipulate content before it gets dumped. A common use case? Stripping headers and footers away from HTML content so it's way easier to detect changes in the body of the page. π
$easy_logger->content_pre_filter(
sub {
my $content = shift;
my $content_type = shift; # the value of the Content-Type header
if ( $content_type =~ m{html}i
&& $content =~ m{<!--\scontent\s-->(.*)<!--\sfooter}msx ) {
return $1;
}
return $content;
}
);
Try to make sure that your content mangling doesn't return broken HTML, because that may not play well with HTML::Restrict. Fr fr, be careful. π
π‘ request_callback
Use this handler to set up console logging on your requests.
my $ua = LWP::UserAgent->new;
$ua->add_handler(
'request_send',
sub { $console_logger->request_callback(@_) }
);
This is done for you by default if you set up your logging via LWP::ConsoleLogger::Easy. So if you're using Easy, you can just vibe and let it handle things. β¨
π₯ response_callback
Use this handler to set up console logging on your responses.
my $ua = LWP::UserAgent->new;
$ua->add_handler(
'response_done',
sub { $console_logger->response_callback(@_) }
);
Also done for you by default if you set up your logging via LWP::ConsoleLogger::Easy. Same deal, bestie. π«Ά
π text_pre_filter( sub { ... } )
Subroutine reference. This lets you manipulate text before it gets dumped. A common use case is stripping away duplicate whitespace and/or newlines to improve formatting. Keep in mind that the "content_pre_filter" will have already been applied to the content that gets passed to the text_pre_filter. The whole idea is that you strip away HTML you don't care about in the content_pre_filter phase, then process the remainder of the content in the text_pre_filter. Chef's kiss pipeline. π©βπ³π
$easy_logger->text_pre_filter(
sub {
my $content = shift;
my $content_type = shift; # the value of the Content-Type header
my $base_url = shift;
# do something with the content
# ...
return ( $content, $new_content_type );
}
);
If your text_pre_filter() converts from HTML to plain text, be sure to return the new content type (text/plain) when you exit the sub. If you don't do this, HTML formatting will then be applied to your plain text as is explained below. So like, don't skip this step, it's giving chaos otherwise. π
If this is HTML content, HTML::Restrict will be applied after the text_pre_filter has been run. Then LWP::ConsoleLogger will strip away some whitespace and newlines from processed HTML in its own opinionated way, to present you with more readable text. She has opinions and she's sticking to them. π
π‘οΈ html_restrict( HTML::Restrict->new( ... ) )
If the content_type indicates HTML, then HTML::Restrict will be used to strip tags from your content during the text rendering process. You may pass your own HTML::Restrict object if you like! This is helpful in situations where you still do want to have some tags in your text. Customize away, bestie. π
πͺ΅ logger( Log::Dispatch->new( ... ) )
By default, all data gets dumped to your console (as the name of this module implies) using Log::Dispatch. But you can use your own Log::Dispatch module to facilitate logging to files or any other output which Log::Dispatch supports. Go off, log wherever you want. πβ¨
π term_width( $integer )
By default this module tries to find the maximum width of your terminal and uses all available space when displaying tabular data. You may use this parameter to constrain the tables to an arbitrary width. If your terminal is being extra, just tell it what to do. π
β οΈ CAVEATS β οΈ
Real talk time. This was written to suit the author's own needs, and there are a lot of things that haven't been considered. For example, it's mostly assuming that the content will be text, HTML, JSON or XML. No cap, that's the assumption. π
The test suite is not very robust either. So if you'd like to contribute to this module and you can't find an appropriate test, do add something to the example folder (either a new script or alter an existing one), so that the author can see what your patch does. Help a bestie out. π«Ά
π AUTHOR π
Olaf Alders <olaf@wundercounter.com>
π COPYRIGHT AND LICENSE π
This software is Copyright (c) 2014 by MaxMind, Inc.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
And that's the whole page, bestie!! π This was documented by the lovely perl folks at version perl v5.42.3, dated 2026-09-18 β go forth and log your little heart out. π¦β¨
π¦ where this page comes from
- fedora 43: perl-LWP-ConsoleLogger 1.0.3-1.fc43 Β· Artistic-2.0
- fedora 44: perl-LWP-ConsoleLogger 1.0.3-1.fc44 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms