Skip to content

Commit

Permalink
Added Log::Log4perl->remove_logger($logger) to remove a logger from
Browse files Browse the repository at this point in the history
the system.
  • Loading branch information
mschilli committed May 22, 2011
1 parent d852b54 commit 8a3c7e6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -11,6 +11,8 @@
https://github.com/mschilli/log4perl/issues/7), so I put on my https://github.com/mschilli/log4perl/issues/7), so I put on my
hazmat suit and cleaned it up. Now perl's garbage collector takes hazmat suit and cleaned it up. Now perl's garbage collector takes
care of disposing of logger and appender carcasses. care of disposing of logger and appender carcasses.
* (ms) Added Log::Log4perl->remove_logger($logger) to remove a logger
from the system.


1.32 (2011/02/26) 1.32 (2011/02/26)
* (ms) Fixed %T caller_depth with wrapper_register(), reported * (ms) Fixed %T caller_depth with wrapper_register(), reported
Expand Down
34 changes: 34 additions & 0 deletions lib/Log/Log4perl.pm
Expand Up @@ -559,6 +559,34 @@ sub easy_closure_global_cleanup {
} }
} }


###########################################
sub easy_closure_logger_remove {
###########################################
my($class, $logger) = @_;

PKG: for my $caller_pkg ( keys %$EASY_CLOSURES ) {
for my $entry ( keys %{ $EASY_CLOSURES->{ $caller_pkg } } ) {
if( $logger == $EASY_CLOSURES->{ $caller_pkg }->{ $entry } ) {
easy_closure_category_cleanup( $caller_pkg );
next PKG;
}
}
}
}

##################################################
sub remove_logger {
##################################################
my ($class, $logger) = @_;

# Any stealth logger convenience function still using it will
# now become a no-op.
Log::Log4perl->easy_closure_logger_remove( $logger );

# Remove the logger from the system
delete $Log::Log4perl::Logger::LOGGERS_BY_NAME->{ $logger->{category} };
}

1; 1;


__END__ __END__
Expand Down Expand Up @@ -2483,6 +2511,12 @@ you need to call C<Log::Log4perl-E<gt>eradicate_appender($appender_name)>
which will first remove the appender from every logger in the system which will first remove the appender from every logger in the system
and then will delete all references Log4perl holds to it. and then will delete all references Log4perl holds to it.
To remove a logger from the system, use
C<Log::Log4perl-E<gt>remove_logger($logger)>. After the remaining
reference C<$logger> goes away, the logger will self-destruct. If the
logger in question is a stealth logger, all of its convenience shortcuts
(DEBUG, INFO, etc) will turn into no-ops.
=head1 How about Log::Dispatch::Config? =head1 How about Log::Dispatch::Config?
Tatsuhiko Miyagawa's C<Log::Dispatch::Config> is a very clever Tatsuhiko Miyagawa's C<Log::Dispatch::Config> is a very clever
Expand Down
6 changes: 2 additions & 4 deletions lib/Log/Log4perl/Logger.pm
Expand Up @@ -79,8 +79,8 @@ sub cleanup {
################################################## ##################################################
sub DESTROY { sub DESTROY {
################################################## ##################################################
CORE::warn CORE::warn "Destroying logger $_[0] ($_[0]->{category})"
"Destroying logger $_[0]" if $Log::Log4perl::CHATTY_DESTROY_METHODS; if $Log::Log4perl::CHATTY_DESTROY_METHODS;
} }


################################################## ##################################################
Expand Down Expand Up @@ -1074,8 +1074,6 @@ sub dec_level {
$self->set_output_methods; $self->set_output_methods;
} }


##################################################

1; 1;


__END__ __END__
Expand Down
56 changes: 56 additions & 0 deletions t/063LoggerRemove.t
@@ -0,0 +1,56 @@
# http://stackoverflow.com/questions/5914088 and
# https://github.com/mschilli/log4perl/issues/7

use strict;
use Test::More;
use Log::Log4perl::Appender::TestBuffer;

plan tests => 6;

use Log::Log4perl qw(get_logger :easy);

# $Log::Log4perl::CHATTY_DESTROY_METHODS = 1;

my $conf = q(
log4perl.category.main = WARN, LogBuffer
log4perl.category.Bar.Twix = WARN, LogBuffer
log4perl.appender.LogBuffer = Log::Log4perl::Appender::TestBuffer
log4perl.appender.LogBuffer.layout = \
Log::Log4perl::Layout::PatternLayout
log4perl.appender.LogBuffer.layout.ConversionPattern = %d %F{1} %L> %m %n
);

Log::Log4perl::init(\$conf);

my $buffer = Log::Log4perl::Appender::TestBuffer->by_name("LogBuffer");

my $logger = get_logger("Bar::Twix");

ok(exists $Log::Log4perl::Logger::LOGGERS_BY_NAME->{"Bar.Twix"},
"logger exists");

Log::Log4perl->remove_logger( $logger );
undef $logger;

ok(!exists $Log::Log4perl::Logger::LOGGERS_BY_NAME->{"Bar.Twix"},
"logger gone");

# now remove a stealth logger
$logger = get_logger("main");

ok(exists $Log::Log4perl::Logger::LOGGERS_BY_NAME->{"main"},
"logger exists");

WARN "before";

Log::Log4perl->remove_logger( $logger );
undef $logger;

ok(!exists $Log::Log4perl::Logger::LOGGERS_BY_NAME->{"main"},
"logger gone");

# this should be a no-op now.
WARN "after";

like($buffer->buffer, qr/before/, "log message before logger removal present");
unlike($buffer->buffer, qr/after/, "log message after logger removal absent");

0 comments on commit 8a3c7e6

Please sign in to comment.