Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

request logging with Mojolicious

by tomgracey (Scribe)
on Oct 11, 2022 at 16:30 UTC ( [id://11147348]=perlquestion: print w/replies, xml ) Need Help??

tomgracey has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

I have a Mojolicious API which previously was running via apache, but I recently switched it to hypnotoad due to a memory leak issue. It is a lot happier running with hypnotoad. However, I was used to the comprehensive apache logs. I am sure hypnotoad/mojolicious can also do a decent job with request logs - but I am struggling to find much in the way of documentation (though I maybe looking in the wrong place).

I have added $self->log( Mojo::Log->new( path => $log_path, level => $log_level )); in startup and out of the box this seems to produce log lines as follows:

[date] [random number] [debug] [random code] POST "/endpoint" [date] [same random number] [debug] [same random code] Routing to a ca +llback

and that's all. The problem is I want to log the full details of the request - ie including the parameters. Now I realise that $self->log can be called inside a route to explicitly log something, and so I could catch the params there and $self->log at that point. However, that way if e.g. a request comes in that doesn't even match any of the registered routes it won't get logged. I am wondering if there is some way to get Mojolicious to include the parameters alongside the POST "/endpoint" line?

Alternatively, perhaps I am barking up the wrong tree expecting Mojolicious to do this job - after all it is global logging I'm after and Mojolicious is a framework not a webserver. Can hypnotoad do the logging independently? Again struggling to find any info on this.

Any pointers in the right direction would be gladly received.... thanks!

Replies are listed 'Best First'.
Re: request logging with Mojolicious
by haukex (Archbishop) on Oct 12, 2022 at 05:44 UTC

      Apologies for the very lengthy delay in responding. I did find a solution and neglected to update my post - which was very inconsiderate. You are right there is a plugin which does the job. I think I got fixated on persuading the default logging system to give me the desired output. The real answer as usual is to drink more coffee. Thanks for your help!

Re: request logging with Mojolicious
by alexander_lunev (Pilgrim) on Oct 12, 2022 at 17:08 UTC
    You could write your access logger like this:
    sub startup { # ... my $r_begin = $self->routes; my $r = $r_begin->under('' => sub { my $c = shift; my $params = $c->req->params->to_hash; # do your logging here }); # # your endpoints here # # catch-all route at end, your fancy 404 page and also place for log +ging $r->any('/*whatever' => {whatever => ''} => sub { my $c = shift; my $message = 'PAGE 404 '.$c->tx->{original_remote_address}." ".$c +->tx->req->url; $self->app->log->debug($message); # . Dumper($c->tx) $c->render(status => 404, text => $message); }); }

      Thanks for your help with this - and sorry for the (very) late reply. I did solve this with a plugin in the end. Your suggestion does look like a good way to do it without needing a plugin however. Thanks again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11147348]
Approved by choroba
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-24 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found