use Modern::Perl;
use Log::Log4perl;
-use Carp;
use C4::Context;
my ( $class, $params ) = @_;
my $interface = $params ? ( $params->{interface} || C4::Context->interface ) : C4::Context->interface;
my $category = $params ? ( $params->{category} || caller ) : caller;
- my $l4pcat = $interface . '.' . $category;
+ my $l4pcat = ( C4::Context->psgi_env ? 'plack-' : q{} ) . $interface . '.' . $category;
my $init = _init();
my $self = {};
return $self;
}
+=head2 put_mdc
+
+my $foo = $logger->put_mdc('foo', $foo );
+
+put_mdc sets global thread specific data that can be access later when generating log lines
+via the "%X{key}" placeholder in Log::Log4perl::Layout::PatternLayouts.
+
+=cut
+
+sub put_mdc {
+ my ( $self, $key, $value ) = @_;
+
+ Log::Log4perl::MDC->put( $key, $value );
+}
+
+=head2 get_mdc
+
+my $foo = $logger->get_mdc('foo');
+
+Retrieves the stored mdc value from the stored map.
+
+=cut
+
+sub get_mdc {
+ my ( $self, $key ) = @_;
+
+ return Log::Log4perl::MDC->get( $key );
+}
+
+=head2 clear_mdc
+
+$logger->clear_mdc();
+
+Removes *all* stored key/value pairs from the MDC map.
+
+=cut
+
+sub clear_mdc {
+ my ( $self, $key ) = @_;
+
+ return Log::Log4perl::MDC->remove( $key );
+}
+
=head1 INTERNALS
=head2 AUTOLOAD
my $method = $Koha::Logger::AUTOLOAD;
$method =~ s/^Koha::Logger:://;
- if ( !exists $self->{logger} ) {
-
- #do not use log4perl; no print to stderr
- }
- elsif ( !$self->_recheck_logfile ) {
- warn "Log file not writable for log4perl";
- warn "$method: $line" if $line;
- }
- elsif ( $self->{logger}->can($method) ) { #use log4perl
- $self->{logger}->$method($line);
- return 1;
+ if ( $self->{logger}->can($method) ) { #use log4perl
+ return $self->{logger}->$method($line);
}
else { # we should not really get here
warn "ERROR: Unsupported method $method";
sub DESTROY { }
-=head2 _init, _recheck_logfile
+=head2 _init
=cut
return Log::Log4perl->init_once($log4perl_config);
}
-sub _recheck_logfile { # recheck saved logfile when logging message
- my $self = shift;
-
- return 1 if !exists $self->{logs}; # remember? your own responsibility
- my $opac = $self->{cat} =~ /^OPAC/;
- my $log;
- foreach ( @{ $self->{logs} } ) {
- $log = $_ if $opac && /^OPAC:/ || !$opac && /^INTRANET:/;
- last if $log;
- }
- $log =~ s/^(OPAC|INTRANET)://;
- return -w $log;
-}
-
=head2 debug_to_screen
Adds a new appender for the given logger that will log all DEBUG-and-higher messages to stderr.