1: <?php
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: 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:
46: private $level;
47:
48:
49: private static $levelMap;
50:
51:
52: private $levelStr;
53:
54: 55: 56: 57:
58: private $syslogEquivalent;
59:
60: 61: 62: 63: 64: 65: 66:
67: private function __construct($level, $levelStr, $syslogEquivalent) {
68: $this->level = $level;
69: $this->levelStr = $levelStr;
70: $this->syslogEquivalent = $syslogEquivalent;
71: }
72:
73: 74: 75: 76: 77: 78:
79: public function equals($other) {
80: if($other instanceof LoggerLevel) {
81: if($this->level == $other->level) {
82: return true;
83: }
84: } else {
85: return false;
86: }
87: }
88:
89: 90: 91: 92:
93: public static function getLevelOff() {
94: if(!isset(self::$levelMap[LoggerLevel::OFF])) {
95: self::$levelMap[LoggerLevel::OFF] = new LoggerLevel(LoggerLevel::OFF, 'OFF', LOG_ALERT);
96: }
97: return self::$levelMap[LoggerLevel::OFF];
98: }
99:
100: 101: 102: 103:
104: public static function getLevelFatal() {
105: if(!isset(self::$levelMap[LoggerLevel::FATAL])) {
106: self::$levelMap[LoggerLevel::FATAL] = new LoggerLevel(LoggerLevel::FATAL, 'FATAL', LOG_ALERT);
107: }
108: return self::$levelMap[LoggerLevel::FATAL];
109: }
110:
111: 112: 113: 114:
115: public static function getLevelError() {
116: if(!isset(self::$levelMap[LoggerLevel::ERROR])) {
117: self::$levelMap[LoggerLevel::ERROR] = new LoggerLevel(LoggerLevel::ERROR, 'ERROR', LOG_ERR);
118: }
119: return self::$levelMap[LoggerLevel::ERROR];
120: }
121:
122: 123: 124: 125:
126: public static function getLevelWarn() {
127: if(!isset(self::$levelMap[LoggerLevel::WARN])) {
128: self::$levelMap[LoggerLevel::WARN] = new LoggerLevel(LoggerLevel::WARN, 'WARN', LOG_WARNING);
129: }
130: return self::$levelMap[LoggerLevel::WARN];
131: }
132:
133: 134: 135: 136:
137: public static function getLevelInfo() {
138: if(!isset(self::$levelMap[LoggerLevel::INFO])) {
139: self::$levelMap[LoggerLevel::INFO] = new LoggerLevel(LoggerLevel::INFO, 'INFO', LOG_INFO);
140: }
141: return self::$levelMap[LoggerLevel::INFO];
142: }
143:
144: 145: 146: 147:
148: public static function getLevelDebug() {
149: if(!isset(self::$levelMap[LoggerLevel::DEBUG])) {
150: self::$levelMap[LoggerLevel::DEBUG] = new LoggerLevel(LoggerLevel::DEBUG, 'DEBUG', LOG_DEBUG);
151: }
152: return self::$levelMap[LoggerLevel::DEBUG];
153: }
154:
155: 156: 157: 158:
159: public static function getLevelTrace() {
160: if(!isset(self::$levelMap[LoggerLevel::TRACE])) {
161: self::$levelMap[LoggerLevel::TRACE] = new LoggerLevel(LoggerLevel::TRACE, 'TRACE', LOG_DEBUG);
162: }
163: return self::$levelMap[LoggerLevel::TRACE];
164: }
165:
166: 167: 168: 169:
170: public static function getLevelAll() {
171: if(!isset(self::$levelMap[LoggerLevel::ALL])) {
172: self::$levelMap[LoggerLevel::ALL] = new LoggerLevel(LoggerLevel::ALL, 'ALL', LOG_DEBUG);
173: }
174: return self::$levelMap[LoggerLevel::ALL];
175: }
176:
177: 178: 179: 180:
181: public function getSyslogEquivalent() {
182: return $this->syslogEquivalent;
183: }
184:
185: 186: 187: 188: 189: 190: 191: 192:
193: public function isGreaterOrEqual($other) {
194: return $this->level >= $other->level;
195: }
196:
197: 198: 199: 200:
201: public function toString() {
202: return $this->levelStr;
203: }
204:
205: 206: 207: 208:
209: public function __toString() {
210: return $this->toString();
211: }
212:
213: 214: 215: 216:
217: public function toInt() {
218: return $this->level;
219: }
220:
221: 222: 223: 224: 225: 226: 227: 228:
229: public static function toLevel($arg, $defaultLevel = null) {
230: if(is_int($arg)) {
231: switch($arg) {
232: case self::ALL: return self::getLevelAll();
233: case self::TRACE: return self::getLevelTrace();
234: case self::DEBUG: return self::getLevelDebug();
235: case self::INFO: return self::getLevelInfo();
236: case self::WARN: return self::getLevelWarn();
237: case self::ERROR: return self::getLevelError();
238: case self::FATAL: return self::getLevelFatal();
239: case self::OFF: return self::getLevelOff();
240: default: return $defaultLevel;
241: }
242: } else {
243: switch(strtoupper($arg)) {
244: case 'ALL': return self::getLevelAll();
245: case 'TRACE': return self::getLevelTrace();
246: case 'DEBUG': return self::getLevelDebug();
247: case 'INFO': return self::getLevelInfo();
248: case 'WARN': return self::getLevelWarn();
249: case 'ERROR': return self::getLevelError();
250: case 'FATAL': return self::getLevelFatal();
251: case 'OFF': return self::getLevelOff();
252: default: return $defaultLevel;
253: }
254: }
255: }
256: }
257: