Permalink
Browse files

adding ability to silence log output

- Added a method `silence(<bool>)` to log module
- Wrapped log methods, `info`, `warn`, `error` and `sql` in conditional
  which disables logging if `this.isSilent === true`, unless
  `global.verbose === true`. Verbosity wins!
  • Loading branch information...
1 parent eaecab4 commit b4ea1313a64d2783c5eff92e605f3208f7a98871 @noazark noazark committed Sep 27, 2013
Showing with 26 additions and 5 deletions.
  1. +26 −5 lib/log.js
View
@@ -1,8 +1,28 @@
-exports.info = console.info.bind(console, '[INFO]');
-exports.warn = console.warn.bind(console, '[WARN]');
-exports.error = console.error.bind(console, '[ERROR]');
+this.isSilent = false
+
+exports.silence = function (isSilent) {
+ return this.isSilent = isSilent;
+};
+exports.info = function () {
+ if (!this.isSilent || global.verbose) {
+ Array.prototype.unshift.call(arguments, '[INFO]');
+ console.info.apply(console, arguments);
+ }
+};
+exports.warn = function () {
+ if (!this.isSilent || global.verbose) {
+ var args = Array.prototype.unshift.call(arguments, '[WARN]');
+ console.warn.apply(console, arguments);
+ }
+};
+exports.error = function () {
+ if (!this.isSilent || global.verbose) {
+ var args = Array.prototype.unshift.call(arguments, '[ERROR]');
+ console.error.apply(console, arguments);
+ }
+};
exports.sql = function(sql) {
- if (global.dryRun || global.verbose) {
+ if (!this.isSilent && (global.dryRun || global.verbose)) {
var args = Array.prototype.slice.call(arguments).slice(1);
args = args.slice(0, args.length - 1);
if(global.verbose) {
@@ -23,6 +43,7 @@ exports.sql = function(sql) {
};
exports.verbose = function() {
if (global.verbose) {
- console.log.bind(console, '[INFO]').apply(console, arguments);
+ Array.prototype.unshift.call(arguments, '[INFO]');
+ console.log.apply(console, arguments);
}
};

0 comments on commit b4ea131

Please sign in to comment.