How to control logging for Mojolicious web server

I want to change the default logging level for the Mojolicious web server but I just cannot figure out how to do it after reading the docs. I am a fairly new Perl developer as I had to use Perl for a specific legacy use case - I typically use Go, Python, Java in day-to-day work. I figure there is some "Perl way" of doing this that I just cannot figure out due to my lack of experience.

Here's a trivial sample app:

#!/usr/bin/env perl

use strict;
use warnings;
use utf8;

use Mojolicious::Lite -signatures;

get '/ping' => sub ($c) {
    $c->render( json => { 'pong' => $c->param('value'), 'number' => 1 } );
};

app->start( 'daemon', '-l', 'http://localhost:8888' );

I run that in a terminal and then, from another terminal I hit the endpoint and get a response:

curl "http://localhost:8888/ping?value=test"
{"number":1,"pong":"test"}

However, in the web server terminal, I get this trace level logging which I understand is the default logging level for Mojolicious:

[2022-01-21 14:38:04.40413] [1628] [info] Listening at "http://localhost:8888"
Web application available at http://localhost:8888
[2022-01-21 14:38:39.06480] [1628] [trace] [sLAkASlGAvjE] GET "/ping"
[2022-01-21 14:38:39.06516] [1628] [trace] [sLAkASlGAvjE] Routing to a callback
[2022-01-21 14:38:39.06552] [1628] [trace] [sLAkASlGAvjE] 200 OK (0.000718s, 1392.758/s)

I want to back off the logging level to 'info'.


Solution 1:

You can set the log level like this:

app->log->level('error');

This example is from the main Mojolicious::Lite documentation in the app section. This was the only hit for grepping log in that document.

In your example, you could do that either at the start, or the end of the application.

# ...
use Mojolicious::Lite -signatures;

app->log->level('info');

get '/ping' => sub ($c) {
    $c->render( json => { 'pong' => $c->param('value'), 'number' => 1 } );
};

app->start( 'daemon', '-l', 'http://localhost:8888' );