Log::Co...::Router(3)
πΈ NAME πΈ
Log::Contextual::Role::Router β Abstract interface between loggers and logging code blocks.
This is the User Contributed Perl Documentation page for Log::Contextual::Role::Router(3), bestie. Itβs giving βthe router role that sits between your logging code and the actual loggerβ energy, and weβre gonna get into all of it. π
β¨ VERSION β¨
version 0.009001
π¦ SYNOPSIS π¦
Okay so hereβs the whole example, copied exactly because code is sacred:
package MyApp::Log::Router;
use Moo;
use Log::Contextual::SimpleLogger;
with 'Log::Contextual::Role::Router';
has logger => (is => 'lazy');
sub _build_logger {
return Log::Contextual::SimpleLogger->new({ levels_upto => 'debug' });
}
sub before_import {
my ($self, %export_info) = @_;
my $exporter = $export_info{exporter};
my $target = $export_info{target};
print STDERR "Package '$target' will import from '$exporter'\n";
}
sub after_import {
my ($self, %export_info) = @_;
my $exporter = $export_info{exporter};
my $target = $export_info{target};
print STDERR "Package '$target' has imported from '$exporter'\n";
}
sub handle_log_request {
my ($self, %message_info) = @_;
my $log_code_block = $message_info{message_sub};
my $args = $message_info{message_args};
my $log_level_name = $message_info{message_level};
my $logger = $self->logger;
my $is_active = $logger->can("is_${log_level_name}");
return unless defined $is_active && $logger->$is_active;
my $log_message = $log_code_block->(@$args);
$logger->$log_level_name($log_message);
}
package MyApp::Log::Contextual;
use Moo;
use MyApp::Log::Router;
extends 'Log::Contextual';
#This example router is a singleton
sub router {
our $Router ||= MyApp::Log::Router->new
}
package main;
use strict;
use warnings;
use MyApp::Log::Contextual qw(:log);
log_info { "Hello there" };
π DESCRIPTION π
Log::Contextual has three parts, and yes, weβre listing all three because every part matters. π«Ά
- Export manager and logging method generator β These tasks are handled by the
Log::Contextualpackage. - Logger selection and invocation β The logging functions generated and exported by
Log::Contextualcall a method on an instance of a log router object which is responsible for invoking any loggers that should get an opportunity to receive the log message. TheLog::Contextual::Routerclass implements theset_logger()andwith_logger()functions as well as uses the arg_ prefixed functions to configure itself and provide the standardLog::Contextuallogger selection API. - Log message formatting and output β The logger objects themselves accept or reject a log message at a certain log level with a guard method per level. If the logger is going to accept the log message the router is then responsible for executing the log message code block and passing the generated message to the logging object's log method.
π METHODS π
before_import($self, %import_info) / after_import($self, %import_info)
These two required methods are called with identical arguments at two different places during the import process. The before_import() method is invoked prior to the logging subroutines being exported into the target package and after_import() is called when the export is completed but before control returns to the package that imported the API.
The arguments are passed as a hash with the following keys:
exporterβ This is the name of the package that has been imported. It can also be'Log::Contextual'itself. In the case of the synopsis the value for exporter would be'MyApp::Log::Contextual'.targetβ This is the package name that is importing the logging API. In the case of the synopsis the value would be'main'.argumentsβ This is a hash reference containing the configuration values that were provided for the import. The key is the name of the configuration item that was specified without the leading hyphen ('-'). For instance if the logging API is imported as follows
use Log::Contextual qw( :log ), -logger => Custom::Logger->new({ levels => [qw( debug )] });
then $import_info{arguments}->{logger} would contain that instance of Custom::Logger.
handle_log_request($self, %message_info)
This method is called by Log::Contextual when a log event happens. The arguments are passed as a hash with the following keys:
exporterβ This is the name of the package that created the logging methods used to generate the log event.caller_packageβ This is the name of the package that the log event has happened inside of.caller_levelβ This is an integer that contains the value to pass tocaller()that will provide information about the location the log event was created at.log_levelβ This is the name of the log level associated with the log event.message_subβ This is the message generating code block associated with the log event passed as a code reference. If the logger accepts the log request the router should execute the code reference to create the log message and then pass the message as a string to the logger.message_argsβ This is an array reference that contains the arguments given to the message generating code block. When invoking the message generator it will almost certainly be expecting these argument values as well.
π BUGS π
Please report any bugs or feature requests on the bugtracker website https://github.com/haarg/Log-Contextual/issues
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
π AUTHOR π
Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2024 by Arthur Axel "fREW" Schmidt.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
xoxo, your Perl bestie π β cute little footer moment: Log::Co...::Router(3), perl v5.42.0, dated 2024-06-23, module version 0.009001. Periodt. β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Log-Contextual 0.009001-bp160.1.11 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms