Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[api] Use RegExp to ignore files so people can use patterns :)

  • Loading branch information...
commit fbd0235be9619b0a1be3d7d736bb9938cdb7af5f 1 parent 9c5174f
@3rd-Eden 3rd-Eden authored
Showing with 39 additions and 11 deletions.
  1. +39 −11 lib/logger.js
View
50 lib/logger.js
@@ -8,7 +8,8 @@
var tty = require('tty')
, colors = require('colors')
, util = require('util')
- , EventEmitter = require('events').EventEmitter;
+ , EventEmitter = require('events').EventEmitter
+ , slice = Array.prototype.slice;
/**
* Strict type checking.
@@ -82,6 +83,14 @@ var methods = {
};
/**
+ * Check if there are's a IGNORE env variable set when the USER ran their
+ * Node.js script. This allows us to ignore a bunch of files at once.
+ *
+ * @type {Array}
+ */
+var IGNORE = (process.env.IGNORE || '').split(/[\s,]+/);
+
+/**
* A easier to understand logger, designed for readablity during development.
* The logs can be stamped and colored based on the evn.
*
@@ -126,15 +135,20 @@ var Logger = module.exports = function devnull(options) {
}
}
- this.transports = [];
- this.calls = 0;
- this.silence = {};
+ this.transports = []; // Array of transports we should log to.
+ this.silencing = []; // Array of regexps to ignore logs for certain files.
+ this.calls = 0; // Small metric on how many logs we have written.
- // do we need to supply a default logging library?
+ // Do we need to supply a default logging library?
if (this.base) {
this.use(require('../transports/stream'));
}
+ // Setup any ignore's that were set using the IGNORE env variable.
+ IGNORE.forEach(function ignore(pattern) {
+ if (pattern) this.ignore(pattern);
+ }.bind(this));
+
// Generate logging methods based on the allowed levels. This allows us to use
// a more readable syntax like: logger.info('hello world'); We are generating
// a new Function here so we reduce scope lookups and get a better call stack
@@ -262,8 +276,14 @@ Logger.prototype.disable = function disable(key) {
* @api public
*/
Logger.prototype.ignore = function ignore(file) {
- this.silence[file] = 1;
+ file = file.replace('*', '.*?');
+
+ var silence = new RegExp('^' + file + '$');
+
+ // Don't add duplicates
+ if (~this.silencing.indexOf(silence)) return this;
+ this.silencing.push(silence);
return this;
};
@@ -273,7 +293,9 @@ Logger.prototype.ignore = function ignore(file) {
* @api public
*/
Logger.prototype.unignore = function unignore(file) {
- if (this.ignoring(file)) delete this.silence[file];
+ this.silencing = this.silencing.filter(function test(re) {
+ return !re.test(file);
+ });
return this;
};
@@ -284,8 +306,14 @@ Logger.prototype.unignore = function unignore(file) {
* @returns {Boolean}
* @api public
*/
-Logger.prototype.ignoring = function ignoring(file) {
- return file in this.silence;
+Logger.prototype.ignoring = function ignoring() {
+ var self = this;
+
+ return slice.call(arguments, 0).some(function ignore(item) {
+ return self.silencing.some(function regexps(re) {
+ return re.test(item);
+ });
+ });
};
/**
@@ -373,8 +401,8 @@ Logger.prototype.write = function write(type, level, stack) {
path = stack[0].getFileName();
filename = path.slice(path.lastIndexOf('/') + 1);
- // Silence, silence! ssht!.
- if (this.silence[filename] || this.silence[path]) return this;
+ // Should we be ignoring this log call?
+ if (this.ignoring(filename, path)) return this;
}
for (; i < length; i++) {
Please sign in to comment.
Something went wrong with that request. Please try again.