MooseX...og4perl(3pm)

πŸ“¦
version 0.47 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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> for Log::Log4perl and interface suggestions
  • omega for catgory prefix support
  • Tim Bunce <TIMB@cpan.org> for corrections in the MooseX::Log::Log4perl::Easy module.

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