|
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; |