From b248118b5358f0f24bb6a5ab0bd518b5d9f8aa91 Mon Sep 17 00:00:00 2001 From: Aaron Buesing Date: Fri, 13 Oct 2023 11:37:02 -0500 Subject: [PATCH 1/4] Updating 'put' method with support for multiple key/value pairs --- lib/Log/Log4perl/MDC.pm | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/Log/Log4perl/MDC.pm b/lib/Log/Log4perl/MDC.pm index ea4d63a5..98776864 100644 --- a/lib/Log/Log4perl/MDC.pm +++ b/lib/Log/Log4perl/MDC.pm @@ -28,15 +28,20 @@ sub get { ########################################### sub put { ########################################### - my($class, $key, $value) = @_; - - if($class ne __PACKAGE__) { - # Somebody called us with Log::Log4perl::MDC::put($key, $value) - $value = $key; - $key = $class; + if( $_[0] eq __PACKAGE__ ) { + # Somebody called us with Log::Log4perl::MDC->put(...); + shift( @_ ); } - $MDC_HASH{$key} = $value; + 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}; + } } ########################################### @@ -80,6 +85,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. From f2f9b9663ba86d90423e310fad25928afa9d707c Mon Sep 17 00:00:00 2001 From: Aaron Buesing Date: Fri, 13 Oct 2023 11:37:39 -0500 Subject: [PATCH 2/4] Adding unit tests for MDC module --- t/072.MDC.t | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 t/072.MDC.t diff --git a/t/072.MDC.t b/t/072.MDC.t new file mode 100644 index 00000000..366d8a38 --- /dev/null +++ b/t/072.MDC.t @@ -0,0 +1,48 @@ +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->remove(); +is_deeply(Log::Log4perl::MDC->get_context(), {}, 'Calling remove deletes all entries'); + +done_testing; From c5ed3debdbd14bbce5199c290e6f8284abb9fe99 Mon Sep 17 00:00:00 2001 From: Aaron Buesing Date: Fri, 13 Oct 2023 11:54:17 -0500 Subject: [PATCH 3/4] Added delete method to remove single key --- lib/Log/Log4perl/MDC.pm | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/Log/Log4perl/MDC.pm b/lib/Log/Log4perl/MDC.pm index 98776864..8f0a3e4a 100644 --- a/lib/Log/Log4perl/MDC.pm +++ b/lib/Log/Log4perl/MDC.pm @@ -28,7 +28,7 @@ sub get { ########################################### sub put { ########################################### - if( $_[0] eq __PACKAGE__ ) { + if( $_[0] eq __PACKAGE__ ) { # Somebody called us with Log::Log4perl::MDC->put(...); shift( @_ ); } @@ -44,6 +44,18 @@ sub put { } } +########################################### +sub delete { +########################################### + my( $class, $key ) = @_; + + if( $class ne __PACKAGE__ ) { + $key = $class; + } + + delete( $MDC_HASH{$key} ); +} + ########################################### sub remove { ########################################### @@ -101,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. @@ -147,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. - From 0f7b33ff7a6ae762a29a00dff4084b11cd278f9c Mon Sep 17 00:00:00 2001 From: Aaron Buesing Date: Fri, 13 Oct 2023 11:54:38 -0500 Subject: [PATCH 4/4] Added test for delete method --- t/072.MDC.t | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/t/072.MDC.t b/t/072.MDC.t index 366d8a38..41bba377 100644 --- a/t/072.MDC.t +++ b/t/072.MDC.t @@ -42,6 +42,12 @@ is( Log::Log4perl::MDC->get('test-four') . Log::Log4perl::MDC->get('test-four-pa 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');