Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Log::Log4perl->remove_logger($logger) to remove a logger from

the system.
  • Loading branch information...
commit 8a3c7e6db43f4ef3d40c7a12820ea03e6488728a 1 parent d852b54
@mschilli authored
View
2  Changes
@@ -11,6 +11,8 @@
https://github.com/mschilli/log4perl/issues/7), so I put on my
hazmat suit and cleaned it up. Now perl's garbage collector takes
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)
* (ms) Fixed %T caller_depth with wrapper_register(), reported
View
34 lib/Log/Log4perl.pm
@@ -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;
__END__
@@ -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
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?
Tatsuhiko Miyagawa's C<Log::Dispatch::Config> is a very clever
View
6 lib/Log/Log4perl/Logger.pm
@@ -79,8 +79,8 @@ sub cleanup {
##################################################
sub DESTROY {
##################################################
- CORE::warn
- "Destroying logger $_[0]" if $Log::Log4perl::CHATTY_DESTROY_METHODS;
+ CORE::warn "Destroying logger $_[0] ($_[0]->{category})"
+ if $Log::Log4perl::CHATTY_DESTROY_METHODS;
}
##################################################
@@ -1074,8 +1074,6 @@ sub dec_level {
$self->set_output_methods;
}
-##################################################
-
1;
__END__
View
56 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");
Please sign in to comment.
Something went wrong with that request. Please try again.