NAME

Object::PadX::Log::Log4perl - A logger role for Object::Pad based classes based on Log::Log4perl

VERSION

version 0.002

SYNOPSIS

package MyClass;
use v5.26;
use Object::Pad;

class MyClass :does(Object::PadX::Log::Log4perl)

method foo {
    $self->log->info("Foo called");
}

DESCRIPTION

A logging role building a very lightweight wrapper to Log::Log4perl for use with your Object::Pad classes. The initialization of the Log4perl instance must be performed prior to logging the first log message. Otherwise the default initialization will happen, probably not doing the things you expect.

The logger needs to be setup before using the logger, which could happen in the main application:

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"

Using the logger within a class is as simple as consuming a role:

METHODS

logger

The logger attribute holds the Log::Log4perl object that implements all logging methods for the defined log levels, such as debug or error.

log

Basically the same as logger, but also allowing to change the log category for this log message.

if ($myapp->log->is_debug()) {
  $myapp->log->debug("Woot");            # category is class myapp
}

$myapp->log("FooBar")->info("Foobar");   # category FooBar
$myapp->log->info("Yihaa");              # category class again myapp
$myapp->log(".FooBar")->info("Foobar");  # category myapp.FooBar
$myapp->log("::FooBar")->info("Foobar"); # category myapp.FooBar

PRIOR ART

This code has been mostly ported/inspired from MooseX::Log::Log4perl. Copyright (c) 2008-2016, Roland Lammel <lammel@cpan.org>, http://www.quikit.at

AUTHOR

Wesley Schwengle <waterkip@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Wesley Schwengle.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.