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 | */ |
---|
25 | if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__)); |
---|
26 | |
---|
27 | /** |
---|
28 | */ |
---|
29 | require_once(LOG4PHP_DIR . '/LoggerLog.php'); |
---|
30 | |
---|
31 | define('LOG4PHP_LEVEL_OFF_INT', 2147483647); |
---|
32 | define('LOG4PHP_LEVEL_FATAL_INT', 50000); |
---|
33 | define('LOG4PHP_LEVEL_ERROR_INT', 40000); |
---|
34 | define('LOG4PHP_LEVEL_WARN_INT', 30000); |
---|
35 | define('LOG4PHP_LEVEL_INFO_INT', 20000); |
---|
36 | define('LOG4PHP_LEVEL_DEBUG_INT', 10000); |
---|
37 | define('LOG4PHP_LEVEL_ALL_INT', -2147483647); |
---|
38 | |
---|
39 | /** |
---|
40 | * Defines the minimum set of levels recognized by the system, that is |
---|
41 | * <i>OFF</i>, <i>FATAL</i>, <i>ERROR</i>, |
---|
42 | * <i>WARN</i>, <i>INFO</i, <i>DEBUG</i> and |
---|
43 | * <i>ALL</i>. |
---|
44 | * |
---|
45 | * <p>The <i>LoggerLevel</i> class may be subclassed to define a larger |
---|
46 | * level set.</p> |
---|
47 | * |
---|
48 | * @author Marco Vassura |
---|
49 | * @version $Revision: 635069 $ |
---|
50 | * @package log4php |
---|
51 | * @since 0.5 |
---|
52 | */ |
---|
53 | class LoggerLevel { |
---|
54 | |
---|
55 | /** |
---|
56 | * @var integer |
---|
57 | */ |
---|
58 | public $level; |
---|
59 | |
---|
60 | /** |
---|
61 | * @var string |
---|
62 | */ |
---|
63 | public $levelStr; |
---|
64 | |
---|
65 | /** |
---|
66 | * @var integer |
---|
67 | */ |
---|
68 | public $syslogEquivalent; |
---|
69 | |
---|
70 | /** |
---|
71 | * Constructor |
---|
72 | * |
---|
73 | * @param integer $level |
---|
74 | * @param string $levelStr |
---|
75 | * @param integer $syslogEquivalent |
---|
76 | */ |
---|
77 | public function __construct($level, $levelStr, $syslogEquivalent) |
---|
78 | { |
---|
79 | $this->level = $level; |
---|
80 | $this->levelStr = $levelStr; |
---|
81 | $this->syslogEquivalent = $syslogEquivalent; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Two priorities are equal if their level fields are equal. |
---|
86 | * |
---|
87 | * @param object $o |
---|
88 | * @return boolean |
---|
89 | */ |
---|
90 | public function equals($o) |
---|
91 | { |
---|
92 | if ($o instanceof LoggerLevel) { |
---|
93 | return ($this->level == $o->level); |
---|
94 | } else { |
---|
95 | return false; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Returns an Off Level |
---|
101 | * @static |
---|
102 | * @return LoggerLevel |
---|
103 | */ |
---|
104 | public static function getLevelOff() |
---|
105 | { |
---|
106 | static $level; |
---|
107 | if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_OFF_INT, 'OFF', 0); |
---|
108 | return $level; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Returns a Fatal Level |
---|
113 | * @static |
---|
114 | * @return LoggerLevel |
---|
115 | */ |
---|
116 | public static function getLevelFatal() |
---|
117 | { |
---|
118 | static $level; |
---|
119 | if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_FATAL_INT, 'FATAL', 0); |
---|
120 | return $level; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Returns an Error Level |
---|
125 | * @static |
---|
126 | * @return LoggerLevel |
---|
127 | */ |
---|
128 | public static function getLevelError() |
---|
129 | { |
---|
130 | static $level; |
---|
131 | if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_ERROR_INT, 'ERROR', 3); |
---|
132 | return $level; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Returns a Warn Level |
---|
137 | * @static |
---|
138 | * @return LoggerLevel |
---|
139 | */ |
---|
140 | public static function getLevelWarn() |
---|
141 | { |
---|
142 | static $level; |
---|
143 | if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_WARN_INT, 'WARN', 4); |
---|
144 | return $level; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * Returns an Info Level |
---|
149 | * @static |
---|
150 | * @return LoggerLevel |
---|
151 | */ |
---|
152 | public static function getLevelInfo() |
---|
153 | { |
---|
154 | static $level; |
---|
155 | if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_INFO_INT, 'INFO', 6); |
---|
156 | return $level; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * Returns a Debug Level |
---|
161 | * @static |
---|
162 | * @return LoggerLevel |
---|
163 | */ |
---|
164 | public static function getLevelDebug() |
---|
165 | { |
---|
166 | static $level; |
---|
167 | if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_DEBUG_INT, 'DEBUG', 7); |
---|
168 | return $level; |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * Returns an All Level |
---|
173 | * @static |
---|
174 | * @return LoggerLevel |
---|
175 | */ |
---|
176 | public static function getLevelAll() |
---|
177 | { |
---|
178 | static $level; |
---|
179 | if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_ALL_INT, 'ALL', 7); |
---|
180 | return $level; |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * Return the syslog equivalent of this priority as an integer. |
---|
185 | * @final |
---|
186 | * @return integer |
---|
187 | */ |
---|
188 | public function getSyslogEquivalent() |
---|
189 | { |
---|
190 | return $this->syslogEquivalent; |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * Returns <i>true</i> if this level has a higher or equal |
---|
195 | * level than the level passed as argument, <i>false</i> |
---|
196 | * otherwise. |
---|
197 | * |
---|
198 | * <p>You should think twice before overriding the default |
---|
199 | * implementation of <i>isGreaterOrEqual</i> method. |
---|
200 | * |
---|
201 | * @param LoggerLevel $r |
---|
202 | * @return boolean |
---|
203 | */ |
---|
204 | public function isGreaterOrEqual($r) |
---|
205 | { |
---|
206 | return $this->level >= $r->level; |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * Returns the string representation of this priority. |
---|
211 | * @return string |
---|
212 | * @final |
---|
213 | */ |
---|
214 | public function toString() |
---|
215 | { |
---|
216 | return $this->levelStr; |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * Returns the integer representation of this level. |
---|
221 | * @return integer |
---|
222 | */ |
---|
223 | public function toInt() |
---|
224 | { |
---|
225 | return $this->level; |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * Convert the string passed as argument to a level. If the |
---|
230 | * conversion fails, then this method returns a DEBUG Level. |
---|
231 | * |
---|
232 | * @param mixed $arg |
---|
233 | * @param LoggerLevel $default |
---|
234 | * @static |
---|
235 | */ |
---|
236 | public static function toLevel($arg, $defaultLevel = null) |
---|
237 | { |
---|
238 | if ($defaultLevel === null) { |
---|
239 | return self::toLevel($arg, self::getLevelDebug()); |
---|
240 | } else { |
---|
241 | if (is_int($arg)) { |
---|
242 | switch($arg) { |
---|
243 | case LOG4PHP_LEVEL_ALL_INT: return self::getLevelAll(); |
---|
244 | case LOG4PHP_LEVEL_DEBUG_INT: return self::getLevelDebug(); |
---|
245 | case LOG4PHP_LEVEL_INFO_INT: return self::getLevelInfo(); |
---|
246 | case LOG4PHP_LEVEL_WARN_INT: return self::getLevelWarn(); |
---|
247 | case LOG4PHP_LEVEL_ERROR_INT: return self::getLevelError(); |
---|
248 | case LOG4PHP_LEVEL_FATAL_INT: return self::getLevelFatal(); |
---|
249 | case LOG4PHP_LEVEL_OFF_INT: return self::getLevelOff(); |
---|
250 | default: return $defaultLevel; |
---|
251 | } |
---|
252 | } else { |
---|
253 | switch(strtoupper($arg)) { |
---|
254 | case 'ALL': return self::getLevelAll(); |
---|
255 | case 'DEBUG': return self::getLevelDebug(); |
---|
256 | case 'INFO': return self::getLevelInfo(); |
---|
257 | case 'WARN': return self::getLevelWarn(); |
---|
258 | case 'ERROR': return self::getLevelError(); |
---|
259 | case 'FATAL': return self::getLevelFatal(); |
---|
260 | case 'OFF': return self::getLevelOff(); |
---|
261 | default: return $defaultLevel; |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | } |
---|