1 package Log::Contextual::Role::Router;
5 requires 'before_import';
6 requires 'after_import';
7 requires 'handle_log_request';
15 Log::Contextual::Role::Router - Abstract interface between loggers and logging code blocks
19 package Custom::Logging::Router;
22 use Log::Contextual::SimpleLogger;
24 with 'Log::Contextual::Role::Router';
26 has logger => (is => 'lazy');
29 return Log::Contextual::SimpleLogger->new({ levels_upto => 'debug' });
33 my ($self, $log_class, $importer, $spec) = @_;
34 print STDERR "Package '$importer' will import '$log_class'\n";
38 my ($self, $log_class, $importer, $spec) = @_;
39 print STDERR "Package '$importer' has imported '$log_class'\n";
42 sub handle_log_request {
43 my ($self, $metadata, $log_code_block, @args) = @_;
44 my $log_level_name = $metadata->{level};
45 my $logger = $self->logger;
46 my $is_active = $logger->can("is_$log_level_name");
48 return unless defined $is_active && $logger->$is_active;
49 my $log_message = $log_code_block->(@args);
50 $logger->$log_level_name($log_message);
53 package Custom::Logging::Class;
57 extends 'Log::Contextual';
59 #Almost certainly the router object should be a singleton
61 our $Router ||= Custom::Logging::Router->new
67 use Custom::Logging::Class qw(:log);
69 log_info { "Hello there" };
73 Log::Contextual has three parts
77 =item Export manager and logging method generator
79 These tasks are handled by the C<Log::Contextual> class.
81 =item Logger selection and invocation
83 The log methods generated and exported by Log::Contextual call a method
84 on a log router object which is responsible for invoking any loggers that should
85 get an opportunity to receive the log message. The C<Log::Contextual::Router>
86 class implements the set_logger() and with_logger() methods as well as uses the
87 arg_ prefixed methods to configure itself and provide the standard C<Log::Contextual>
90 =item Log message formatting and output
92 The logger objects themselves accept or reject a log message at a certain log
93 level with a guard method per level. If the logger is going to accept the
94 log message the router is then responsible for executing the log message code
95 block and passing the generated message to the logging object's log method.
103 =item before_import($self, $log_class, $importer, $spec)
105 =item after_import($self, $log_class, $importer, $spec)
107 These two required methods are called with identical arguments at two different places
108 during the import process. The before_import() method is invoked prior to the logging
109 methods being exported into the consuming packages namespace. The after_import() method
110 is called when the export is completed but before control returns to the package that
113 The arguments are as follows:
119 This is the package name of the subclass of Log::Contextual that has been imported. It can
120 also be 'Log::Contextual' itself. In the case of the synopsis the value in $log_class would be
121 'Custom::Logging::Class'.
125 This is the package name that is importing the logging class. In the case of the synopsis the
126 value would be 'main'.
130 This is the import specification that is being used when exporting methods to $importer. The
131 value is an unmodified C<Exporter::Declare::Specs> object.
135 =item handle_log_request($self, $info, $generator, @args)
137 This method is called by C<Log::Contextual> when a log event happens. The arguments are as
144 This is the metadata describing the log event. The value is a hash reference with the following
151 This is the name of the Log::Contextual subclass (or 'Log::Contextual' itself) that created
152 the logging methods used to generate the log event.
156 This is the name of the package that the log event has happened inside of.
160 This is an integer that contains the value to pass to caller() that will provide
161 information about the location the log event was created at.
165 This is the name of the log level associated with the log event.
171 This is the message generating block associated with the log event passed as a subref. If
172 the logger accepts the log request the router should execute the generator to create
173 the log message and then pass the message as a string to the logger.
177 This is the arguments provided to the log block passed through completely unmodified. When
178 invoking the generator method it will almost certainly be expecting these argument values
189 =item C<Log::Contextual>