Bug 14050: Default framework for authorities should not be deletable
[koha.git] / Koha / Logger.pm
blobb45410022703849e9968b21ffa60a2c390a8689a
1 package Koha::Logger;
3 # Copyright 2015 ByWater Solutions
4 # kyle@bywatersolutions.com
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 3 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 =head1 NAME
23 Koha::Log
25 =head1 SYNOPSIS
27 use Koha::Log;
29 =head1 FUNCTIONS
31 =cut
33 use Modern::Perl;
35 use Log::Log4perl;
36 use Carp;
38 use C4::Context;
40 BEGIN {
41 Log::Log4perl->wrapper_register(__PACKAGE__);
44 =head2 get
46 Returns a logger object (based on log4perl).
47 Category and interface hash parameter are optional.
48 Normally, the category should follow the current package and the interface
49 should be set correctly via C4::Context.
51 =cut
53 sub get {
54 my ( $class, $params ) = @_;
55 my $interface = $params ? ( $params->{interface} || C4::Context->interface ) : C4::Context->interface;
56 my $category = $params ? ( $params->{category} || caller ) : caller;
57 my $l4pcat = $interface . '.' . $category;
59 my $init = _init();
60 my $self = {};
61 if ($init) {
62 $self->{logger} = Log::Log4perl->get_logger($l4pcat);
63 $self->{cat} = $l4pcat;
64 $self->{logs} = $init if ref $init;
66 bless $self, $class;
67 return $self;
70 =head1 INTERNALS
72 =head2 AUTOLOAD
74 In order to prevent a crash when log4perl cannot write to Koha logfile,
75 we check first before calling log4perl.
76 If log4perl would add such a check, this would no longer be needed.
78 =cut
80 sub AUTOLOAD {
81 my ( $self, $line ) = @_;
82 my $method = $Koha::Logger::AUTOLOAD;
83 $method =~ s/^Koha::Logger:://;
85 if ( !exists $self->{logger} ) {
87 #do not use log4perl; no print to stderr
89 elsif ( !$self->_recheck_logfile ) {
90 warn "Log file not writable for log4perl";
91 warn "$method: $line" if $line;
93 elsif ( $self->{logger}->can($method) ) { #use log4perl
94 $self->{logger}->$method($line);
95 return 1;
97 else { # we should not really get here
98 warn "ERROR: Unsupported method $method";
100 return;
103 =head2 DESTROY
105 Dummy destroy to prevent call to AUTOLOAD
107 =cut
109 sub DESTROY { }
111 =head2 _init, _check_conf and _recheck_logfile
113 =cut
115 sub _init {
116 my $rv;
117 if ( exists $ENV{"LOG4PERL_CONF"} and $ENV{'LOG4PERL_CONF'} and -s $ENV{"LOG4PERL_CONF"} ) {
119 # Check for web server level configuration first
120 # In this case we ASSUME that you correctly arranged logfile
121 # permissions. If not, log4perl will crash on you.
122 # We will not parse apache files here.
123 Log::Log4perl->init_once( $ENV{"LOG4PERL_CONF"} );
125 elsif ( C4::Context->config("log4perl_conf") ) {
127 # Now look in the koha conf file. We only check the permissions of
128 # the default logfiles. For the rest, we again ASSUME that
129 # you arranged file permissions.
130 my $conf = C4::Context->config("log4perl_conf");
131 if ( $rv = _check_conf($conf) ) {
132 Log::Log4perl->init_once($conf);
133 return $rv;
135 else {
136 return 0;
139 else {
140 # This means that you do not use log4perl currently.
141 # We will not be forcing it.
142 return 0;
144 return 1; # if we make it here, log4perl did not crash :)
147 sub _check_conf { # check logfiles in log4perl config (at initialization)
148 my $file = shift;
149 return if !-r $file;
150 open my $fh, '<', $file;
151 my @lines = <$fh>;
152 close $fh;
153 my @logs;
154 foreach my $l (@lines) {
155 if ( $l =~ /(OPAC|INTRANET)\.filename\s*=\s*(.*)\s*$/i ) {
157 # we only check the two default logfiles, skipping additional ones
158 return if !-w $2;
159 push @logs, $1 . ':' . $2;
162 return if !@logs; # we should find one
163 return \@logs;
166 sub _recheck_logfile { # recheck saved logfile when logging message
167 my $self = shift;
169 return 1 if !exists $self->{logs}; # remember? your own responsibility
170 my $opac = $self->{cat} =~ /^OPAC/;
171 my $log;
172 foreach ( @{ $self->{logs} } ) {
173 $log = $_ if $opac && /^OPAC:/ || !$opac && /^INTRANET:/;
174 last if $log;
176 $log =~ s/^(OPAC|INTRANET)://;
177 return -w $log;
180 =head1 AUTHOR
182 Kyle M Hall, E<lt>kyle@bywatersolutions.comE<gt>
183 Marcel de Rooy, Rijksmuseum
185 =cut
189 __END__