From e8d5e2a57792860756e152b2bc167a057d3c34ac Mon Sep 17 00:00:00 2001 From: hylom Date: Sun, 25 Sep 2016 22:53:49 +0900 Subject: [PATCH] Model::Base: implement logging method --- dev/newslash_web/lib/Newslash/Model/Base.pm | 16 ++++++++++++++++ dev/newslash_web/lib/Newslash/Model/Comments.pm | 8 ++++---- dev/newslash_web/lib/Newslash/Web.pm | 5 +++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/dev/newslash_web/lib/Newslash/Model/Base.pm b/dev/newslash_web/lib/Newslash/Model/Base.pm index d0d894cb..b9aed951 100644 --- a/dev/newslash_web/lib/Newslash/Model/Base.pm +++ b/dev/newslash_web/lib/Newslash/Model/Base.pm @@ -56,6 +56,22 @@ sub connect_db { } +sub logger { + my $self = shift; + if ($self->{options}->{logger}) { + return $self->{options}->{logger}; + } + # return dummy object + return { + append => sub {}, + debug => sub {}, + error => sub {}, + fatal => sub {}, + info => sub {}, + is_level => sub {}, + warn => sub {}, + }; +} 1; diff --git a/dev/newslash_web/lib/Newslash/Model/Comments.pm b/dev/newslash_web/lib/Newslash/Model/Comments.pm index 9013c2f4..0e195c42 100644 --- a/dev/newslash_web/lib/Newslash/Model/Comments.pm +++ b/dev/newslash_web/lib/Newslash/Model/Comments.pm @@ -72,11 +72,13 @@ EOSQL $dbh->disconnect(); return undef; } + + my $root = $self->build_comment_tree($comments); return $comments; } sub build_comment_tree { - my ($self, $comments, $logger) = @_; + my ($self, $comments) = @_; my $root = []; my $comment_of = {}; @@ -96,9 +98,7 @@ sub build_comment_tree { if ($parent) { push @{$parent->{children}}, $comment; } else { - if ($logger) { - $logger->warn("(Controller::Story) invalid comment order. cid: $cid, pid: $pid"); - } + warn("(Controller::Story) invalid comment order. cid: $cid, pid: $pid"); } } return $root; diff --git a/dev/newslash_web/lib/Newslash/Web.pm b/dev/newslash_web/lib/Newslash/Web.pm index 4efa13f0..67f22855 100644 --- a/dev/newslash_web/lib/Newslash/Web.pm +++ b/dev/newslash_web/lib/Newslash/Web.pm @@ -25,8 +25,9 @@ sub startup { $app->secrets([$app->config->{System}->{secret_key},]); # add Model - my $db_opts = $app->config->{Database}; - $app->helper(model => Newslash::Model->loader($db_opts)); + my $model_opts = $app->config->{Database}; + $model_opts->{logger} = $app->log; + $app->helper(model => Newslash::Model->loader($model_opts)); # use Template::Toolkit 2 render $app->plugin('Newslash::Plugin::TT2Renderer'); -- 2.11.0