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

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

#149 ロガークラス作成

Line 
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 *
19 * @package log4php
20 */
21
22/**
23 * @ignore
24 */
25if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__));
26 
27/**
28 */
29require_once(LOG4PHP_DIR . '/LoggerLog.php');
30
31
32define('LOGGER_MDC_HT_SIZE', 7);
33
34/**
35 * This is the global repository of user mappings
36 */
37$GLOBALS['log4php.LoggerMDC.ht'] = array();
38
39/**
40 * The LoggerMDC class is similar to the {@link LoggerNDC} class except that it is
41 * based on a map instead of a stack. It provides <i>mapped diagnostic contexts</i>.
42 *
43 * A <i>Mapped Diagnostic Context</i>, or
44 * MDC in short, is an instrument for distinguishing interleaved log
45 * output from different sources. Log output is typically interleaved
46 * when a server handles multiple clients near-simultaneously.
47 *
48 * <p><b><i>The MDC is managed on a per thread basis</i></b>.
49 *
50 * @author  Marco Vassura
51 * @version $Revision: 635069 $
52 * @since 0.3
53 * @package log4php
54 */
55class LoggerMDC {
56 
57    /**
58     * Put a context value as identified with the key parameter into the current thread's
59     *  context map.
60     *
61     * <p>If the current thread does not have a context map it is
62     *  created as a side effect.</p>
63     *
64     * <p>Note that you cannot put more than {@link LOGGER_MDC_HT_SIZE} keys.</p>
65     *
66     * @param string $key the key
67     * @param string $value the value
68     * @static
69     */
70    public static function put($key, $value)
71    {
72        if ( sizeof($GLOBALS['log4php.LoggerMDC.ht']) < LOGGER_MDC_HT_SIZE )
73            $GLOBALS['log4php.LoggerMDC.ht'][$key] = $value;
74    }
75 
76    /**
77     * Get the context identified by the key parameter.
78     *
79     * <p>You can use special key identifiers to map values in
80     * PHP $_SERVER and $_ENV vars. Just put a 'server.' or 'env.'
81     * followed by the var name you want to refer.</p>
82     *
83     * <p>This method has no side effects.</p>
84     *
85     * @param string $key
86     * @return string
87     * @static
88     */
89    public static function get($key)
90    {
91        LoggerLog::debug("LoggerMDC::get() key='$key'");
92   
93        if (!empty($key)) {
94            if (strpos($key, 'server.') === 0) {
95                $varName = substr($key, 7);
96               
97                LoggerLog::debug("LoggerMDC::get() a _SERVER[$varName] is requested.");
98               
99                return @$_SERVER[$varName];
100            } elseif (strpos($key, 'env.') === 0) {
101
102                $varName = substr($key, 4);
103               
104                LoggerLog::debug("LoggerMDC::get() a _ENV[$varName] is requested.");
105               
106                return @$_ENV[$varName];
107            } elseif (isset($GLOBALS['log4php.LoggerMDC.ht'][$key])) {
108           
109                LoggerLog::debug("LoggerMDC::get() a user key is requested.");
110           
111                return $GLOBALS['log4php.LoggerMDC.ht'][$key];
112            }
113        }
114        return '';
115    }
116
117    /**
118     * Remove the the context identified by the key parameter.
119     *
120     * It only affects user mappings.
121     *
122     * @param string $key
123     * @return string
124     * @static
125     */
126    public static function remove($key)
127    {
128        unset($GLOBALS['log4php.LoggerMDC.ht'][$key]);
129    }
130
131}
Note: See TracBrowser for help on using the repository browser.