1<?php 2/** 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 * @package log4php 19 */ 20 21/** 22 * This is the global repository of user mappings 23 */ 24$GLOBALS['log4php.LoggerMDC.ht'] = array(); 25 26/** 27 * The LoggerMDC class provides <i>mapped diagnostic contexts</i>. 28 * 29 * <p>A <i>Mapped Diagnostic Context</i>, or 30 * MDC in short, is an instrument for distinguishing interleaved log 31 * output from different sources. Log output is typically interleaved 32 * when a server handles multiple clients near-simultaneously. 33 * 34 * <p>This class is similar to the {@link LoggerNDC} class except that 35 * it is based on a map instead of a stack. 36 * 37 * <p><b>The MDC is managed on a per thread basis</b>. 38 * 39 * <p>Example: 40 * 41 * {@example ../../examples/php/mdc.php 19}<br> 42 * 43 * With the properties file: 44 * 45 * {@example ../../examples/resources/mdc.properties 18}<br> 46 * 47 * Will result in the following (notice the username "knut" in the output): 48 * 49 * <pre> 50 * 2009-09-13 18:48:28 DEBUG root knut: Testing MDC in src/examples/php/mdc.php at 23 51 * </pre> 52 * 53 * @version $Revision: 883114 $ 54 * @since 0.3 55 * @package log4php 56 */ 57class LoggerMDC { 58 /** 59 * Put a context value as identified with the key parameter into the current thread's 60 * context map. 61 * 62 * <p>If the current thread does not have a context map it is 63 * created as a side effect.</p> 64 * 65 * <p>Note that you cannot put more than {@link self::HT_SIZE} keys.</p> 66 * 67 * @param string $key the key 68 * @param string $value the value 69 * @static 70 */ 71 public static function put($key, $value) { 72 $GLOBALS['log4php.LoggerMDC.ht'][$key] = $value; 73 } 74 75 /** 76 * Get the context identified by the key parameter. 77 * 78 * <p>You can use special key identifiers to map values in 79 * PHP $_SERVER and $_ENV vars. Just put a 'server.' or 'env.' 80 * followed by the var name you want to refer.</p> 81 * 82 * <p>This method has no side effects.</p> 83 * 84 * @param string $key 85 * @return string 86 * @static 87 */ 88 public static function get($key) { 89 if(!empty($key)) { 90 if(strpos($key, 'server.') === 0) { 91 $varName = substr($key, 7); 92 return @$_SERVER[$varName]; 93 } else if(strpos($key, 'env.') === 0) { 94 $varName = substr($key, 4); 95 return @$_ENV[$varName]; 96 } else if (isset($GLOBALS['log4php.LoggerMDC.ht'][$key])) { 97 return $GLOBALS['log4php.LoggerMDC.ht'][$key]; 98 } 99 } 100 return ''; 101 } 102 103 /** 104 * Remove the the context identified by the key parameter. 105 * 106 * It only affects user mappings. 107 * 108 * @param string $key 109 * @return string 110 * @static 111 */ 112 public static function remove($key) { 113 unset($GLOBALS['log4php.LoggerMDC.ht'][$key]); 114 } 115 116} 117