Bugzilla – Attachment 85713 Details for
Bug 18205
Use Koha::Logger/Log4Perl using Mojolicious app log method
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 18205: Mojo::Log with Koha::Logger::Mojo
Bug-18205-MojoLog-with-KohaLoggerMojo.patch (text/plain), 8.10 KB, created by
Josef Moravec
on 2019-02-26 12:16:42 UTC
(
hide
)
Description:
Bug 18205: Mojo::Log with Koha::Logger::Mojo
Filename:
MIME Type:
Creator:
Josef Moravec
Created:
2019-02-26 12:16:42 UTC
Size:
8.10 KB
patch
obsolete
>From b79f2ab5684226a5773c3d3ae2c638e7d36a95fa Mon Sep 17 00:00:00 2001 >From: Lari Taskula <lari.taskula@jns.fi> >Date: Mon, 6 Mar 2017 19:05:10 +0200 >Subject: [PATCH] Bug 18205: Mojo::Log with Koha::Logger::Mojo > >This patch adds a new module, Koha::Logger::Mojo, based on MojoX::Log::Log4perl. >This module allows us to use log4perl configurations with Mojolicious on the >freshly introduced 'rest' interface in log4perl.conf. > >$c->app->log->warn will then use Log4perl as the underlying log mechanism and >those log events will point to the appropriate log file. > >To test: >1. Install Perl dependency MojoX::Log::Log4perl >2. prove t/Koha/Logger/Mojo.t > >Signed-off-by: Josef Moravec <josef.moravec@gmail.com> >--- > C4/Installer/PerlDependencies.pm | 5 ++ > Koha/Logger/Mojo.pm | 102 +++++++++++++++++++++++++++++++++++++++ > Koha/REST/V1.pm | 3 ++ > t/Koha/Logger/Mojo.t | 85 ++++++++++++++++++++++++++++++++ > 4 files changed, 195 insertions(+) > create mode 100644 Koha/Logger/Mojo.pm > create mode 100644 t/Koha/Logger/Mojo.t > >diff --git a/C4/Installer/PerlDependencies.pm b/C4/Installer/PerlDependencies.pm >index fc5e99e0ca..977df02e0d 100644 >--- a/C4/Installer/PerlDependencies.pm >+++ b/C4/Installer/PerlDependencies.pm >@@ -777,6 +777,11 @@ our $PERL_DEPS = { > 'required' => '1', > 'min_ver' => '0.97', > }, >+ 'MojoX::Log::Log4perl' => { >+ 'usage' => 'Logs', >+ 'required' => '0', >+ 'min_ver' => '0.11', >+ }, > 'UNIVERSAL::can' => { > 'usage' => 'SIP', > 'required' => '1', >diff --git a/Koha/Logger/Mojo.pm b/Koha/Logger/Mojo.pm >new file mode 100644 >index 0000000000..e967e8310c >--- /dev/null >+++ b/Koha/Logger/Mojo.pm >@@ -0,0 +1,102 @@ >+package Koha::Logger::Mojo; >+ >+# Copyright 2017 Koha-Suomi Oy >+# >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it under the >+# terms of the GNU General Public License as published by the Free Software >+# Foundation; either version 3 of the License, or (at your option) any later >+# version. >+# >+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY >+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR >+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License along >+# with Koha; if not, write to the Free Software Foundation, Inc., >+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. >+ >+=head1 NAME >+ >+Koha::Logger::Mojo >+ >+=head1 SYNOPSIS >+ >+ use Koha::Logger::Mojo; >+ >+ $c->app->log(Koha::Logger::Mojo->get); >+ $c->app->log->warn( 'WARNING: Serious error encountered' ); >+ >+ EXAMPLE CONFIGS: >+ log4perl.logger.rest = ERROR, REST >+ log4perl.appender.REST=Log::Log4perl::Appender::File >+ log4perl.appender.REST.filename=__LOG_DIR__/rest.log >+ log4perl.appender.REST.create_at_logtime=true >+ log4perl.appender.REST.syswrite=true >+ log4perl.appender.REST.recreate=true >+ log4perl.appender.REST.mode=append >+ log4perl.appender.REST.layout=PatternLayout >+ log4perl.appender.REST.layout.ConversionPattern=[%d] [%p] %m %l %n >+ >+ log4perl.logger.rest.Mojolicious.Plugin.OpenAPI = WARN, REST >+ log4perl.appender.REST=Log::Log4perl::Appender::File >+ log4perl.appender.REST.filename=__LOG_DIR__/rest.log >+ log4perl.appender.REST.create_at_logtime=true >+ log4perl.appender.REST.syswrite=true >+ log4perl.appender.REST.recreate=true >+ log4perl.appender.REST.mode=append >+ log4perl.appender.REST.layout=PatternLayout >+ log4perl.appender.REST.layout.ConversionPattern=[%d] [%p] %m %l %n >+ >+=head1 DESCRIPTION >+ >+ Use Log4perl on Mojolicious with the help of MojoX::Log::Log4perl. >+=cut >+ >+use Modern::Perl; >+ >+use base 'MojoX::Log::Log4perl'; >+ >+sub get { >+ my ($class, $params) = @_; >+ >+ my $self; >+ if ($ENV{'LOG4PERL_CONF'} and -s $ENV{"LOG4PERL_CONF"}) { >+ $self = MojoX::Log::Log4perl->new($ENV{"LOG4PERL_CONF"}); >+ } elsif (C4::Context->config("log4perl_conf")) { >+ $self = MojoX::Log::Log4perl->new(C4::Context->config("log4perl_conf")); >+ } else { >+ $self = MojoX::Log::Log4perl->new; >+ } >+ my $interface = $params ? $params->{interface} >+ ? $params->{interface} : C4::Context->interface >+ : C4::Context->interface; >+ $self->{interface} = $interface; >+ $self->{category} = $params->{category}; >+ >+ bless $self, $class; >+ return $self; >+} >+ >+=head3 _get_logger >+ >+Overloads MojoX::Log::Log4perl::_get_logger by optionally including interface >+and category. >+ >+For REST API, 'rest' should be the root category by default (defined in startup) >+ >+=cut >+ >+sub _get_logger { >+ my ($self, $depth) = @_; >+ >+ my $category = $self->{category} ? $self->{category} >+ : scalar caller( $depth || 1 ); >+ my $l4pcat = $self->{interface} ? $self->{interface}.'.' : ''; >+ $l4pcat .= $category if $category; >+ >+ return Log::Log4perl->get_logger($l4pcat); >+} >+ >+1; >diff --git a/Koha/REST/V1.pm b/Koha/REST/V1.pm >index fee6a7c967..acb4e6891f 100644 >--- a/Koha/REST/V1.pm >+++ b/Koha/REST/V1.pm >@@ -19,6 +19,7 @@ use Mojo::Base 'Mojolicious'; > > use C4::Context; > use JSON::Validator::OpenAPI::Mojolicious; >+use Koha::Logger::Mojo; > > =head1 NAME > >@@ -39,6 +40,8 @@ sub startup { > > C4::Context->interface('rest'); > >+ $self->log(Koha::Logger::Mojo->get); >+ > # Remove /api/v1/app.pl/ from the path > $self->hook( before_dispatch => sub { > shift->req->url->base->path('/'); >diff --git a/t/Koha/Logger/Mojo.t b/t/Koha/Logger/Mojo.t >new file mode 100644 >index 0000000000..0bdebca71b >--- /dev/null >+++ b/t/Koha/Logger/Mojo.t >@@ -0,0 +1,85 @@ >+#!/usr/bin/perl >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+use Mojo::Base -strict; >+use Mojolicious::Lite; >+use Log::Log4perl; >+ >+use Test::Mojo; >+use Test::More tests => 4; >+use t::lib::Mocks; >+ >+use C4::Context; >+ >+BEGIN { >+ $ENV{MOJO_MODE} = 'development'; >+ $ENV{MOJO_NO_IPV6} = 1; >+ $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll'; >+ $ENV{'LOG4PERL_CONF'} = undef; >+} >+ >+use_ok('Koha::Logger::Mojo'); >+ >+# Create test context >+my $config = { >+ 'log4perl.logger.rest' => 'ERROR, TEST', >+ 'log4perl.appender.TEST' => 'Log::Log4perl::Appender::TestBuffer', >+ 'log4perl.appender.TEST.layout' => 'SimpleLayout', >+}; >+C4::Context->interface('rest'); >+t::lib::Mocks::mock_config('log4perl_conf', $config); >+ >+subtest 'check inteface' => sub { >+ plan tests => 1; >+ >+ is(C4::Context->interface, 'rest', 'Using appropriate interface.'); >+}; >+ >+subtest 'get() tests' => sub { >+ plan tests => 1; >+ >+ app->log(Koha::Logger::Mojo->get); >+ my $log = app->log; >+ isa_ok($log, 'Koha::Logger::Mojo'); >+}; >+ >+subtest 'Test configuration effectiveness' => sub { >+ plan tests => 6; >+ >+ get '/test' => sub { >+ package Koha::REST::V1::Test; # otherwise category would be rest.main >+ $_[0]->app->log->error('Very problem'); >+ $_[0]->app->log->warn('Such issue'); >+ $_[0]->render(text => 'Hello World'); >+ }; >+ >+ app->log(Koha::Logger::Mojo->get); >+ >+ my $appender = Log::Log4perl->appenders->{TEST}; >+ ok($appender, 'Got TEST appender'); >+ >+ my $t = Test::Mojo->new; >+ $t->app->log(Koha::Logger::Mojo->get); >+ $t->get_ok('/test') >+ ->status_is(200) >+ ->content_is('Hello World'); >+ like($appender->buffer, qr/ERROR - Very problem/, 'found wanted message'); >+ unlike($appender->buffer, qr/WARN - Such issue/, 'did not find unwanted message'); >+}; >+ >+1; >-- >2.11.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 18205
:
60848
|
60849
|
60850
|
69226
|
69227
|
69228
|
69232
|
85711
|
85712
|
85713
|
85714
|
87537
|
87538
|
87539
|
87540
|
87541
|
87542
|
87545
|
139811