From b854f2181c4bedc3196669e1feaa7a0a8975a9b1 Mon Sep 17 00:00:00 2001 From: Lari Taskula 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. prove t/Koha/Logger/Mojo.t --- 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 59bbd37..d8cd0f6 100644 --- a/C4/Installer/PerlDependencies.pm +++ b/C4/Installer/PerlDependencies.pm @@ -782,6 +782,11 @@ our $PERL_DEPS = { 'required' => '0', 'min_ver' => '1.10', }, + '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 0000000..e967e83 --- /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 488b28c..9fa43f8 100644 --- a/Koha/REST/V1.pm +++ b/Koha/REST/V1.pm @@ -23,6 +23,7 @@ use C4::Context; use Koha::Account::Lines; use Koha::Checkouts; use Koha::Holds; +use Koha::Logger::Mojo; use Koha::Old::Checkouts; use Koha::Patrons; @@ -45,6 +46,8 @@ sub startup { C4::Context->interface('rest'); + $self->log(Koha::Logger::Mojo->get); + # Force charset=utf8 in Content-Type header for JSON responses $self->types->type(json => 'application/json; charset=utf8'); diff --git a/t/Koha/Logger/Mojo.t b/t/Koha/Logger/Mojo.t new file mode 100644 index 0000000..a0b9708 --- /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 . + +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; -- 1.9.1