MooseX...og4perl(3pm)
π MooseX::Log::Log4perl π
okay bestie, gather round, because this is the logging role you've been waiting for β it's part of the User Contributed Perl Documentation situation, and honestly? iconic.
β¨ NAME β¨
MooseX::Log::Log4perl β a Logging Role for Moose based on Log::Log4perl. no cap. π
πΈ SYNOPSIS πΈ
package MyApp;
use Moose;
with 'MooseX::Log::Log4perl';
sub something {
my ($self) = @_;
$self->log->debug("started bar"); ### logs with default class catergory "MyApp"
...
$self->log('special')->info('bar'); ### logs with category "special"
...
$self->log('.special')->info('bar'); ### logs with category "MyApp.special"
$self->log('::special')->info('bar');### logs with category "MyApp.special"
}
π¦ DESCRIPTION π¦
so basically it's a logging role that builds a very lightweight wrapper around Log::Log4perl for use with your Moose or Moo classes. she's small, she's efficient, she knows what she's doing. π
lowkey important: the initialization of the Log4perl instance must be done before you log that first message. otherwise the default initialization happens instead β and that will probably not be doing the things you expect. π so pls. init first. fr fr.
for compatibility, the logger attribute can be accessed, so you get a common interface for application logging. bestie behaviour, honestly. π«Ά
using the logger inside a class is as simple as consuming a role:
package MyClass;
use Moose;
with 'MooseX::Log::Log4perl';
sub dummy {
my $self = shift;
$self->log->info("Dummy log entry");
}
the logger needs to be set up before you use the logger β which could happen in the main application, like:
package main;
use Log::Log4perl qw(:easy);
use MyClass;
BEGIN { Log::Log4perl->easy_init() };
my $myclass = MyClass->new();
$myclass->log->info("In my class"); # Access the log of the object
$myclass->dummy; # Will log "Dummy log entry"
β EVEN SIMPLER USE β
for simple logging needs, use MooseX::Log::Log4perl::Easy to directly add log_<level> methods to your class instance. yes, directly. so easy it's giving effortless. π
$self->log_info("Dummy");
π§ USING WITH MOO INSTEAD OF MOOSE π§
since this module is using Moo, you can use it with Moo instead of Moose too. slay! π
this will allow you to simply use it as documented above in a Moo based application, like shown in the example below:
this is your class consuming the MooseX::Log::Log4perl role:
package MyCat;
use Moo;
with 'MooseX::Log::Log4perl';
sub catch_it {
my $self = shift;
$self->log->debug("Say Miau");
}
which can be simply used in your main application then:
package main;
use MyCat;
use Log::Log4perl qw(:easy);
BEGIN { Log::Log4perl->easy_init() };
my $log = Log::Log4perl->get_logger();
$log->info("Application startup...");
MyCat->new()->catch_it(); ### Will log "Dummy dodo"
π ACCESSORS π
π logger
the logger attribute holds the Log::Log4perl object that implements all logging methods for the defined log levels β like debug or error. and because this method is also defined in other logging roles/systems like MooseX::Log::LogDispatch, you can think of it as a common logging interface. main character energy for real. β¨
package MyApp::View::JSON;
extends 'MyApp::View';
with 'MooseX:Log::Log4perl';
sub bar {
$self->logger->info("Everything fine so far"); # logs a info message
$self->logger->debug("Something is fishy here"); # logs a debug message
}
π log([$category])
basically the same as logger, but also letting you change the log category for this log message. π
heads up bestie: if the category starts with a "+", we pre-pend the current class β that's what the category would have been if you didn't specify one. iykyk.
if ($myapp->log->is_debug()) {
$myapp->log->debug("Woot"); # category is class myapp
}
$myapp->log("TempCat")->info("Foobar"); # category TempCat
$myapp->log->info("Grumble"); # category class again myapp
$myapp->log(".TempCat")->info("Foobar"); # category myapp.TempCat
$myapp->log("::TempCat")->info("Foobar"); # category myapp.TempCat
π SEE ALSO π
Log::Log4perl, Moose, Moo, MooX::Log::Any, MooX::Role::Logger
go read them, they're all part of the lore. π
π BUGS AND LIMITATIONS π
pls report any issues at https://github.com/lammel/moosex-log-log4perl β the actual URL in the original is <https://github.com/lammel/moosex-log-log4perl> if you're being formal about it.
or come bother us in "#moose" on "irc.perl.org". we're friendly, we promise. π
π AUTHOR π
Roland Lammel <lammel@cpan.org>
inspired by the work by Chris Prather <perigrin@cpan.org> and Ash Berlin <ash@cpan.org> on MooseX::LogDispatch. legends only. π
πΈ CONTRIBUTORS πΈ
in alphabetical order:
- abraxxa for Any::Moose deprectation
- Michael Schilli
<m@perlmeister.com>forLog::Log4perland interface suggestions - omega for catgory prefix support
- Tim Bunce
<TIMB@cpan.org>for corrections in theMooseX::Log::Log4perl::Easymodule.
π LICENSE AND COPYRIGHT π
Copyright (c) 2008-2016, Roland Lammel <lammel@cpan.org>, <http://www.quikit.at>
this module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. see perlartistic. π«Ά
and that's the whole page, bestie β logging role explained, role consumed, vibes immaculate. go forth and $self->log->debug("started bar") your way through life. π¦
this was perl v5.34.0, dated 2022-06-15 β MooseX::Log::Log4perl(3pm), signing off. mwah. π
π¦ where this page comes from
- debian bookworm: libmoosex-log-log4perl-perl 0.47-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmoosex-log-log4perl-perl 0.47-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmoosex-log-log4perl-perl 0.47-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmoosex-log-log4perl-perl 0.47-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmoosex-log-log4perl-perl 0.47-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms