LoggerLevel.php
6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package log4php
*/
/**
* Defines the minimum set of levels recognized by the system, that is
* <i>OFF</i>, <i>FATAL</i>, <i>ERROR</i>,
* <i>WARN</i>, <i>INFO</i>, <i>DEBUG</i> and
* <i>ALL</i>.
*
* <p>The <i>LoggerLevel</i> class may be subclassed to define a larger
* level set.</p>
*
* @version $Revision: 884719 $
* @package log4php
* @since 0.5
*/
class LoggerLevel {
const OFF = 2147483647;
const FATAL = 50000;
const ERROR = 40000;
const WARN = 30000;
const INFO = 20000;
const DEBUG = 10000;
const ALL = -2147483647;
/**
* TODO: check if still necessary or to be refactored
* @var integer
*/
private $level;
/**
* Contains a list of instantiated levels
*/
private static $levelMap;
/**
* @var string
*/
private $levelStr;
/**
* @var integer
*/
private $syslogEquivalent;
/**
* Constructor
*
* @param integer $level
* @param string $levelStr
* @param integer $syslogEquivalent
*/
private function __construct($level, $levelStr, $syslogEquivalent) {
$this->level = $level;
$this->levelStr = $levelStr;
$this->syslogEquivalent = $syslogEquivalent;
}
/**
* Two priorities are equal if their level fields are equal.
*
* @param object $o
* @return boolean
*/
public function equals($o) {
if($o instanceof LoggerLevel) {
if($this->level == $o->level) {
return true;
}
} else {
return false;
}
}
/**
* Returns an Off Level
* @static
* @return LoggerLevel
*/
public static function getLevelOff() {
if(!isset(self::$levelMap[LoggerLevel::OFF])) {
self::$levelMap[LoggerLevel::OFF] = new LoggerLevel(LoggerLevel::OFF, 'OFF', 0);
}
return self::$levelMap[LoggerLevel::OFF];
}
/**
* Returns a Fatal Level
* @static
* @return LoggerLevel
*/
public static function getLevelFatal() {
if(!isset(self::$levelMap[LoggerLevel::FATAL])) {
self::$levelMap[LoggerLevel::FATAL] = new LoggerLevel(LoggerLevel::FATAL, 'FATAL', 0);
}
return self::$levelMap[LoggerLevel::FATAL];
}
/**
* Returns an Error Level
* @static
* @return LoggerLevel
*/
public static function getLevelError() {
if(!isset(self::$levelMap[LoggerLevel::ERROR])) {
self::$levelMap[LoggerLevel::ERROR] = new LoggerLevel(LoggerLevel::ERROR, 'ERROR', 3);
}
return self::$levelMap[LoggerLevel::ERROR];
}
/**
* Returns a Warn Level
* @static
* @return LoggerLevel
*/
public static function getLevelWarn() {
if(!isset(self::$levelMap[LoggerLevel::WARN])) {
self::$levelMap[LoggerLevel::WARN] = new LoggerLevel(LoggerLevel::WARN, 'WARN', 4);
}
return self::$levelMap[LoggerLevel::WARN];
}
/**
* Returns an Info Level
* @static
* @return LoggerLevel
*/
public static function getLevelInfo() {
if(!isset(self::$levelMap[LoggerLevel::INFO])) {
self::$levelMap[LoggerLevel::INFO] = new LoggerLevel(LoggerLevel::INFO, 'INFO', 6);
}
return self::$levelMap[LoggerLevel::INFO];
}
/**
* Returns a Debug Level
* @static
* @return LoggerLevel
*/
public static function getLevelDebug() {
if(!isset(self::$levelMap[LoggerLevel::DEBUG])) {
self::$levelMap[LoggerLevel::DEBUG] = new LoggerLevel(LoggerLevel::DEBUG, 'DEBUG', 7);
}
return self::$levelMap[LoggerLevel::DEBUG];
}
/**
* Returns an All Level
* @static
* @return LoggerLevel
*/
public static function getLevelAll() {
if(!isset(self::$levelMap[LoggerLevel::ALL])) {
self::$levelMap[LoggerLevel::ALL] = new LoggerLevel(LoggerLevel::ALL, 'ALL', 7);
}
return self::$levelMap[LoggerLevel::ALL];
}
/**
* Return the syslog equivalent of this priority as an integer.
* @final
* @return integer
*/
public function getSyslogEquivalent() {
return $this->syslogEquivalent;
}
/**
* Returns <i>true</i> if this level has a higher or equal
* level than the level passed as argument, <i>false</i>
* otherwise.
*
* <p>You should think twice before overriding the default
* implementation of <i>isGreaterOrEqual</i> method.
*
* @param LoggerLevel $r
* @return boolean
*/
public function isGreaterOrEqual($r) {
return $this->level >= $r->level;
}
/**
* Returns the string representation of this priority.
* @return string
* @final
*/
public function toString() {
return $this->levelStr;
}
/**
* Returns the integer representation of this level.
* @return integer
*/
public function toInt() {
return $this->level;
}
/**
* Convert the string passed as argument to a level. If the
* conversion fails, then this method returns a DEBUG Level.
*
* @param mixed $arg
* @param LoggerLevel $default
* @static
*/
public static function toLevel($arg, $defaultLevel = null) {
if($defaultLevel === null) {
return self::toLevel($arg, self::getLevelDebug());
} else {
if(is_int($arg)) {
switch($arg) {
case self::ALL: return self::getLevelAll();
case self::DEBUG: return self::getLevelDebug();
case self::INFO: return self::getLevelInfo();
case self::WARN: return self::getLevelWarn();
case self::ERROR: return self::getLevelError();
case self::FATAL: return self::getLevelFatal();
case self::OFF: return self::getLevelOff();
default: return $defaultLevel;
}
} else {
switch(strtoupper($arg)) {
case 'ALL': return self::getLevelAll();
case 'DEBUG': return self::getLevelDebug();
case 'INFO': return self::getLevelInfo();
case 'WARN': return self::getLevelWarn();
case 'ERROR': return self::getLevelError();
case 'FATAL': return self::getLevelFatal();
case 'OFF': return self::getLevelOff();
default: return $defaultLevel;
}
}
}
}
}