      1 var define = require("../define.js").define, base = require("../base"), fs = require('fs');
      2
      3 var logging, Logger, Level, appenders;
      4
      5 var parseProperties = function(properties) {
      6 	for (var i in properties) {
      7 		var logger = Logger.getLogger(i);
      8 		var props = properties[i], level = props.level, appenderArr = props.appenders;
      9 		if (level) {
     10 			level = Level.toLevel(level);
     11 			if (level) {
     12 				logger.level = level;
     13 			}
     14 		}
     15 		if (appenderArr && base.isArray(appenderArr)) {
     16 			for (var j = appenderArr.length - 1; j >= 0; j--) {
     17 				var appenderProps = appenderArr[j], type = appenderProps.type;
     18 				appenderProps.type = null;
     19 				if (type) {
     20 					var appender;
     21 					switch (type.toLowerCase()) {
     22 						case "consoleappender":
     23 							appender = appenders.ConsoleAppender;
     24 							break;
     25 						case "jsonappender":
     26 							appender = appenders.JSONAppender;
     27 							break;
     28 						case "fileappender":
     29 							appender = appenders.FileAppender;
     30 							break;
     31 						case "rollingfileappender":
     32 							appender = appenders.RollingFileAppender;
     33 							break;
     34 					}
     35 					if (appender) {
     36 						logger.addAppender(new appender(appenderProps));
     37 					}
     38 				}
     39 			}
     40 		}
     41 	}
     42 };
     43
     44 /**
     45  * @class default configurator for logging
     46  *
     47  * @name BasicConfigurator
     48  * @memberOf comb.logging
     49  *
     50  */
     51 var BasicConfigurator = (exports.BasicConfigurator = define(null, {
     52 			instance : {
     53 				/**@lends comb.logging.BasicConfigurator.prototype*/
     54
     55 				constructor : function() {
     56 					if (!Logger) {
     57 						logging = require("./index").logging;
     58 						Logger = logging.Logger;
     59 						Level = logging.Level;
     60 						appenders = logging.appenders;
     61 					}
     62 					this.__configured = false;
     63 				},
     64
     65 				/**
     66 				 * Configure logging.
     67 				 *
     68 				 * @param {comb.logging.Appender} [appender=null] appender to add to the root logger, by default a console logger is added.
     69 				 */
     70 				configure : function(appender) {
     71 					var rootLogger = Logger.getRootLogger();
     72 					if (!appender || !(appender instanceof appenders.Appender) && !this.__configured) {
     73 						rootLogger.addAppender(new appenders.ConsoleAppender());
     74 						this.__configured = true;
     75 					} else {
     76 						rootLogger.addAppender(appender);
     77 					}
     78 				}
     79 			}
     80 		}));
     81
     82 /**
     83  * @class Configures comb.Logger with the properties or properties contained within a file
     84  *
     85  * @example
     86  *
     87  * var propertyConfigurator = new comb.logging.PropertyConfigurator();
     88  *
     89  * propertyConfigurator.configure("/location/of/combLogger.json");
     90  *
     91  * //or
     92  *
     93  * var config = {
     94  *     "my.logger" : {
     95  *         level : "INFO",
     96  *         appenders : [
     97  *             {
     98  *                 //default file appender
     99  *                 type : "FileAppender",
    100  *                 file : "/var/log/myApp.log",
    101  *             },
    102  *             {
    103  *                 //default JSON appender
    104  *                 type : "JSONAppender",
    105  *                 file : "/var/log/myApp.JSON",
    106  *             },
    107  *             {
    108  *                 type : "FileAppender",
    109  *                  //override default patter
    110  *                 pattern : "{[EEEE, MMMM dd, yyyy h:m a]timeStamp} {[5]level}"
    111  *                          + " {[- 5]levelName} {[-20]name} : {message}",
    112  *                 //location of my log file
    113  *                 file : "/var/log/myApp-errors.log",
    114  *                 //override name so it will get added to the log
    115  *                 name : "errorFileAppender",
    116  *                 //overwrite each time
    117  *                 overwrite : true,
    118  *                 //explicity set the appender to only accept errors
    119  *                 level : "ERROR"
    120  *             },
    121  *             {
    122  *                 type : "JSONAppender",
    123  *                 file : "/var/log/myApp-error.json",
    124  *                 //explicity set the appender to only accept errors
    125  *                 level : "ERROR"
    126  *             }
    127  *         ]
    128  *     }
    129  *     //repeat for more loggers
    130  *
    131  *     propertyConfigurator.configure(config);
    132  * }
    133  *
    134  * @name PropertyConfigurator
    135  * @augments comb.logging.BasicConfigurator
    136  * @memberOf comb.logging
    137  *
    138  */
    139 exports.PropertyConfigurator = define(BasicConfigurator, {
    140 			instance : {
    141 			   /**@lends comb.logging.PropertyConfigurator.prototype*/
    142 				/**
    143 				 * Call to configure logging
    144 				 *
    145 				 * @example
    146 				 *
    147 				 * //Example configuration
    148 				 *  {
    149 				 *     "my.logger" : {
    150 				 *         level : "INFO",
    151 				 *         appenders : [
    152 				 *             {
    153 				 *                 //default file appender
    154 				 *                 type : "FileAppender",
    155 				 *                 file : "/var/log/myApp.log",
    156 				 *             },
    157 				 *             {
    158 				 *                 //default JSON appender
    159 				 *                 type : "JSONAppender",
    160 				 *                 file : "/var/log/myApp.JSON",
    161 				 *             },
    162 				 *             {
    163 				 *                 type : "FileAppender",
    164 				 *                  //override default patter
    165 				 *                 pattern : "{[EEEE, MMMM dd, yyyy h:m a]timeStamp} {[5]level}"
    166 				 *                          + " {[- 5]levelName} {[-20]name} : {message}",
    167 				 *                 //location of my log file
    168 				 *                 file : "/var/log/myApp-errors.log",
    169 				 *                 //override name so it will get added to the log
    170 				 *                 name : "errorFileAppender",
    171 				 *                 //overwrite each time
    172 				 *                 overwrite : true,
    173 				 *                 //explicity set the appender to only accept errors
    174 				 *                 level : "ERROR"
    175 				 *             },
    176 				 *             {
    177 				 *                 type : "JSONAppender",
    178 				 *                 file : "/var/log/myApp-error.json",
    179 				 *                 //explicity set the appender to only accept errors
    180 				 *                 level : "ERROR"
    181 				 *             }
    182 				 *         ]
    183 				 *     }
    184 				 *
    185 				 * @param {Object|String} properties Object containing configuration or string containing a file name with the configuration.
    186 				 */
    187 				configure : function(properties) {
    188 					if (base.isObject(properties)) {
    189 						parseProperties(properties);
    190 					} else {
    191 						fs.readFile(properties, function(err, res) {
    192 							if (err) {
    193 								throw err;
    194 							} else {
    195 								try {
    196 									parseProperties(JSON.parse(res));
    197 								} catch(e) {
    198 									throw e;
    199 								}
    200 							}
    201 						})
    202 					}
    203 				}
    204 			}
    205 		});