1################################################## 2package Log::Log4perl::MDC; 3################################################## 4 5use 5.006; 6use strict; 7use warnings; 8 9our %MDC_HASH = (); 10 11########################################### 12sub get { 13########################################### 14 my($class, $key) = @_; 15 16 if($class ne __PACKAGE__) { 17 # Somebody called us with Log::Log4perl::MDC::get($key) 18 $key = $class; 19 } 20 21 if(exists $MDC_HASH{$key}) { 22 return $MDC_HASH{$key}; 23 } else { 24 return undef; 25 } 26} 27 28########################################### 29sub put { 30########################################### 31 my($class, $key, $value) = @_; 32 33 if($class ne __PACKAGE__) { 34 # Somebody called us with Log::Log4perl::MDC::put($key, $value) 35 $value = $key; 36 $key = $class; 37 } 38 39 $MDC_HASH{$key} = $value; 40} 41 42########################################### 43sub remove { 44########################################### 45 %MDC_HASH = (); 46 47 1; 48} 49 50########################################### 51sub get_context { 52########################################### 53 return \%MDC_HASH; 54} 55 561; 57 58__END__ 59 60=head1 NAME 61 62Log::Log4perl::MDC - Mapped Diagnostic Context 63 64=head1 DESCRIPTION 65 66Log::Log4perl allows loggers to maintain global thread-specific data, 67called the Nested Diagnostic Context (NDC) and 68Mapped Diagnostic Context (MDC). 69 70The MDC is a simple thread-specific hash table, in which the application 71can stuff values under certain keys and retrieve them later 72via the C<"%X{key}"> placeholder in 73C<Log::Log4perl::Layout::PatternLayout>s. 74 75=over 4 76 77=item Log::Log4perl::MDC->put($key, $value); 78 79Store a value C<$value> under key C<$key> in the map. 80 81=item my $value = Log::Log4perl::MDC->get($key); 82 83Retrieve the content of the map under the specified key. 84Typically done by C<%X{key}> in 85C<Log::Log4perl::Layout::PatternLayout>. 86If no value exists to the given key, C<undef> is returned. 87 88=item my $text = Log::Log4perl::MDC->remove(); 89 90Delete all entries from the map. 91 92=item Log::Log4perl::MDC->get_context(); 93 94Returns a reference to the hash table. 95 96=back 97 98Please note that all of the methods above are class methods, there's no 99instances of this class. Since the thread model in perl 5.8.0 is 100"no shared data unless explicetly requested" the data structures 101used are just global (and therefore thread-specific). 102 103=head1 LICENSE 104 105Copyright 2002-2012 by Mike Schilli E<lt>m@perlmeister.comE<gt> 106and Kevin Goess E<lt>cpan@goess.orgE<gt>. 107 108This library is free software; you can redistribute it and/or modify 109it under the same terms as Perl itself. 110 111=head1 AUTHOR 112 113Please contribute patches to the project on Github: 114 115 http://github.com/mschilli/log4perl 116 117Send bug reports or requests for enhancements to the authors via our 118 119MAILING LIST (questions, bug reports, suggestions/patches): 120log4perl-devel@lists.sourceforge.net 121 122Authors (please contact them via the list above, not directly): 123Mike Schilli <m@perlmeister.com>, 124Kevin Goess <cpan@goess.org> 125 126Contributors (in alphabetical order): 127Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp, Hutton 128Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony 129Foiani, James FitzGibbon, Carl Franks, Dennis Gregorovic, Andy 130Grundman, Paul Harrington, David Hull, Robert Jacobson, Jason Kohles, 131Jeff Macdonald, Markus Peter, Brett Rann, Peter Rabbitson, Erik 132Selberg, Aaron Straup Cope, Lars Thegler, David Viner, Mac Yang. 133 134