From 1ae6acc2de24b98daa5333de6818bd290d97857b 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. Install Perl dependency MojoX::Log::Log4perl 2. 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 3cacafc..2a3bb5e 100644 --- a/C4/Installer/PerlDependencies.pm +++ b/C4/Installer/PerlDependencies.pm @@ -792,6 +792,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 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 9019c12..afe1701 100644 --- a/Koha/REST/V1.pm +++ b/Koha/REST/V1.pm @@ -20,6 +20,7 @@ use Modern::Perl; use Mojo::Base 'Mojolicious'; use C4::Context; +use Koha::Logger::Mojo; =head1 NAME @@ -40,6 +41,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..0bdebca --- /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; -- 2.7.4