Catmandu::Logger(3pm)

πŸ“¦
version 1.2025 Β· 4 versions
, in ubuntu resolute, ubuntu stonking

πŸ’– Catmandu::Logger β€” the Perl doc you didn't know you needed πŸ’–

okay bestie, so this is the Catmandu::Logger(3pm) page from the User Contributed Perl Documentation, and it's literally just a little role for classes that need logging capabilities. she's small. she's powerful. she's giving main character energy. let's get into it 🫢


✨ NAME ✨

Catmandu::Logger β€” a role for classes that need logging capabilities. that's it, that's the whole name section, she's concise πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

here's the vibe, no cap:

package MyApp::View;
use Moo;

with 'Catmandu::Logger';

sub something {
    my ($self) = @_;
    $self->log->debug("started bar"); # logs with default class catergory "MyApp::View"
    $self->log->error("started bar");
}

yes the word "catergory" is spelled like that in the original and yes i'm keeping it exactly as-is, bestie, we do not touch the sacred typos πŸ™ˆ


🌸 DESCRIPTION 🌸

so basically? this is a logging role that builds a very lightweight wrapper around Log::Any. she's not doing too much, she's just vibing as a thin lil layer.

important!! connecting a Log::Any::Adapter has to happen before you log your first log message. otherwise nothing will happen β€” like, nothing at all, just silence, which is exactly how Log::Any behaves too. so do it first, bestie, or your logs are gonna ghost you 😭

the logger needs to be setup before using the logger. that could happen in the main application, like this:

package main;
use Log::Any::Adapter;
use Log::Log4perl;

Log::Any::Adapter->set('Log4perl');
Log::Log4perl::init('./log4perl.conf');

my $app = MyApp::View->new;
$app->something();  # will print debug and error messages

with log4perl.conf looking like:

log4perl.rootLogger=DEBUG,OUT
log4perl.appender.OUT=Log::Log4perl::Appender::Screen
log4perl.appender.OUT.stderr=1
log4perl.appender.OUT.utf8=1

log4perl.appender.OUT.layout=PatternLayout
log4perl.appender.OUT.layout.ConversionPattern=%d [%P] - %p %l time=%r : %m%n

see Log::Log4perl for more configuration options and for picking which messages get logged and which ones don't. slay ✨


πŸ‘‘ CATMANDU COMMAND LINE πŸ‘‘

when you're using the catmandu command line, you can activate the logger with the -D option β€” and yes, that works on all Catmandu commands. iconic behaviour honestly.

$ catmandu -D convert JSON to YAML < data.json
$ catmandu -D export MongoDB --database-name items --bag

the log4perl configuration for the "catmandu" command line must be defined in a 'catmandu.yml' configuration file:

$ cat catmandu.yml
log4perl: |
  log4perl.rootLogger=DEBUG,OUT
  log4perl.appender.OUT=Log::Log4perl::Appender::Screen
  log4perl.appender.OUT.stderr=1
  log4perl.appender.OUT.utf8=1

  log4perl.appender.OUT.layout=PatternLayout
  log4perl.appender.OUT.layout.ConversionPattern=%d [%P] - %p %l time=%r : %m%n

the "log4perl" section can point to either an inline log4perl configuration or a filename containing the configuration. your choice, bestie, both are valid πŸŽ€

also! see Catmandu::Fix::log for how to include log messages in the Catmandu::Fix language. iykyk πŸ’œ


πŸŽ€ ACCESSORS πŸŽ€

πŸ“ log

the "log" attribute holds the Log::Any::Adapter object that implements all the logging methods for the defined log levels β€” things like "debug" or "error".

package MyApp::View::JSON;

extends 'MyApp::View';
with 'Catmandu::Logger';

sub bar {
    $self->log->info("Everything fine so far");   # logs a info message
    $self->log->debug("Something is fishy here"); # logs a debug message
}

your package automatically has a logging category of MyApp::View::JSON. and you can use lines like:

log4perl.logger.MyApp::View::JSON=DEBUG,STDOUT

or

log4perl.logger.MyApp::View=DEBUG,STDOUT

or

log4perl.logger.MyApp=DEBUG,STDOUT

for specialized logging for your application. pick your level of specificity, queen πŸ‘‘


🧚 log_category

default is the class name. that's the whole thing. short and sweet, she said what she said πŸ’…


🌈 SEE ALSO 🌈

  • Log::Any

and that's the page, bestie!! 🫢 this whole thing comes from perl v5.40.1, dated 2026-01-24, and it's the Catmandu::Logger(3pm) page from the User Contributed Perl Documentation. go forth and log responsibly, mwah πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from