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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
29 my $logger = Koha::Logger->get;
30 $logger->warn( 'WARNING: Serious error encountered' );
31 $logger->debug( 'I thought that this code was not used' );
45 Log
::Log4perl
->wrapper_register(__PACKAGE__
);
50 Returns a logger object (based on log4perl).
51 Category and interface hash parameter are optional.
52 Normally, the category should follow the current package and the interface
53 should be set correctly via C4::Context.
58 my ( $class, $params ) = @_;
59 my $interface = $params ?
( $params->{interface
} || C4
::Context
->interface ) : C4
::Context
->interface;
60 my $category = $params ?
( $params->{category
} || caller ) : caller;
61 my $l4pcat = $interface . '.' . $category;
66 $self->{logger
} = Log
::Log4perl
->get_logger($l4pcat);
67 $self->{cat
} = $l4pcat;
68 $self->{logs
} = $init if ref $init;
78 In order to prevent a crash when log4perl cannot write to Koha logfile,
79 we check first before calling log4perl.
80 If log4perl would add such a check, this would no longer be needed.
85 my ( $self, $line ) = @_;
86 my $method = $Koha::Logger
::AUTOLOAD
;
87 $method =~ s/^Koha::Logger:://;
89 if ( !exists $self->{logger
} ) {
91 #do not use log4perl; no print to stderr
93 elsif ( !$self->_recheck_logfile ) {
94 warn "Log file not writable for log4perl";
95 warn "$method: $line" if $line;
97 elsif ( $self->{logger
}->can($method) ) { #use log4perl
98 $self->{logger
}->$method($line);
101 else { # we should not really get here
102 warn "ERROR: Unsupported method $method";
109 Dummy destroy to prevent call to AUTOLOAD
115 =head2 _init, _check_conf and _recheck_logfile
121 if ( exists $ENV{"LOG4PERL_CONF"} and $ENV{'LOG4PERL_CONF'} and -s
$ENV{"LOG4PERL_CONF"} ) {
123 # Check for web server level configuration first
124 # In this case we ASSUME that you correctly arranged logfile
125 # permissions. If not, log4perl will crash on you.
126 # We will not parse apache files here.
127 Log
::Log4perl
->init_once( $ENV{"LOG4PERL_CONF"} );
129 elsif ( C4
::Context
->config("log4perl_conf") ) {
131 # Now look in the koha conf file. We only check the permissions of
132 # the default logfiles. For the rest, we again ASSUME that
133 # you arranged file permissions.
134 my $conf = C4
::Context
->config("log4perl_conf");
135 if ( $rv = _check_conf
($conf) ) {
136 Log
::Log4perl
->init_once($conf);
144 # This means that you do not use log4perl currently.
145 # We will not be forcing it.
148 return 1; # if we make it here, log4perl did not crash :)
151 sub _check_conf
{ # check logfiles in log4perl config (at initialization)
154 open my $fh, '<', $file;
158 foreach my $l (@lines) {
159 if ( $l =~ /(OPAC|INTRANET)\.filename\s*=\s*(.*)\s*$/i ) {
161 # we only check the two default logfiles, skipping additional ones
163 push @logs, $1 . ':' . $2;
166 return if !@logs; # we should find one
170 sub _recheck_logfile
{ # recheck saved logfile when logging message
173 return 1 if !exists $self->{logs
}; # remember? your own responsibility
174 my $opac = $self->{cat
} =~ /^OPAC/;
176 foreach ( @
{ $self->{logs
} } ) {
177 $log = $_ if $opac && /^OPAC:/ || !$opac && /^INTRANET:/;
180 $log =~ s/^(OPAC|INTRANET)://;
184 =head2 debug_to_screen
186 Adds a new appender for the given logger that will log all DEBUG-and-higher messages to stderr.
191 sub debug_to_screen
{
194 return unless ( $self->{logger
} );
196 my $appender = Log
::Log4perl
::Appender
->new(
197 'Log::Log4perl::Appender::Screen',
200 name
=> 'debug_to_screen' # We need a specific name to prevent duplicates
203 $appender->threshold( $Log::Log4perl
::DEBUG
);
204 $self->{logger
}->add_appender( $appender );
205 $self->{logger
}->level( $Log::Log4perl
::DEBUG
);
210 Kyle M Hall, E<lt>kyle@bywatersolutions.comE<gt>
211 Marcel de Rooy, Rijksmuseum