Bug 16304

Summary: Koha::Logger, lazy load loggers so environment has time to get set
Product: Koha Reporter: Olli-Antti Kivilahti <olli-antti.kivilahti>
Component: Architecture, internals, and plumbingAssignee: Olli-Antti Kivilahti <olli-antti.kivilahti>
Status: NEW --- QA Contact: Testopia <testopia>
Severity: enhancement    
Priority: P5 - low CC: katrin.fischer, lari.taskula, m.de.rooy
Version: Main   
Hardware: All   
OS: All   
Change sponsored?: --- Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
Bug Depends on: 16302, 14167    
Bug Blocks: 16312, 16313    
Attachments: Bug 16304 - Koha::Logger, lazy load loggers so environment has time to get set
Bug 16304 - Koha::Logger, lazy load loggers so environment has time to get set

Description Olli-Antti Kivilahti 2016-04-19 23:01:29 UTC
If you instantiate Koha::Loggers before having set the C4::Context->interface,
their interface defaults to 'opac'.

This is rather undesired especially for 'commandline' scripts such as cronjobs.

A solution to this is to lazyLoad loggers when they are really needed.
LazyLoading might also have a performance boost when using packages that initialize a package-level logger but which never gets used.

see t/Koha/Logger.t on how to replicate this issue, but here is a nutshell of how to replicate this issue:


@@ file A.pm
package A;

use Koha::Logger;
my $logger = Koha::Logger->get({category => __PACKAGE__}); #interface unknown

sub doStuff {
    $logger->error('My interface is always "opac"');
}


@@ script run.pl
use C4::Context;
C4::Context->interface('commandline');

use A;
A::doStuff(); #error is logged using the "opac"-interface instead
              #of the "commandline"-interface
Comment 1 Olli-Antti Kivilahti 2016-04-19 23:11:17 UTC
Created attachment 50449 [details] [review]
Bug 16304 - Koha::Logger, lazy load loggers so environment has time to get set

If you instantiate Koha::Loggers before having set the C4::Context->interface,
their interface defaults to 'opac'.

This is rather undesired especially for 'commandline' scripts such as cronjobs.

A solution to this is to lazyLoad loggers when they are really needed.
LazyLoading might also have a performance boost when using packages that
initialize a package-level logger but which never gets used.

see t/Koha/Logger.t on how to replicate this issue, but here is a nutshell
of how to replicate this issue:

@@ file A.pm
package A;

use Koha::Logger;
my $logger = Koha::Logger->get({category => __PACKAGE__}); #interface unknown

sub doStuff {
    $logger->error('My interface is always "opac"');
}

@@ script run.pl
use C4::Context;
C4::Context->interface('commandline');

use A;
A::doStuff(); #error is logged using the "opac"-interface instead
              #of the "commandline"-interface
Comment 2 Olli-Antti Kivilahti 2016-04-20 08:34:54 UTC
You can also circumvent this environment setting problem with this code block:


Use C4::Context;
BEGIN {
    C4::Context->interface('commandline');
}
use Logger_Invoking_Module;


But this is dependent on the load order of Perl modules and is practically impossible to know if you are just starting to work with Koha / new to Perl.


BEGIN {
    C4::Context->interface('commandline');
}
Use C4::Context;
use Logger_Invoking_Module;


This no longer works.

Also lazyLoading saves CPU time on package-level $loggers which might not get used.
Comment 3 Lari Taskula 2017-07-06 11:50:37 UTC
Created attachment 64845 [details] [review]
Bug 16304 - Koha::Logger, lazy load loggers so environment has time to get set

If you instantiate Koha::Loggers before having set the C4::Context->interface,
their interface defaults to 'opac'.

This is rather undesired especially for 'commandline' scripts such as cronjobs.

A solution to this is to lazyLoad loggers when they are really needed.
LazyLoading might also have a performance boost when using packages that
initialize a package-level logger but which never gets used.

see t/Koha/Logger.t on how to replicate this issue, but here is a nutshell
of how to replicate this issue:

@@ file A.pm
package A;

use Koha::Logger;
my $logger = Koha::Logger->get({category => __PACKAGE__}); #interface unknown

sub doStuff {
    $logger->error('My interface is always "opac"');
}

@@ script run.pl
use C4::Context;
C4::Context->interface('commandline');

use A;
A::doStuff(); #error is logged using the "opac"-interface instead
              #of the "commandline"-interface
Comment 4 Lari Taskula 2017-07-06 11:51:09 UTC
Fixed a conflict when applying patch.
Comment 5 Katrin Fischer 2017-10-15 13:28:45 UTC
Depends on bug 16312 which is Failed QA. Marking BLOCKED until dependency can be resolved.
Comment 6 Katrin Fischer 2020-01-11 11:29:55 UTC
(In reply to Katrin Fischer from comment #5)
> Depends on bug 16312 which is Failed QA. Marking BLOCKED until dependency
> can be resolved.

Depeneny is set to NEW now, removing blocked in favor of new here as well.