| 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 plumbing | Assignee: | Olli-Antti Kivilahti <olli-antti.kivilahti> | 
| Status: | NEW --- | QA Contact: | Testopia <testopia> | 
| Severity: | enhancement | ||
| Priority: | P5 - low | CC: | lari.taskula, m.de.rooy | 
| Version: | Main | ||
| Hardware: | All | ||
| OS: | All | ||
| GIT URL: | Initiative type: | --- | |
| Sponsorship status: | --- | Crowdfunding goal: | 0 | 
| Patch complexity: | --- | Documentation contact: | |
| Documentation submission: | Text to go in the release notes: | ||
| Version(s) released in: | Circulation function: | ||
| 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 | ||
| 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 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.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 Fixed a conflict when applying patch. Depends on bug 16312 which is Failed QA. Marking BLOCKED until dependency can be resolved. (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. | 
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