Permalink
Browse files

Support display level and custom display level

  • Loading branch information...
1 parent 8fbd3fd commit 16c5caa93272e3148dff148f96b4440fbed95c89 @firejune committed Jan 16, 2012
Showing with 97 additions and 22 deletions.
  1. +20 −1 README.md
  2. +59 −20 lib/clog.js
  3. +1 −1 package.json
  4. +17 −0 test.js
View
21 README.md
@@ -4,6 +4,7 @@ Colorful console output for your applications in NodeJS.
* Colors for log, info, warn and error
* Support custom labels
+* Support display level and custom level
* Tiny library
* Easy to use
@@ -17,7 +18,7 @@ NPM
$ npm install clog
-### Code
+### Usage
var clog = require('clog');
@@ -31,6 +32,24 @@ NPM
'Content-Type': 'text/javascript'
});
+### Configure
+
+ // display level configration:
+ clog.configure({'log level': 2});
+ //=> {'log': true, 'info': true, 'warn': false, 'error': false, 'debug': false}
+
+ // custom display configration:
+ clog.configure({
+ 'log level': {
+ 'log': true,
+ 'info': true,
+ 'warn': false,
+ 'error': true,
+ 'debug': false
+ }
+ });
+ //=> {'log': true, 'info': true, 'warn': false, 'error': true, 'debug': false}
+
### Output
![output](https://github.com/firejune/clog/raw/master/images/clog.png)
View
79 lib/clog.js
@@ -9,16 +9,28 @@
/**
* Object to array
*/
-
-var toArray = function (enu) {
+
+function toArray(enu) {
var arr = [];
for (var i = 0, l = enu.length; i < l; i++)
arr.push(enu[i]);
return arr;
-};
+}
+
+function extendObject(destination, source) {
+ for (var property in source)
+ destination[property] = source[property];
+
+ return destination;
+}
+
+/**
+ * Clog version.
+ */
+var version = '0.1.2'
/**
* Log types and colors.
@@ -33,28 +45,27 @@ var toArray = function (enu) {
* Light Gray 37
*
*/
+ , types = {
+ 'log' : 0
+ , 'error' : 31
+ , 'warn' : 33
+ , 'info' : 36
+ , 'debug' : 90
+ }
-var types = {
- 'log' : 0
- , 'error' : 31
- , 'warn' : 33
- , 'info' : 36
- , 'debug' : 90
-};
-
+ , config = {
+ 'log': true
+ , 'info': true
+ , 'warn': true
+ , 'error': true
+ , 'debug': true
+ };
/**
* Clog class.
*/
var Clog = function () {
-
- /**
- * Clog version.
- */
-
- this.log.version = '0.0.2';
-
/**
* Generate methods.
*/
@@ -68,25 +79,53 @@ var Clog = function () {
})(name);
}
+ this.log.configure = function (settings) {
+ return self.configure.apply(self, [settings]);
+ };
+
return this.log;
};
/**
+ * Configure method.
+ */
+
+Clog.prototype.configure = function (settings) {
+ var level
+ , index = 0;
+
+ if (!settings || !(level = settings['log level']))
+ return 'WTF?';
+
+ if ('object' == typeof level)
+ extendObject(config, level);
+
+ if ('number' == typeof level)
+ for (var property in config) {
+ config[property] = index < level;
+ index++;
+ }
+
+ return config;
+};
+
+
+/**
* Log method.
*/
Clog.prototype.log = function (type) {
- console.log.apply(
+ config[type] !== false && console.log.apply(
console
, [types[type]
? '\033[' + types[type] + 'm' + type + ':\033[39m'
: type + ':'
].concat(toArray(arguments).slice(1))
);
- return this;
+ return config[type];
};
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "clog",
"author": "Firejune (http://firejune.com/)",
- "version": "0.1.1",
+ "version": "0.1.2",
"description": "Colorful console output in NodeJS.",
"homepage": "http://github.com/firejune/clog",
"keywords": [
View
17 test.js
@@ -1,5 +1,22 @@
var clog = require('./lib/clog');
+// display level configration:
+clog.configure({'log level': 2});
+//=> {'log': true, 'info': true, 'warn': false, 'error': false, 'debug': false}
+
+// custom display configration:
+clog.configure({
+ 'log level': {
+ 'log': true,
+ 'info': true,
+ 'warn': false,
+ 'error': true,
+ 'debug': false
+ }
+});
+//=> {'log': true, 'info': true, 'warn': false, 'error': true, 'debug': false}
+
+// clog usage:
clog('server', 'start listening on port 3000'); // custom head
clog.log('hello', 'world'); // console.log

0 comments on commit 16c5caa

Please sign in to comment.