/*
 * exceptions.js: Plugin responsible for logging all uncaughtExceptions in a flatiron App.
 *
 * (C) 2011, Nodejitsu Inc.
 * MIT LICENSE
 *
 */

var winston = require('winston'),
    common = require('../common');

var exceptions = exports;

//
// Setup default state for the exceptions plugin
//
exceptions.name       = 'exceptions';
exceptions.initalized = false;

var defaultConfig = exceptions.defaultConfig = {
  console: {
    colorize: false,
    json: true,
    level: 'silly'
  }
};

exceptions.init = function (app, options, callback) {
  if (!callback && typeof options === 'function') {
    callback = options;
    options = {};
  }
  
  callback = callback || function () { };
  options  = options  || {};
  
  if (app.config) {
    options = common.mixin({}, options, app.config.get('exceptions') || {});
  }
  
  if (exceptions.initalized) {
    return callback();
  }

  var exceptionHandlers = [];

  //
  // Create the exceptionHandlers defaulting to Console and Loggly.
  //
  exceptionHandlers.push(new winston.transports.Console(options.console || defaultConfig.console));
  
  //
  // Only add the Loggly transport if the input token is 
  // in the options passed to 'init' 
  //
  if (options.loggly && options.loggly.inputs['exceptions']) {
    exceptionHandlers.push(new winston.transports.Loggly({
      json:       options.loggly.json !== false,
      inputToken: options.loggly.inputs['exceptions'],
      level:      options.loggly.level || null,
      subdomain:  options.loggly.subdomain      
    }));
  }
  
  //
  // Update the state of the plugin with the logger.
  //
  exceptions.logger = new winston.Logger({ exceptionHandlers: exceptionHandlers });
  exceptions.initalized = true;
  
  //
  // Have the logger handle uncaught exceptions.
  //
  exceptions.logger.handleExceptions();
  callback();
};
@@ -1099,118 +1099,145 @@
  %0A  
-//%0A  // Only add the Loggly transport if the input token is %0A  // in the options passed to 'init' %0A  //%0A  if (
+Object.keys(options).forEach(function (name) %7B%0A    if (name === 'console') %7B%0A      return;%0A    %7D%0A    else if (name === 'loggly') %7B%0A      
 opti
@@ -1246,19 +1246,23 @@
 s.loggly
- &&
+.json =
  options
@@ -1273,31 +1273,34 @@
 gly.
-inputs%5B'exceptions'%5D) %7B
+json !== false;%0A    %7D%0A    
 %0A   
@@ -1319,32 +1319,33 @@
 ndlers.push(new 
+(
 winston.transpor
@@ -1350,231 +1350,57 @@
 orts
-.Loggly(%7B%0A      json:       options.loggly.json !== false,%0A      inputToken: options.loggly.inputs%5B'exceptions'%5D,%0A      level:      options.loggly.level %7C%7C null,%0A      subdomain:  options.loggly.subdomain      %0A    %7D
+%5Bcommon.capitalize(name)%5D)(options%5Bname%5D
 ));%0A  %7D
+);
 %0A  %0A

Make transports added to exceptions plugin more flexible