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