1 package Log::Contextual::Role::Router;
3 # ABSTRACT: Abstract interface between loggers and logging code blocks
7 requires 'before_import';
8 requires 'after_import';
9 requires 'handle_log_request';
17 package MyApp::Log::Router;
20 use Log::Contextual::SimpleLogger;
22 with 'Log::Contextual::Role::Router';
24 has logger => (is => 'lazy');
27 return Log::Contextual::SimpleLogger->new({ levels_upto => 'debug' });
31 my ($self, %export_info) = @_;
32 my $exporter = $export_info{exporter};
33 my $target = $export_info{target};
34 print STDERR "Package '$target' will import from '$exporter'\n";
38 my ($self, %export_info) = @_;
39 my $exporter = $export_info{exporter};
40 my $target = $export_info{target};
41 print STDERR "Package '$target' has imported from '$exporter'\n";
44 sub handle_log_request {
45 my ($self, %message_info) = @_;
46 my $log_code_block = $message_info{message_sub};
47 my $args = $message_info{message_args};
48 my $log_level_name = $message_info{message_level};
49 my $logger = $self->logger;
50 my $is_active = $logger->can("is_${log_level_name}");
52 return unless defined $is_active && $logger->$is_active;
53 my $log_message = $log_code_block->(@$args);
54 $logger->$log_level_name($log_message);
57 package MyApp::Log::Contextual;
60 use MyApp::Log::Router;
62 extends 'Log::Contextual';
64 #This example router is a singleton
66 our $Router ||= MyApp::Log::Router->new
73 use MyApp::Log::Contextual qw(:log);
75 log_info { "Hello there" };
79 Log::Contextual has three parts
83 =item Export manager and logging method generator
85 These tasks are handled by the C<Log::Contextual> package.
87 =item Logger selection and invocation
89 The logging functions generated and exported by Log::Contextual call a method
90 on an instance of a log router object which is responsible for invoking any loggers
91 that should get an opportunity to receive the log message. The C<Log::Contextual::Router>
92 class implements the set_logger() and with_logger() functions as well as uses the
93 arg_ prefixed functions to configure itself and provide the standard C<Log::Contextual>
96 =item Log message formatting and output
98 The logger objects themselves accept or reject a log message at a certain log
99 level with a guard method per level. If the logger is going to accept the
100 log message the router is then responsible for executing the log message code
101 block and passing the generated message to the logging object's log method.
109 =item before_import($self, %import_info)
111 =item after_import($self, %import_info)
113 These two required methods are called with identical arguments at two different places
114 during the import process. The before_import() method is invoked prior to the logging
115 subroutines being exported into the target package and after_import() is called when the
116 export is completed but before control returns to the package that imported the API.
118 The arguments are passed as a hash with the following keys:
124 This is the name of the package that has been imported. It can also be 'Log::Contextual' itself. In
125 the case of the synopsis the value for exporter would be 'MyApp::Log::Contextual'.
129 This is the package name that is importing the logging API. In the case of the synopsis the
130 value would be 'main'.
134 This is a hash reference containing the configuration values that were provided for the import.
135 The key is the name of the configuration item that was specified without the leading hyphen ('-').
136 For instance if the logging API is imported as follows
138 use Log::Contextual qw( :log ), -logger => Custom::Logger->new({ levels => [qw( debug )] });
140 then $import_info{arguments}->{logger} would contain that instance of Custom::Logger.
144 =item handle_log_request($self, %message_info)
146 This method is called by C<Log::Contextual> when a log event happens. The arguments are passed
147 as a hash with the following keys
153 This is the name of the package that created the logging methods used to generate the log event.
157 This is the name of the package that the log event has happened inside of.
161 This is an integer that contains the value to pass to caller() that will provide
162 information about the location the log event was created at.
166 This is the name of the log level associated with the log event.
170 This is the message generating code block associated with the log event passed as a subref. If
171 the logger accepts the log request the router should execute the subref to create
172 the log message and then pass the message as a string to the logger.
176 This is an array reference that contains the arguments given to the message generating code block.
177 When invoking the message generator it will almost certainly be expecting these argument values