home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / MDC.pm < prev    next >
Encoding:
Perl POD Document  |  2003-01-14  |  2.4 KB  |  108 lines

  1. ##################################################
  2. package Log::Log4perl::MDC;
  3. ##################################################
  4.  
  5. use 5.006;
  6. use strict;
  7. use warnings;
  8.  
  9. our %MDC_HASH = ();
  10.  
  11. ###########################################
  12. sub 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. ###########################################
  29. sub 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. ###########################################
  43. sub remove {
  44. ###########################################
  45.     %MDC_HASH = ();
  46.  
  47.     1;
  48. }
  49.  
  50. ###########################################
  51. sub get_context {
  52. ###########################################
  53.     return \%MDC_HASH;
  54. }
  55.  
  56. 1;
  57.  
  58. __END__
  59.  
  60. =head1 NAME
  61.  
  62. Log::Log4perl::MDC - Mapped Diagnostic Context
  63.  
  64. =head1 DESCRIPTION
  65.  
  66. Log::Log4perl allows loggers to maintain global thread-specific data, 
  67. called the Nested Diagnostic Context (NDC) and 
  68. Mapped Diagnostic Context (MDC).
  69.  
  70. The MDC is a simple thread-specific hash table, in which the application
  71. can stuff values under certain keys and retrieve them later
  72. via the C<"%X{key}"> placeholder in 
  73. C<Log::Log4perl::Layout::PatternLayout>s.
  74.  
  75. =over 4
  76.  
  77. =item Log::Log4perl::MDC->put($key, $value);
  78.  
  79. Store a value C<$value> under key C<$key> in the map.
  80.  
  81. =item my $value = Log::Log4perl::MDC->get($key);
  82.  
  83. Retrieve the content of the map under the specified key.
  84. Typically done by C<%X{key}> in
  85. C<Log::Log4perl::Layout::PatternLayout>.
  86. If no value exists to the given key, the string C<"[undef]"> is being returned.
  87.  
  88. =item my $text = Log::Log4perl::MDC->remove();
  89.  
  90. Delete all entries from the map.
  91.  
  92. =item Log::Log4perl::NDC->get_context();
  93.  
  94. Returns a reference to the hash table.
  95.  
  96. =back
  97.  
  98. Please note that all of the methods above are class methods, there's no
  99. instances of this class. Since the thread model in perl 5.8.0 is
  100. "no shared data unless explicetly requested" the data structures
  101. used are just global (and therefore thread-specific).
  102.  
  103. =head1 AUTHOR
  104.  
  105. Mike Schilli, E<lt>log4perl@perlmeister.comE<gt>
  106.  
  107. =cut
  108.