View | Details | Raw Unified | Return to bug 18205
Collapse All | Expand All

(-)a/C4/Installer/PerlDependencies.pm (+5 lines)
Lines 777-782 our $PERL_DEPS = { Link Here
777
        'required' => '1',
777
        'required' => '1',
778
        'min_ver'  => '0.97',
778
        'min_ver'  => '0.97',
779
    },
779
    },
780
    'MojoX::Log::Log4perl' => {
781
        'usage'    => 'Logs',
782
        'required' => '0',
783
        'min_ver'  => '0.11',
784
    },
780
    'UNIVERSAL::can' => {
785
    'UNIVERSAL::can' => {
781
        'usage'    => 'SIP',
786
        'usage'    => 'SIP',
782
        'required' => '1',
787
        'required' => '1',
(-)a/Koha/Logger/Mojo.pm (+102 lines)
Line 0 Link Here
1
package Koha::Logger::Mojo;
2
3
# Copyright 2017 Koha-Suomi Oy
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
=head1 NAME
21
22
Koha::Logger::Mojo
23
24
=head1 SYNOPSIS
25
26
    use Koha::Logger::Mojo;
27
28
    $c->app->log(Koha::Logger::Mojo->get);
29
    $c->app->log->warn( 'WARNING: Serious error encountered' );
30
31
    EXAMPLE CONFIGS:
32
    log4perl.logger.rest = ERROR, REST
33
    log4perl.appender.REST=Log::Log4perl::Appender::File
34
    log4perl.appender.REST.filename=__LOG_DIR__/rest.log
35
    log4perl.appender.REST.create_at_logtime=true
36
    log4perl.appender.REST.syswrite=true
37
    log4perl.appender.REST.recreate=true
38
    log4perl.appender.REST.mode=append
39
    log4perl.appender.REST.layout=PatternLayout
40
    log4perl.appender.REST.layout.ConversionPattern=[%d] [%p] %m %l %n
41
42
    log4perl.logger.rest.Mojolicious.Plugin.OpenAPI = WARN, REST
43
    log4perl.appender.REST=Log::Log4perl::Appender::File
44
    log4perl.appender.REST.filename=__LOG_DIR__/rest.log
45
    log4perl.appender.REST.create_at_logtime=true
46
    log4perl.appender.REST.syswrite=true
47
    log4perl.appender.REST.recreate=true
48
    log4perl.appender.REST.mode=append
49
    log4perl.appender.REST.layout=PatternLayout
50
    log4perl.appender.REST.layout.ConversionPattern=[%d] [%p] %m %l %n
51
52
=head1 DESCRIPTION
53
54
    Use Log4perl on Mojolicious with the help of MojoX::Log::Log4perl.
55
=cut
56
57
use Modern::Perl;
58
59
use base 'MojoX::Log::Log4perl';
60
61
sub get {
62
    my ($class, $params) = @_;
63
64
    my $self;
65
    if ($ENV{'LOG4PERL_CONF'} and -s $ENV{"LOG4PERL_CONF"}) {
66
        $self = MojoX::Log::Log4perl->new($ENV{"LOG4PERL_CONF"});
67
    } elsif (C4::Context->config("log4perl_conf")) {
68
        $self = MojoX::Log::Log4perl->new(C4::Context->config("log4perl_conf"));
69
    } else {
70
        $self = MojoX::Log::Log4perl->new;
71
    }
72
    my $interface = $params ? $params->{interface}
73
                            ? $params->{interface} : C4::Context->interface
74
                            : C4::Context->interface;
75
    $self->{interface} = $interface;
76
    $self->{category} = $params->{category};
77
78
    bless $self, $class;
79
    return $self;
80
}
81
82
=head3 _get_logger
83
84
Overloads MojoX::Log::Log4perl::_get_logger by optionally including interface
85
and category.
86
87
For REST API, 'rest' should be the root category by default (defined in startup)
88
89
=cut
90
91
sub _get_logger {
92
    my ($self, $depth) = @_;
93
94
    my $category = $self->{category} ? $self->{category}
95
                                     : scalar caller( $depth || 1 );
96
    my $l4pcat  = $self->{interface} ? $self->{interface}.'.' : '';
97
       $l4pcat .= $category if $category;
98
99
    return Log::Log4perl->get_logger($l4pcat);
100
}
101
102
1;
(-)a/Koha/REST/V1.pm (+3 lines)
Lines 21-26 use Mojo::Base 'Mojolicious'; Link Here
21
21
22
use C4::Context;
22
use C4::Context;
23
use JSON::Validator::OpenAPI::Mojolicious;
23
use JSON::Validator::OpenAPI::Mojolicious;
24
use Koha::Logger::Mojo;
24
25
25
=head1 NAME
26
=head1 NAME
26
27
Lines 41-46 sub startup { Link Here
41
42
42
    C4::Context->interface('rest');
43
    C4::Context->interface('rest');
43
44
45
    $self->log(Koha::Logger::Mojo->get);
46
44
    # Remove /api/v1/app.pl/ from the path
47
    # Remove /api/v1/app.pl/ from the path
45
    $self->hook( before_dispatch => sub {
48
    $self->hook( before_dispatch => sub {
46
        shift->req->url->base->path('/');
49
        shift->req->url->base->path('/');
(-)a/t/Koha/Logger/Mojo.t (-1 / +85 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
use Mojo::Base -strict;
20
use Mojolicious::Lite;
21
use Log::Log4perl;
22
23
use Test::Mojo;
24
use Test::More tests => 4;
25
use t::lib::Mocks;
26
27
use C4::Context;
28
29
BEGIN {
30
    $ENV{MOJO_MODE}    = 'development';
31
    $ENV{MOJO_NO_IPV6} = 1;
32
    $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
33
    $ENV{'LOG4PERL_CONF'} = undef;
34
}
35
36
use_ok('Koha::Logger::Mojo');
37
38
# Create test context
39
my $config = {
40
    'log4perl.logger.rest' => 'ERROR, TEST',
41
    'log4perl.appender.TEST' => 'Log::Log4perl::Appender::TestBuffer',
42
    'log4perl.appender.TEST.layout' => 'SimpleLayout',
43
};
44
C4::Context->interface('rest');
45
t::lib::Mocks::mock_config('log4perl_conf', $config);
46
47
subtest 'check inteface' => sub {
48
    plan tests => 1;
49
50
    is(C4::Context->interface, 'rest', 'Using appropriate interface.');
51
};
52
53
subtest 'get() tests' => sub {
54
    plan tests => 1;
55
56
    app->log(Koha::Logger::Mojo->get);
57
    my $log = app->log;
58
    isa_ok($log, 'Koha::Logger::Mojo');
59
};
60
61
subtest 'Test configuration effectiveness' => sub {
62
    plan tests => 6;
63
64
    get '/test' => sub {
65
        package Koha::REST::V1::Test; # otherwise category would be rest.main
66
        $_[0]->app->log->error('Very problem');
67
        $_[0]->app->log->warn('Such issue');
68
        $_[0]->render(text => 'Hello World');
69
    };
70
71
    app->log(Koha::Logger::Mojo->get);
72
73
    my $appender = Log::Log4perl->appenders->{TEST};
74
    ok($appender, 'Got TEST appender');
75
76
    my $t = Test::Mojo->new;
77
    $t->app->log(Koha::Logger::Mojo->get);
78
    $t->get_ok('/test')
79
      ->status_is(200)
80
      ->content_is('Hello World');
81
    like($appender->buffer, qr/ERROR - Very problem/, 'found wanted message');
82
    unlike($appender->buffer, qr/WARN - Such issue/, 'did not find unwanted message');
83
};
84
85
1;

Return to bug 18205