      1 var define = require("../define.js").define, base = require("../base");
      2
      3 var LEVELS = {
      4 	ALL : -100000,
      5 	DEBUG : 1,
      6 	TRACE : 2,
      7 	INFO : 3,
      8 	WARN :4,
      9 	ERROR : 5,
     10 	FATAL:6,
     11 	OFF : 100000
     12 };
     13
     14 var LEVELS_REVERSE = {
     15 	"-100000" : "ALL",
     16 	"1" : "DEBUG",
     17 	"2" : "TRACE",
     18 	"3" : "INFO",
     19 	"4": "WARN",
     20 	"5" : "ERROR",
     21 	"6" : "FATAL",
     22 	"100000" : "OFF"
     23 };
     24
     25 /**
     26  * @class Level class used to describe logging levels. The levels determine what types of events are logged to the appenders
     27  *  for example the if Level.ALL is used then all event will be logged, however if Level.INFO was used then <b>ONLY</b>
     28  *  INFO, WARN, ERROR, and FATAL events will be logged. To turn off logging for a logger use Level.OFF.
     29  *
     30  * <p><b>Not typically instantiated directly, but through staticly defined levels</b></p>
     31  * @example
     32  * //Levels in ascending order
     33  * comb.logging.Level.ALL
     34  * comb.logging.Level.DEBUG
     35  * comb.logging.Level.TRACE
     36  * comb.logging.Level.INFO
     37  * comb.logging.Level.WARN
     38  * comb.logging.Level.ERROR
     39  * comb.logging.Level.FATAL
     40  * comb.logging.Level.OFF
     41  *
     42  * //or
     43  * Level.getLevel("INFO");
     44  *
     45  * @name Level
     46  * @memberOf comb.logging
     47  *
     48  * @property {Number} level the numerical representation of this level.
     49  * @property {String} name the name of level.
     50  */
     51 var Level = (exports = module.exports = define(null, {
     52 			instance : {
     53 				/**@lends comb.logging.Level.prototype*/
     54
     55 				constructor : function(level, name) {
     56 					this.level = level;
     57 					this.name = name;
     58 				},
     59
     60 				/**
     61 				 * Determing if this level is >= another level
     62 				 * @param {comb.logging.Level} level the level to test against
     63 				 *
     64 				 * @returns {Boolean} true if this is >= false otherwise.
     65 				 */
     66 				isGreaterOrEqualToo : function(level) {
     67 					var ret = false;
     68 					if (level && level instanceof Level) {
     69 						if (this.level >= level.level) {
     70 							ret = true;
     71 						}
     72 					}
     73 					return ret;
     74 				},
     75
     76 				/**
     77 				 * Determing if this level is equal to another level based off of the numerical rank.
     78 				 *
     79 				 * @param {comb.logging.Level} level the level to compare
     80 				 *
     81 				 * @returns {Boolean} true if this is equal to that false otherwise.
     82 				 */
     83 				equals : function(level) {
     84 					return level.level == this.level;
     85 				}
     86 			},
     87 			static : {
     88 				/**@lends comb.logging.Level*/
     89
     90 				/**
     91 				 * Converts a numerical or string representation of a level, if a default level is provided,
     92 				 * then if a level cannot be determined then the default level is used.
     93 				 *
     94 				 * @param {Number|String|comb.logging.Level} level the level to try to convert
     95 				 * @param {comb.logging.Level} [defaultLevel] default level to use if one cannot be determined,
     96 				 *
     97 				 * @returns {comb.logging.Level|null} returns a level if one can be determined null otherwise.
     98 				 */
     99 				toLevel : function(level, defaultLevel) {
    100 					var ret = null;
    101 					var args = base.argsToArray(arguments);
    102 					if (args.length === 1) {
    103 						var level = args[0];
    104 						if (level instanceof Level) {
    105 							ret = level;
    106 						} else if (base.isNumber(level)) {
    107 							var strLevel = LEVELS_REVERSE[level];
    108 							ret = Level[strLevel];
    109 						} else if (base.isString(level)) {
    110 							ret = Level[level.toUpperCase()];
    111 						}
    112 					} else {
    113 						ret = (Level.toLevel(args[0]) || args[1]);
    114 					}
    115 					return ret;
    116 				},
    117
    118                 /**
    119                  * Adds a new level to the Level object.
    120                  *
    121                  * @example
    122                  *
    123                  * logger = Logger.getLogger("my.logger");
    124                  *
    125                  * //create the custom level
    126                  * Level.addLevel("custom_Level", 20);
    127                  *
    128                  * //now set the level on a logger
    129                  * logger.level = Level.CUSTOM_LEVEL;
    130                  *
    131                  * @param {string} label the label of the level, <b>Note:</b> the label will be coverted to uppercase.
    132                  * @param {number} level the level of the level
    133                  *
    134                  * @return {undefined|comb.logging.Level} the level that was created.
    135                  *
    136                  */
    137                 addLevel : function(label, level){
    138                     var ret;
    139                     if(base.isString(label) && base.isNumber(level)){
    140                         label = label.toUpperCase();
    141                         LEVELS_REVERSE[level] = label;
    142                         LEVELS[label] = level;
    143                         ret = (this[label] = new Level(level, label));
    144                     }
    145                     return ret;
    146                 },
    147
    148 				/**
    149 				 * Level to allow logging of all events.
    150 				 */
    151 				ALL : null,
    152 				/**
    153 				 * Logs only events debug or greater.
    154 				 */
    155 				DEBUG : null,
    156 				/**
    157 				 * Like debug but provides a finer level of detail
    158 				 */
    159 				TRACE : null,
    160 				/**
    161 				 * Only info, or error related events
    162 				 */
    163 				INFO : null,
    164 				/**
    165 				 * Only warn or error related events
    166 				 */
    167 				WARN : null,
    168 				/**
    169 				 * Error or fatal events
    170 				 */
    171 				ERROR : null,
    172 				/**
    173 				 * Only fatal events
    174 				 */
    175 				FATAL : null,
    176 				/**
    177 				 * No events will be logged.
    178 				 */
    179 				OFF : null
    180
    181 			}
    182 		}));
    183
    184 for (var i in LEVELS_REVERSE) {
    185 	Level[LEVELS_REVERSE[i]] = new Level(parseInt(i, 10), LEVELS_REVERSE[i]);
    186 }