Permalink
Browse files

Better exception logging

  • Loading branch information...
Loren
Loren committed Dec 20, 2010
1 parent 9e60037 commit b3ffdb72f273d7616c3907fa95a33ed3bcf3b093
Showing with 98 additions and 25 deletions.
  1. +17 −15 README.md
  2. +8 −6 deps.js
  3. +2 −2 lib/module-monitor.js
  4. +32 −2 lib/monitor.js
  5. +19 −0 logger/stderr-logger.js
  6. +20 −0 logger/stdout-logger.js
View
@@ -188,28 +188,30 @@ These configuration parameters can be specified globally, or as defaults
for all monitors in a particular module, or on a monitor by monitor basis
within a module.
-_Global_ - specify them as parameters for the 'monitor' module. Example:
+_Global_ - specify them as defaults for the 'monitor' module. Example:
// production.js - Configurations for the production deployment
module.exports = {
monitor: {
- errorLogger: require('monitor/emailLogger'),
- maxLogSize: 4196
+ defaults: {
+ eventLogger: null,
+ maxLogSize: 4196
+ }
},
...
}
-The above configuration defines global defaults for the errorLogger and
-maxLogSize parameters.
+The above configuration turns off logging for the event logger, and sets the
+maxLogSize parameter for all monitors (unless overridden).
-_Module Defaults_ - specify them as the 'default' monitor for your module.
+_Module Defaults_ - specify them as the 'defaults' monitor for your module.
Example:
// production.js - Configurations for the production deployment
module.exports = {
- 'Customer': {
- 'monitors': {
- 'default': {
+ Customer: {
+ monitor: {
+ defaults: {
errorLogger: require('monitor/emailLogger'),
maxLogSize: 4196
}
@@ -219,7 +221,7 @@ Example:
// Customer.js - Customer module
var config = require('config')('Customer');
- var monitor = require('monitor')('Customer', config.monitors);
+ var monitor = require('monitor')('Customer', config.monitor);
The above configuration defines module-level monitor defaults for *Customer*
@@ -228,9 +230,9 @@ only use the monitor name instead of 'default'. Example:
// production.js - Configurations for the production deployment
module.exports = {
- 'Customer': {
- 'monitors': {
- 'default': {
+ Customer: {
+ monitor: {
+ default: {
errorLogger: null
},
'Customer insertion failure': {
@@ -245,7 +247,7 @@ only use the monitor name instead of 'default'. Example:
// Customer.js - Customer module
var config = require('config')('Customer');
- var monitor = require('monitor')('Customer', config.monitors);
+ var monitor = require('monitor')('Customer', config.monitor);
The above configuration defaults error logging off for monitors within the
*Customer* module. The the *Customer insertion failure* monitor overrides this
@@ -286,7 +288,7 @@ new ModuleMonitor(moduleName, configs) - Constructor
Inputs:
moduleName - Name of your module
configs - A dictionary of monitor configurations for this module
- key: monitorName, or 'default' for the module level defaults
+ key: monitorName, or 'defaults' for the module level defaults
value: A configuration object for the monitor, possibly including:
enabled - (boolean) Should the monitor be enabled? (default: true)
eventLogger - Logger[s] to use for events (default: util.log)
View
14 deps.js
@@ -15,18 +15,20 @@ var deps = module.exports = {
// Node 0.3.0 has util vs. sys
try {
- var out = require('util');
+ deps.out = require('util');
} catch (e) {
- out = require('sys');
+ deps.out = require('sys');
}
// Default module configuration
deps.monitorConfig = deps.config('monitor', {
// These are defaults for all monitors, and can be overridden
// either globally or when creating an individual monitor
- enabled: true,
- eventLogger: out.log,
- errorLogger: out.debug,
- maxLogSize: 10240
+ defaults: {
+ enabled: true,
+ eventLogger: require('./logger/stdout-logger'),
+ errorLogger: require('./logger/stderr-logger'),
+ maxLogSize: 10240
+ }
});
View
@@ -17,7 +17,7 @@ var Monitor = require('./monitor');
* Input:
* modName - Name of your module
* configs - A dictionary of monitor configurations for this module
-* key: monitorName, or 'default' for the module level defaults
+* key: monitorName, or 'defaults' for the module level defaults
* value: A configuration object for the monitor, possibly including:
* enabled - (boolean) Should the monitor be enabled? (default: true)
* eventLogger - Logger[s] to use for events (default: util.log)
@@ -57,7 +57,7 @@ proto.get = function(monitorName) {
if (!monitor) {
// Get the module and monitor level configurations
- var defaultConfig = t.moduleConfig ? t.moduleConfig['default'] : null;
+ var defaultConfig = t.moduleConfig ? t.moduleConfig['defaults'] : null;
var monitorConfig = t.moduleConfig ? t.moduleConfig[monitorName] : null;
monitor = new Monitor(monitorName, t.moduleName, monitorConfig,
defaultConfig);
View
@@ -27,12 +27,30 @@ var _ = deps._;
var monitor = module.exports = function(name, moduleName, config,
moduleConfig) {
+ // TEMP: Defaults used to be stored in the config namespace, and are now
+ // in the config.defaults namespace. Remove this after v3.0.
+ if (deps.monitorConfig.eventLogger || deps.monitorConfig.errorLogger) {
+ deps.sys.debug("WARNING: Moving global configs into defaults object");
+ deps.sys.debug("WARNING: See monitor.README.md for more info.");
+ deps.sys.debug("WARNING: This will be deprecated in v3.0+.");
+ if (deps.monitorConfig.eventLogger)
+ deps.monitorConfig.defaults.eventLogger = deps.monitorConfig.eventLogger;
+ if (deps.monitorConfig.errorLogger)
+ deps.monitorConfig.defaults.errorLogger = deps.monitorConfig.errorLogger;
+ if (deps.monitorConfig.maxLogSize)
+ deps.monitorConfig.defaults.maxLogSize = deps.monitorConfig.maxLogSize;
+ if (!_.isUndefined(deps.monitorConfig.enabled))
+ deps.monitorConfig.defaults.maxLogSize = deps.monitorConfig.maxLogSize;
+ }
+
// Instance variables
var t = this;
- t.config = _.extendDeep({}, deps.monitorConfig, moduleConfig, config);
+ t.config = _.extendDeep({}, deps.monitorConfig.defaults, moduleConfig, config);
t.name = name;
t.loggers = {};
t.moduleName = moduleName;
+
+ // Initialize the logger
t.reset();
};
@@ -302,7 +320,19 @@ proto._getLogMessage = function(value, data) {
var msg = new Date().toFormattedString()
+ ' [' + t.moduleName + '] ' + t.name + ' ';
if (value != 1) {msg += '(' + value + ') ';}
- if (data) msg += _.outStr('', data, t.config.maxLogSize);
+
+ // Add object data
+ if (data) {
+ // Don't format stack traces in exception objects
+ if (data.stack && data.message) {
+ msg += "\n" + data.stack;
+ } else {
+ // Pretty print it
+ msg += _.outStr('', data, t.config.maxLogSize, {prettyPrint:true});
+ }
+ }
+
+ // Return the log message
return msg;
}; // _getLogMessage()
View
@@ -0,0 +1,19 @@
+/*******************************************************************************
+* stderr-logger.js - A simple logger function for sending logs to stderr
+********************************************************************************
+* Input:
+* message - The message to send to stdout
+* value - The monitor value (not used)
+* data - The log data object (not used)
+* monitor - The monitor object (not used)
+*
+* Output:
+* none (except the log message sent to stderr)
+*/
+var deps = require('../deps');
+module.exports = function(message, value, data, monitor) {
+
+ // Output the formatted message
+ deps.out.debug(message);
+
+};
View
@@ -0,0 +1,20 @@
+/*******************************************************************************
+* stdout-logger.js - A simple logger function for sending logs to stdout
+********************************************************************************
+* Input:
+* message - The message to send to stdout
+* value - The monitor value (not used)
+* data - The log data object (not used)
+* monitor - The monitor object (not used)
+*
+* Output:
+* none (except the log message sent to stdout)
+*/
+var deps = require('../deps');
+deps._.out("stdout");
+module.exports = function(message, value, data, monitor) {
+
+ // Output just the message
+ deps.out.puts(message);
+
+};

0 comments on commit b3ffdb7

Please sign in to comment.