source: branches/comu-ver2/data/module/log4php/php4/log4php/LoggerMDC.php @ 18220

Revision 18220, 3.9 KB checked in by yokkuns, 11 years ago (diff)

#149 ロガークラス作成

Line 
1<?php
2/**
3 * log4php is a PHP port of the log4j java logging package.
4 *
5 * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
6 * <p>Design, strategies and part of the methods documentation are developed by log4j team
7 * (Ceki Gülcü as log4j project founder and
8 * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
9 *
10 * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
11 * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
12 *
13 * <p>This software is published under the terms of the LGPL License
14 * a copy of which has been included with this distribution in the LICENSE file.</p>
15 *
16 * @package log4php
17 */
18
19/**
20 * @ignore
21 */
22if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__));
23 
24/**
25 */
26require_once(LOG4PHP_DIR . '/LoggerLog.php');
27
28
29define('LOGGER_MDC_HT_SIZE', 7);
30
31/**
32 * This is the global repository of user mappings
33 */
34$GLOBALS['log4php.LoggerMDC.ht'] = array();
35
36/**
37 * The LoggerMDC class is similar to the {@link LoggerNDC} class except that it is
38 * based on a map instead of a stack. It provides <i>mapped diagnostic contexts</i>.
39 *
40 * A <i>Mapped Diagnostic Context</i>, or
41 * MDC in short, is an instrument for distinguishing interleaved log
42 * output from different sources. Log output is typically interleaved
43 * when a server handles multiple clients near-simultaneously.
44 *
45 * <p><b><i>The MDC is managed on a per thread basis</i></b>.
46 *
47 * @author VxR <vxr@vxr.it>
48 * @version $Revision: 1.4 $
49 * @since 0.3
50 * @package log4php
51 */
52class LoggerMDC {
53 
54    /**
55     * Put a context value as identified with the key parameter into the current thread's
56     *  context map.
57     *
58     * <p>If the current thread does not have a context map it is
59     *  created as a side effect.</p>
60     *
61     * <p>Note that you cannot put more than {@link LOGGER_MDC_HT_SIZE} keys.</p>
62     *
63     * @param string $key the key
64     * @param string $value the value
65     * @static
66     */
67    function put($key, $value)
68    {
69        if ( sizeof($GLOBALS['log4php.LoggerMDC.ht']) < LOGGER_MDC_HT_SIZE )
70            $GLOBALS['log4php.LoggerMDC.ht'][$key] = $value;
71    }
72 
73    /**
74     * Get the context identified by the key parameter.
75     *
76     * <p>You can use special key identifiers to map values in
77     * PHP $_SERVER and $_ENV vars. Just put a 'server.' or 'env.'
78     * followed by the var name you want to refer.</p>
79     *
80     * <p>This method has no side effects.</p>
81     *
82     * @param string $key
83     * @return string
84     * @static
85     */
86    function get($key)
87    {
88        LoggerLog::debug("LoggerMDC::get() key='$key'");
89   
90        if (!empty($key)) {
91            if (strpos($key, 'server.') === 0) {
92                $varName = substr($key, 7);
93               
94                LoggerLog::debug("LoggerMDC::get() a _SERVER[$varName] is requested.");
95               
96                return $_SERVER[$varName];
97            } elseif (strpos($key, 'env.') === 0) {
98
99                $varName = substr($key, 4);
100               
101                LoggerLog::debug("LoggerMDC::get() a _ENV[$varName] is requested.");
102               
103                return $_ENV[$varName];
104            } elseif (isset($GLOBALS['log4php.LoggerMDC.ht'][$key])) {
105           
106                LoggerLog::debug("LoggerMDC::get() a user key is requested.");
107           
108                return $GLOBALS['log4php.LoggerMDC.ht'][$key];
109            }
110        }
111        return '';
112    }
113
114    /**
115     * Remove the the context identified by the key parameter.
116     *
117     * It only affects user mappings.
118     *
119     * @param string $key
120     * @return string
121     * @static
122     */
123    function remove($key)
124    {
125        unset($GLOBALS['log4php.LoggerMDC.ht'][$key]);
126    }
127
128}
129?>
Note: See TracBrowser for help on using the repository browser.