Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make logging instance

  • Loading branch information...
commit c73eb416f15671ccbc16f34e123effd06b0f2349 1 parent 363943e
Hsiaoming Yang authored
Showing with 78 additions and 78 deletions.
  1. +3 −1 index.js
  2. +75 −77 lib/logging.js
View
4 index.js
@@ -1,4 +1,6 @@
var color = require('./lib/color');
exports = module.exports = color;
exports.program = require('./lib/program');
-exports.logging = require('./lib/logging');
+var logging = require('./lib/logging');
+exports.logging = logging;
+exports.Logging = logging.Logging;
View
152 lib/logging.js
@@ -9,10 +9,11 @@
var util = require('util');
var os = require('os');
var EventEmitter = require('events').EventEmitter;
-var paint = require('./color').paint;
+var color = require('./color');
+var paint = color.paint;
var count = 0;
-var levels = {
+var _levels = {
'debug': 10,
'info': 20,
'warn': 30,
@@ -20,26 +21,26 @@ var levels = {
'log': 50
};
-var colors = {
- debug: 'grey',
- info: 'green',
- warn: 'yellow',
- error: 'red'
-};
-var icons = {
- logIcon: paint('').cyan.color,
- startIcon: paint('').bold.magenta.color,
- endIcon: paint('').cyan.color
+function colorize(name, text) {
+ var func;
+ if (typeof name === 'string') {
+ func = color.color[name]
+ } else if (typeof name === 'function') {
+ func = name;
+ }
+ if (!func) return text;
+ return func(text);
};
+
function log(context, level, args) {
- if (levels[context.level] > levels[level]) return;
+ if (_levels[context.level] > _levels[level]) return;
var text = '';
var stream = process.stdout;
text += Array(count + 1).join(' ');
if (count) {
- text += icons.logIcon;
+ text += context.icons.logIcon;
}
if (level === 'warn') {
text += paint(' WARN ').yellow_bg.color + ' ';
@@ -47,16 +48,7 @@ function log(context, level, args) {
text += paint(' ERROR ').red_bg.color + ' ';
stream = process.stderr;
}
- var colorize = function(text) {
- var name = colors[level];
- if (!name) return text;
- return paint(text)[name].color;
- }
- if (colorize) {
- text += colorize(util.format.apply(context, args)) + os.EOL;
- } else {
- text += util.format.apply(context, args) + os.EOL;
- }
+ text += colorize(util.format.apply(context, args)) + os.EOL;
stream.write(text);
}
@@ -64,75 +56,81 @@ function Logging(level) {
this.level = level || 'info';
}
Logging.prototype.__proto__ = EventEmitter.prototype;
+Logging.prototype.icons = {
+ logIcon: paint('').cyan.color,
+ startIcon: paint('').bold.magenta.color,
+ endIcon: paint('').cyan.color
+};
+Logging.prototype.colors = {
+ debug: 'grey',
+ info: 'green',
+ warn: 'yellow',
+ error: 'red'
+};
Logging.prototype.start = function() {
- if (levels[this.level] > 25) return;
+ if (_levels[this.level] > 25) return;
- var text = Array(count + 1).join(' ');
- text += icons.startIcon;
- text += paint(util.format.apply(this, arguments)).bold.color + os.EOL;
- process.stdout.write(text);
- count += 1;
- this.emit('logging-start');
+ var text = Array(count + 1).join(' ');
+ text += this.icons.startIcon;
+ text += paint(util.format.apply(this, arguments)).bold.color + os.EOL;
+ process.stdout.write(text);
+ count += 1;
+ this.emit('logging-start');
};
Logging.prototype.end = function() {
- if (levels[this.level] > 25) return;
- var text = '';
- text += Array(count + 1).join(' ');
- if (count) {
- text += icons.endIcon;
- }
- text += util.format.apply(this, arguments) + os.EOL;
- process.stdout.write(text);
- count -= 1;
- this.emit('logging-end');
+ if (_levels[this.level] > 25) return;
+ var text = '';
+ text += Array(count + 1).join(' ');
+ if (count) {
+ text += this.icons.endIcon;
+ }
+ text += util.format.apply(this, arguments) + os.EOL;
+ process.stdout.write(text);
+ count -= 1;
+ this.emit('logging-end');
};
Logging.prototype.log = function() {
- log(this, 'log', arguments);
+ log(this, 'log', arguments);
};
Logging.prototype.debug = function() {
- log(this, 'debug', arguments);
+ log(this, 'debug', arguments);
};
Logging.prototype.info = function() {
- log(this, 'info', arguments);
+ log(this, 'info', arguments);
};
Logging.prototype.warn = function() {
- log(this, 'warn', arguments);
- this.emit('logging-warn');
+ log(this, 'warn', arguments);
+ this.emit('logging-warn');
};
Logging.prototype.error = function() {
- log(this, 'error', arguments);
- this.emit('logging-error');
+ log(this, 'error', arguments);
+ this.emit('logging-error');
};
Logging.prototype.config = function(obj) {
- if (obj.verbose) {
- this.level = 'debug';
- }
- if (obj.quiet) {
- this.level = 'warn';
- }
- if (obj.level) {
- this.level = obj.level;
- }
- if (obj in colors) {
- this.level = obj;
- }
- var key;
- var stylish = function(key) {
- colors[key] = obj.colors[key];
- };
- if (obj.colors) {
- for (key in obj.colors) {
- stylish(key);
- }
- }
- var iconit = function(key) {
- icons[key] = obj.icons[key];
- };
- if (obj.icons) {
- for (key in obj.icons) {
- iconit(key);
- }
- }
+ var self = this;
+ if (obj.verbose) {
+ self.level = 'debug';
+ }
+ if (obj.quiet) {
+ self.level = 'warn';
+ }
+ if (obj.level) {
+ self.level = obj.level;
+ }
+ if (obj in _levels) {
+ self.level = obj;
+ }
+
+ if (obj.colors) {
+ Object.keys(obj.colors).forEach(function(key) {
+ self.colors[key] = obj.colors[key];
+ });
+ }
+ if (obj.icons) {
+ Object.keys(obj.icons).forEach(function(key) {
+ self.icons[key] = obj.icons[key];
+ });
+ }
};
exports = module.exports = new Logging;
Please sign in to comment.
Something went wrong with that request. Please try again.