NAME
MooX::Role::HasLogger - Moo role that provides a logger attribute
SYNOPSIS
package Foo;
use Moo;
use MooX::TypeTiny;
use namespace::clean;
with qw( MooX::Role::HasLogger );
sub foo {
my ( $self ) = @_;
$self->logger->info( 'log something' );
}
1;
DESCRIPTION
This Moo::Role based role provides a logger
attribute to the consuming class. The attribute is read-only and its value is grabbed lazily. The type library MooX::Role::HasLogger::Types exports the Logger
type that defines the interface of the attribute. The build_logger()
builder method returns a Log::Any logger object with the consuming class as its category.
EXAMPLES
The first example
package Bar;
use Moo;
use MooX::TypeTiny;
use Types::Standard qw( HasMethods );
use MooX::Role::HasLogger::Types qw( Logger );
use namespace::clean;
with qw( MooX::Role::HasLogger );
has '+logger' => ( isa => ( Logger ) & ( HasMethods [ qw( tracef debugf infof warnf errorf fatalf ) ] ) );
sub build_logger {
return Log::Any->get_logger( category => ref shift, default_adapter => 'Stderr' );
}
1;
shows
how to override the
isa
option of thelogger
attribute using the+
notation.how to extend the
Logger
type using a Type::Tiny intersection. Now a logger has to be an object of a class that implements theLogger
type methods and additionally thef
suffixed log methods.how to override the
build_logger()
builder method. This time the builder returns a Log::Any logger object with the consuming class as its category and with a default adapter that refers to the Log::Any::Adapter::Stderr adapter class.
As of version 0.002
the previous HasMethods
based override of the logger
attribute can be replaced with a Type::Tiny::Duck based override
has '+logger' => (
isa => Type::Tiny::Duck->new(
name => 'ExtendedLogger',
methods => [ \@{ Logger->methods }, qw( tracef debugf infof warnf errorf fatalf ) ]
)
);
or the new LoggerAny
type can be used
use MooX::Role::HasLogger::Types qw( LoggerAny );
has '+logger' => ( isa => LoggerAny );
This new type requires that a logger object has to implement the category()
getter method too.
The second example
package Baz;
use Moo;
use MooX::TypeTiny;
use MooX::Role::HasLogger::Types qw( Logger );
use namespace::clean;
with qw( MooX::Role::HasLogger );
has '+logger' => ( handles => Logger->methods );
sub baz {
my ( $self ) = @_;
# delegation
$self->info( 'log something' );
}
1;
shows how to override the handles
option of the logger
attribute using the +
notation. The delegating class Baz
can now use the methods provided by the logger
(the delegatee) directly. If you prefer to work with an extended interface replace the Logger
type with the LoggerAny
type.
SEE ALSO
AUTHOR
Sven Willenbuecher <sven.willenbuecher@gmx.de>
COPYRIGHT AND LICENSE
This software is copyright (c) 2023 by Sven Willenbuecher.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.