MooX::Role::HasLogger - Moo role that provides a logger attribute
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;
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.
logger
Logger
build_logger()
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 the logger attribute using the + notation.
isa
+
how to extend the Logger type using a Type::Tiny intersection. Now a logger has to be an object of a class that implements the Logger type methods and additionally the f suffixed log methods.
f
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
0.002
HasMethods
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
LoggerAny
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.
category()
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.
handles
Baz
MooX::Role::Logger
MooX::Log::Any
Sven Willenbuecher <sven.willenbuecher@gmx.de>
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.
To install MooX::Role::HasLogger, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooX::Role::HasLogger
CPAN shell
perl -MCPAN -e shell install MooX::Role::HasLogger
For more information on module installation, please visit the detailed CPAN module installation guide.