From: Arthur Axel 'fREW' Schmidt Date: Fri, 23 Aug 2013 23:36:23 +0000 (-0500) Subject: Modernize LC usage X-Git-Tag: v0.002207~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=374a05c8dd5c974fe841569aa34eeabd7b072dd6;p=dbsrgits%2FDBIx-Class-DeploymentHandler.git Modernize LC usage --- diff --git a/Changes b/Changes index a228633..501acaa 100644 --- 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 diff --git a/dist.ini b/dist.ini index 816d7a1..1af1b15 100644 --- 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 diff --git a/lib/DBIx/Class/DeploymentHandler/LogImporter.pm b/lib/DBIx/Class/DeploymentHandler/LogImporter.pm index a4c0b46..a17aa35 100644 --- a/lib/DBIx/Class/DeploymentHandler/LogImporter.pm +++ b/lib/DBIx/Class/DeploymentHandler/LogImporter.pm @@ -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 index 0000000..b58c9b8 --- /dev/null +++ b/lib/DBIx/Class/DeploymentHandler/LogRouter.pm @@ -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; diff --git a/lib/DBIx/Class/DeploymentHandler/Logger.pm b/lib/DBIx/Class/DeploymentHandler/Logger.pm index 565a898..1aadb82 100644 --- a/lib/DBIx/Class/DeploymentHandler/Logger.pm +++ b/lib/DBIx/Class/DeploymentHandler/Logger.pm @@ -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;