dave
dave

Reputation: 1185

Mojolicious source IP and user agent

I have the following Mojolicious app which serving files from specific folder

use Mojolicious::Lite;
use Mojo::Log;

my $app = app;
my $static = $app->static;
push @{$static->paths} => 'c:\test';

$app->start

when I run:

perl mojo_server.pl daemon -m development

I get this:

[2021-05-18 19:46:39.22370] [29388] [info] Listening at "http://*:3000"
Server available at http://127.0.0.1:3000

and when I access a file from the web browser I am able to access the file, but there are no information on the STDERR after "Server available at http://127.0.0.1:3000" like what is the source address and the page requested and the user agent. How can I turn mojolicious to show those info on debug mode, if this possible?

Upvotes: 2

Views: 367

Answers (1)

clamp
clamp

Reputation: 3347

You can use after_static from HOOKS in Mojolicious:

use Mojolicious::Lite -signatures;
use Mojo::Log;

my $log = Mojo::Log->new;

hook after_static => sub ($c){
    $log->debug('original_remote_address : ' => $c->tx->original_remote_address);
    $log->debug('user_agent : ' => $c->tx->req->content->headers->user_agent );
    $log->debug('url : ' => $c->tx->req->url->to_abs);
};
app->start;

The after_static event is triggered when a static response has been generated.

Update: If you need a more general solution which is not specifically targeted at serving static files, take a look at the AccessLog plugin.

use Mojolicious::Lite -signatures;
use Mojo::Log;
use Mojolicious::Plugin::AccessLog;

my $log = Mojo::Log->new;
plugin 'AccessLog';
app->start;

Upvotes: 5

Related Questions