Bug 16304 - Koha::Logger, lazy load loggers so environment has time to get set
Summary: Koha::Logger, lazy load loggers so environment has time to get set
Status: NEW
Alias: None
Product: Koha
Classification: Unclassified
Component: Architecture, internals, and plumbing (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement (vote)
Assignee: Olli-Antti Kivilahti
QA Contact: Testopia
URL:
Keywords:
Depends on: 16302 14167
Blocks: 16312 16313
  Show dependency treegraph
 
Reported: 2016-04-19 23:01 UTC by Olli-Antti Kivilahti
Modified: 2020-01-11 11:29 UTC (History)
3 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:


Attachments
Bug 16304 - Koha::Logger, lazy load loggers so environment has time to get set (11.25 KB, patch)
2016-04-19 23:11 UTC, Olli-Antti Kivilahti
Details | Diff | Splinter Review
Bug 16304 - Koha::Logger, lazy load loggers so environment has time to get set (11.15 KB, patch)
2017-07-06 11:50 UTC, Lari Taskula
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
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.