      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: 1379729 $
     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:         $this->level = $level;
     69:         $this->levelStr = $levelStr;
     70:         $this->syslogEquivalent = $syslogEquivalent;
     71:     }
     72:
     73:     /**
     74:      * Compares two logger levels.
     75:      *
     76:      * @param LoggerLevels $other
     77:      * @return boolean
     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:      * Returns an Off Level
     91:      * @return LoggerLevel
     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:      * Returns a Fatal Level
    102:      * @return LoggerLevel
    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:      * Returns an Error Level
    113:      * @return LoggerLevel
    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:      * Returns a Warn Level
    124:      * @return LoggerLevel
    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:      * Returns an Info Level
    135:      * @return LoggerLevel
    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:      * Returns a Debug Level
    146:      * @return LoggerLevel
    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:      * Returns a Trace Level
    157:      * @return LoggerLevel
    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:      * Returns an All Level
    168:      * @return LoggerLevel
    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:      * Return the syslog equivalent of this level as an integer.
    179:      * @return integer
    180:      */
    181:     public function getSyslogEquivalent() {
    182:         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:         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:         return $this->levelStr;
    203:     }
    204:
    205:     /**
    206:      * Returns the string representation of this level.
    207:      * @return string
    208:      */
    209:     public function __toString() {
    210:         return $this->toString();
    211:     }
    212:
    213:     /**
    214:      * Returns the integer representation of this level.
    215:      * @return integer
    216:      */
    217:     public function toInt() {
    218:         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:         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: