Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mulitple Key/Value Pair to MDC->put and Add Delete Method to MDC #128

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions lib/Log/Log4perl/MDC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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} );
}

###########################################
Expand Down Expand Up @@ -80,18 +97,35 @@ C<Log::Log4perl::Layout::PatternLayout>s.

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.
Typically done by C<%X{key}> in
C<Log::Log4perl::Layout::PatternLayout>.
If no value exists to the given key, C<undef> 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.

Expand Down Expand Up @@ -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.

54 changes: 54 additions & 0 deletions t/072.MDC.t
Original file line number Diff line number Diff line change
@@ -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;