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

Revision 18220, 7.5 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
31define('LOGGER_NDC_HT_SIZE', 7);
32
33/**
34 * This is the global repository of NDC stack
35 */
36$GLOBALS['log4php.LoggerNDC.ht'] = array();
37
38/**
39 * This is the max depth of NDC stack
40 */
41$GLOBALS['log4php.LoggerNDC.maxDepth'] = LOGGER_NDC_HT_SIZE;
42
43
44/**
45 * The NDC class implements <i>nested diagnostic contexts</i> as
46 * defined by Neil Harrison in the article "Patterns for Logging
47 * Diagnostic Messages" part of the book "<i>Pattern Languages of
48 * Program Design 3</i>" edited by Martin et al.
49 *
50 * <p>A Nested Diagnostic Context, or NDC in short, is an instrument
51 * to distinguish interleaved log output from different sources. Log
52 * output is typically interleaved when a server handles multiple
53 * clients near-simultaneously.
54 *
55 * <p>Interleaved log output can still be meaningful if each log entry
56 * from different contexts had a distinctive stamp. This is where NDCs
57 * come into play.
58 *
59 * <p><i><b>Note that NDCs are managed on a per thread
60 * basis</b></i>. NDC operations such as {@link push()}, {@link pop()},
61 * {@link clear()}, {@link getDepth()} and {@link setMaxDepth()}
62 * affect the NDC of the <i>current</i> thread only. NDCs of other
63 * threads remain unaffected.
64 *
65 * <p>For example, a servlet can build a per client request NDC
66 * consisting the clients host name and other information contained in
67 * the the request. <i>Cookies</i> are another source of distinctive
68 * information. To build an NDC one uses the {@link push()}
69 * operation.</p>
70 *
71 * Simply put,
72 *
73 * - Contexts can be nested.
74 * - When entering a context, call
75 *   <code>LoggerNDC::push()</code>
76 *   As a side effect, if there is no nested diagnostic context for the
77 *   current thread, this method will create it.
78 * - When leaving a context, call
79 *   <code>LoggerNDC::pop()</code>
80 * - <b>When exiting a thread make sure to call {@link remove()}</b>
81 *   
82 * <p>There is no penalty for forgetting to match each
83 * <code>push</code> operation with a corresponding <code>pop</code>,
84 * except the obvious mismatch between the real application context
85 * and the context set in the NDC.</p>
86 *
87 * <p>If configured to do so, {@link LoggerPatternLayout} and {@link LoggerLayoutTTCC}
88 * instances automatically retrieve the nested diagnostic
89 * context for the current thread without any user intervention.
90 * Hence, even if a servlet is serving multiple clients
91 * simultaneously, the logs emanating from the same code (belonging to
92 * the same category) can still be distinguished because each client
93 * request will have a different NDC tag.</p>
94 *
95 * 
96 * @author  Marco Vassura
97 * @version $Revision: 635069 $
98 * @package log4php
99 * @since 0.3
100 */
101class LoggerNDC {
102
103    /**
104     * Clear any nested diagnostic information if any. This method is
105     * useful in cases where the same thread can be potentially used
106     * over and over in different unrelated contexts.
107     *
108     * <p>This method is equivalent to calling the {@link setMaxDepth()}
109     * method with a zero <var>maxDepth</var> argument.
110     *
111     * @static 
112     */
113    public static function clear()
114    {
115        LoggerLog::debug("LoggerNDC::clear()");
116       
117        $GLOBALS['log4php.LoggerNDC.ht'] = array();
118    }
119
120    /**
121     * Never use this method directly, use the {@link LoggerLoggingEvent::getNDC()} method instead.
122     * @static
123     * @return array
124     */
125    public static function get()
126    {
127        LoggerLog::debug("LoggerNDC::get()");
128   
129        return $GLOBALS['log4php.LoggerNDC.ht'];
130    }
131 
132    /**
133     * Get the current nesting depth of this diagnostic context.
134     *
135     * @see setMaxDepth()
136     * @return integer
137     * @static
138     */
139    public static function getDepth()
140    {
141        LoggerLog::debug("LoggerNDC::getDepth()");
142   
143        return sizeof($GLOBALS['log4php.LoggerNDC.ht']);     
144    }
145
146    /**
147     * Clients should call this method before leaving a diagnostic
148     * context.
149     *
150     * <p>The returned value is the value that was pushed last. If no
151     * context is available, then the empty string "" is returned.</p>
152     *
153     * @return string The innermost diagnostic context.
154     * @static
155     */
156    public static function pop()
157    {
158        LoggerLog::debug("LoggerNDC::pop()");
159   
160        if (sizeof($GLOBALS['log4php.LoggerNDC.ht']) > 0) {
161            return array_pop($GLOBALS['log4php.LoggerNDC.ht']);
162        } else {
163            return '';
164        }
165    }
166
167    /**
168     * Looks at the last diagnostic context at the top of this NDC
169     * without removing it.
170     *
171     * <p>The returned value is the value that was pushed last. If no
172     * context is available, then the empty string "" is returned.</p>
173     * @return string The innermost diagnostic context.
174     * @static
175     */
176    public static function peek()
177    {
178        LoggerLog::debug("LoggerNDC::peek()");
179   
180        if (sizeof($GLOBALS['log4php.LoggerNDC.ht']) > 0) {
181            return end($GLOBALS['log4php.LoggerNDC.ht']);
182        } else {
183            return '';
184        }
185    }
186 
187    /**
188     * Push new diagnostic context information for the current thread.
189     *
190     * <p>The contents of the <var>message</var> parameter is
191     * determined solely by the client.
192     * 
193     * @param string $message The new diagnostic context information.
194     * @static 
195     */
196    public static function push($message)
197    {
198        LoggerLog::debug("LoggerNDC::push()");
199   
200        array_push($GLOBALS['log4php.LoggerNDC.ht'], (string)$message);
201    }
202
203    /**
204     * Remove the diagnostic context for this thread.
205     * @static
206     */
207    public static function remove()
208    {
209        LoggerLog::debug("LoggerNDC::remove()");
210   
211        LoggerNDC::clear();
212    }
213
214    /**
215     * Set maximum depth of this diagnostic context. If the current
216     * depth is smaller or equal to <var>maxDepth</var>, then no
217     * action is taken.
218     *
219     * <p>This method is a convenient alternative to multiple
220     * {@link pop()} calls. Moreover, it is often the case that at
221     * the end of complex call sequences, the depth of the NDC is
222     * unpredictable. The {@link setMaxDepth()} method circumvents
223     * this problem.
224     *
225     * @param integer $maxDepth
226     * @see getDepth()
227     * @static
228     */
229    public static function setMaxDepth($maxDepth)
230    {
231        LoggerLog::debug("LoggerNDC::setMaxDepth() maxDepth='$maxDepth'");
232   
233        $maxDepth = (int)$maxDepth;
234        if ($maxDepth <= LOGGER_NDC_HT_SIZE) {
235            if (LoggerNDC::getDepth() > $maxDepth) {
236                $GLOBALS['log4php.LoggerNDC.ht'] = array_slice($GLOBALS['log4php.LoggerNDC.ht'], $maxDepth);
237            }
238            $GLOBALS['log4php.LoggerNDC.maxDepth'] = $maxDepth;           
239        }
240    }
241 
242}
Note: See TracBrowser for help on using the repository browser.