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