-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggerMDC.php
More file actions
129 lines (111 loc) · 3.88 KB
/
LoggerMDC.php
File metadata and controls
129 lines (111 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* log4php is a PHP port of the log4j java logging package.
*
* <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
* <p>Design, strategies and part of the methods documentation are developed by log4j team
* (Ceki Gülcü as log4j project founder and
* {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
*
* <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
* For more information, please see {@link http://www.vxr.it/log4php/}.</p>
*
* <p>This software is published under the terms of the LGPL License
* a copy of which has been included with this distribution in the LICENSE file.</p>
*
* @package log4php
*/
/**
* @ignore
*/
if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__));
/**
*/
require_once(LOG4PHP_DIR . '/LoggerLog.php');
define('LOGGER_MDC_HT_SIZE', 7);
/**
* This is the global repository of user mappings
*/
$GLOBALS['log4php.LoggerMDC.ht'] = array();
/**
* The LoggerMDC class is similar to the {@link LoggerNDC} class except that it is
* based on a map instead of a stack. It provides <i>mapped diagnostic contexts</i>.
*
* A <i>Mapped Diagnostic Context</i>, 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.
*
* <p><b><i>The MDC is managed on a per thread basis</i></b>.
*
* @author VxR <vxr@vxr.it>
* @version $Revision: 1.1 $
* @since 0.3
* @package log4php
*/
class LoggerMDC {
/**
* Put a context value as identified with the key parameter into the current thread's
* context map.
*
* <p>If the current thread does not have a context map it is
* created as a side effect.</p>
*
* <p>Note that you cannot put more than {@link LOGGER_MDC_HT_SIZE} keys.</p>
*
* @param string $key the key
* @param string $value the value
* @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.
*
* <p>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.</p>
*
* <p>This method has no side effects.</p>
*
* @param string $key
* @return string
* @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
*/
function remove($key)
{
unset($GLOBALS['log4php.LoggerMDC.ht'][$key]);
}
}
?>