Modernize LC usage
Arthur Axel 'fREW' Schmidt [Fri, 23 Aug 2013 23:36:23 +0000 (18:36 -0500)]
Changes
dist.ini
lib/DBIx/Class/DeploymentHandler/LogImporter.pm
lib/DBIx/Class/DeploymentHandler/LogRouter.pm [new file with mode: 0644]
lib/DBIx/Class/DeploymentHandler/Logger.pm

diff --git a/Changes b/Changes
index a228633..501acaa 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,8 @@
 Revision history for {{$dist->name}}
 
 {{$NEXT}}
+  - Modernize usage of Log::Contextual, creating a simple example for how to
+    make static loggers for modules and applications
 
 0.002206  2013-03-09 12:50:57 CST6CDT
   - Fix embarrassing broken charity link
index 816d7a1..1af1b15 100644 (file)
--- a/dist.ini
+++ b/dist.ini
@@ -24,10 +24,11 @@ bugtracker.web    = https://github.com/frioux/DBIx-Class-DeploymentHandler/issue
 parent                      = 0.225
 autodie                     = 0
 namespace::autoclean        = 0
-Log::Contextual             = 0.004200
+Log::Contextual             = 0.005005
 Path::Class                 = 0.26
 DBIx::Class                 = 0.08121
 Moose                       = 1.0
+Moo                         = 1.003000
 MooseX::Role::Parameterized = 0.18
 Try::Tiny                   = 0
 SQL::Translator             = 0.11005
index a4c0b46..a17aa35 100644 (file)
@@ -5,12 +5,11 @@ use strict;
 
 use parent 'Log::Contextual';
 
-use DBIx::Class::DeploymentHandler::Logger;
+use DBIx::Class::DeploymentHandler::LogRouter;
 
-my $logger = DBIx::Class::DeploymentHandler::Logger->new({
-   env_prefix => 'DBICDH'
-});
-
-sub arg_package_logger { $_[1] || $logger }
+{
+   my $router;
+   sub router { $router ||= DBIx::Class::DeploymentHandler::LogRouter->new }
+}
 
 1;
diff --git a/lib/DBIx/Class/DeploymentHandler/LogRouter.pm b/lib/DBIx/Class/DeploymentHandler/LogRouter.pm
new file mode 100644 (file)
index 0000000..b58c9b8
--- /dev/null
@@ -0,0 +1,31 @@
+package DBIx::Class::DeploymentHandler::LogRouter;
+use Moo;
+use DBIx::Class::DeploymentHandler::Logger;
+
+with 'Log::Contextual::Role::Router';
+
+has _logger => (
+   is      => 'lazy',
+   builder => sub { DBIx::Class::DeploymentHandler::Logger->new },
+);
+
+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);
+
+   $self->_logger->$log_level_name($log_message);
+}
+
+sub before_import {}
+sub after_import {}
+
+1;
index 565a898..1aadb82 100644 (file)
@@ -37,4 +37,13 @@ sub _log {
   warn "[DBICDH] [$level] $message";
 }
 
+sub new {
+   my ($self, $options, @rest) = @_;
+
+   $options ||= {};
+   $options->{env_prefix} ||= 'DBICDH';
+
+   $self->next::method($options, @rest)
+}
+
 1;