diff --git a/lib/Log/Log4perl/MDC.pm b/lib/Log/Log4perl/MDC.pm index ea4d63a5..8f0a3e4a 100644 --- a/lib/Log/Log4perl/MDC.pm +++ b/lib/Log/Log4perl/MDC.pm @@ -28,15 +28,32 @@ sub get { ########################################### sub put { ########################################### - my($class, $key, $value) = @_; + if( $_[0] eq __PACKAGE__ ) { + # Somebody called us with Log::Log4perl::MDC->put(...); + shift( @_ ); + } - if($class ne __PACKAGE__) { - # Somebody called us with Log::Log4perl::MDC::put($key, $value) - $value = $key; - $key = $class; + my %values = (ref $_[0] eq 'HASH') ? + # called with hashref argument + %{ $_[0] } : + # called with list of key value pairs + @_; + + foreach my $key( keys %values ) { + $MDC_HASH{$key} = $values{$key}; + } +} + +########################################### +sub delete { +########################################### + my( $class, $key ) = @_; + + if( $class ne __PACKAGE__ ) { + $key = $class; } - $MDC_HASH{$key} = $value; + delete( $MDC_HASH{$key} ); } ########################################### @@ -80,6 +97,15 @@ Cs. Store a value C<$value> under key C<$key> in the map. +=item Log::Log4perl::MDC->put($key1 => $value1, $key2 => $value2); + +=item Log::Log4perl::MDC->put({$key1 => $value1, $key2 => $value2}); + +Store multiple key C<$key#>/value C<$value#> pairs in the map. + +NOTE: This diverges from the log4j implementation where only one key/value +pair can be added at a time. + =item my $value = Log::Log4perl::MDC->get($key); Retrieve the content of the map under the specified key. @@ -87,11 +113,19 @@ Typically done by C<%X{key}> in C. If no value exists to the given key, C is returned. -=item my $text = Log::Log4perl::MDC->remove(); +=item Log::Log4perl::MDC->delete($key); + +Deletes the C<$key> in the context map. + +NOTE: In log4j, this is the 'remove' method. + +=item Log::Log4perl::MDC->remove(); Delete all entries from the map. -=item Log::Log4perl::MDC->get_context(); +NOTE: In log4j, this is the 'clear' method. + +=item my $context = Log::Log4perl::MDC->get_context(); Returns a reference to the hash table. @@ -133,4 +167,3 @@ Grundman, Paul Harrington, Alexander Hartmaier David Hull, Robert Jacobson, Jason Kohles, Jeff Macdonald, Markus Peter, Brett Rann, Peter Rabbitson, Erik Selberg, Aaron Straup Cope, Lars Thegler, David Viner, Mac Yang. - diff --git a/t/072.MDC.t b/t/072.MDC.t new file mode 100644 index 00000000..41bba377 --- /dev/null +++ b/t/072.MDC.t @@ -0,0 +1,54 @@ +BEGIN { + if($ENV{INTERNAL_DEBUG}) { + require Log::Log4perl::InternalDebug; + Log::Log4perl::InternalDebug->enable(); + } +} + +use strict; +use warnings; + +use Test::More; +use Log::Log4perl::MDC; + +Log::Log4perl::MDC::put('test-one', 'value-one'); +is( Log::Log4perl::MDC::get('test-one'), + 'value-one', + 'Calling put/get class methods works with colon notation' +); + +Log::Log4perl::MDC->put('test-two', 'value-two'); +is( Log::Log4perl::MDC->get('test-two'), + 'value-two', + 'Calling put/get class methods works with arrow notation' +); + +# We have verified both arrow and colon notation work. Sticking +# with arrow notation from now on. + +Log::Log4perl::MDC->put('test-three' => 'value-three', + 'test-three-part-two' => 'value-three-part-two'); +is( Log::Log4perl::MDC->get('test-three') . Log::Log4perl::MDC->get('test-three-part-two'), + 'value-threevalue-three-part-two', + 'Calling put with multiple key/value pairs adds all to store' +); + +Log::Log4perl::MDC->put({ 'test-four' => 'value-four', + 'test-four-part-two' => 'value-four-part-two' }); +is( Log::Log4perl::MDC->get('test-four') . Log::Log4perl::MDC->get('test-four-part-two'), + 'value-fourvalue-four-part-two', + 'Calling put with hashref adds all key/values to store' +); + +is( Log::Log4perl::MDC->get('test-five'), undef, 'Calling get on unknown key returns undef'); + +Log::Log4perl::MDC->delete('test-three'); +is( Log::Log4perl::MDC->get('test-three'), + undef, + 'Calling delete on a key removes from context' +); + +Log::Log4perl::MDC->remove(); +is_deeply(Log::Log4perl::MDC->get_context(), {}, 'Calling remove deletes all entries'); + +done_testing;