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. * *

This class is similar to the {@link LoggerNDC} class except that * it is based on a map instead of a stack. * *

The MDC is managed on a per thread basis. * *

Example: * * {@example ../../examples/php/mdc.php 19}
* * With the properties file: * * {@example ../../examples/resources/mdc.properties 18}
* * Will result in the following (notice the username "knut" in the output): * *

 * 2009-09-13 18:48:28 DEBUG root knut: Testing MDC in src/examples/php/mdc.php at 23
 * 
* * @version $Revision$ * @since 0.3 * @package log4php */ class LoggerMDC { /** * This is the repository of user mappings */ private static $map = array(); /** * 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 self::HT_SIZE} keys.

* * @param string $key the key * @param string $value the value * @static */ public static function put($key, $value) { self::$map[$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 the key * @return string the context or an empty string if no context found * for given key * @static */ public static function get($key) { if(!empty($key)) { if(strpos($key, 'server.') === 0) { $varName = substr($key, 7); return isset($_SERVER[$varName]) ? $_SERVER[$varName] : ''; } else if(strpos($key, 'env.') === 0) { $varName = substr($key, 4); $value = getenv($varName); return ($value !== false) ? $value : ''; } else { return isset(self::$map[$key]) ? self::$map[$key] : ''; } } return ''; } /** * Remove the the context identified by the key parameter. * * It only affects user mappings, not $_ENV or $_SERVER. * * @param string $key the key to be removed * @static */ public static function remove($key) { unset(self::$map[$key]); } }