mapped diagnostic contexts. * * A Mapped Diagnostic Context, or * MDC in short, is an instrument for distinguishing interleaved log * output from different sources. Log output is typically interleaved * when a server handles multiple clients near-simultaneously. * *

The MDC is managed on a per thread basis. * * @author Marco Vassura * @version $Revision: 635069 $ * @since 0.3 * @package log4php */ class LoggerMDC { /** * Put a context value as identified with the key parameter into the current thread's * context map. * *

If the current thread does not have a context map it is * created as a side effect.

* *

Note that you cannot put more than {@link LOGGER_MDC_HT_SIZE} keys.

* * @param string $key the key * @param string $value the value * @static */ public static function put($key, $value) { if ( sizeof($GLOBALS['log4php.LoggerMDC.ht']) < LOGGER_MDC_HT_SIZE ) $GLOBALS['log4php.LoggerMDC.ht'][$key] = $value; } /** * Get the context identified by the key parameter. * *

You can use special key identifiers to map values in * PHP $_SERVER and $_ENV vars. Just put a 'server.' or 'env.' * followed by the var name you want to refer.

* *

This method has no side effects.

* * @param string $key * @return string * @static */ public static function get($key) { LoggerLog::debug("LoggerMDC::get() key='$key'"); if (!empty($key)) { if (strpos($key, 'server.') === 0) { $varName = substr($key, 7); LoggerLog::debug("LoggerMDC::get() a _SERVER[$varName] is requested."); return @$_SERVER[$varName]; } elseif (strpos($key, 'env.') === 0) { $varName = substr($key, 4); LoggerLog::debug("LoggerMDC::get() a _ENV[$varName] is requested."); return @$_ENV[$varName]; } elseif (isset($GLOBALS['log4php.LoggerMDC.ht'][$key])) { LoggerLog::debug("LoggerMDC::get() a user key is requested."); return $GLOBALS['log4php.LoggerMDC.ht'][$key]; } } return ''; } /** * Remove the the context identified by the key parameter. * * It only affects user mappings. * * @param string $key * @return string * @static */ public static function remove($key) { unset($GLOBALS['log4php.LoggerMDC.ht'][$key]); } }