Skip to content
Browse files

update logger

  • Loading branch information...
2 parents aa8ecb4 + cfc3757 commit 8b5263fac446628dfabf3664d1561284989590cb @mxfli committed Jan 16, 2012
Showing with 84 additions and 61 deletions.
  1. +1 −1 index.js
  2. +49 −43 lib/logger.js
  3. +16 −8 lib/request.pipe.js
  4. +1 −1 lib/statusCode.js
  5. +15 −6 package.json
  6. +2 −2 streamPlugins/streamIconv.js
View
2 index.js
@@ -5,6 +5,6 @@
* Description:
* Description of index.js
*/
-var requestPipe = require('lib/request.pipe.js');
+var requestPipe = require('./lib/request.pipe.js');
exports['request.pipe'] = requestPipe;
View
92 lib/logger.js
@@ -6,64 +6,70 @@
* Simple logger for nodejs.
*/
-var levels = ["FATAL", "ERROR", "WARN", "INFO", "DEBUG"];
+var levels = ["error", "warn", "info", "debug"];
+var logger = function (options) {
+ var colors = [31, 33, 36, 90];
+ var innerLevel = {level:levels[3]};
+ /**
+ * real log function;
+ * this value for log level: this.level
+ */
+ var log = function (level, levelStr) {
+ var levelIndex = levels.indexOf(level);
+ if (levels.indexOf(this.level) >= levelIndex) {
+ console.log.apply(console, [' \033[' + colors[levelIndex] + 'm' + levelStr.toUpperCase() + '\033[39m']
+ .concat(Array.prototype.slice.call(arguments, 2)));
+ }
+ };
-if (levels.indexOf(process.env['LOG_LEVEL']) === -1) {
- console.error('ERROR : ', 'Your value:', process.env['LOG_LEVEL'], " accepted LOG_LEVEL values: ", levels.join(';'));
- throw new Error('Available process.env["LOG_LEVEL"] value.');
-}
+ function bindLogMethod(that, options) {
+ levels.forEach(function (level) {
+ that[level] = log.bind(options, level, level + (level.length === 5 ? ':' : ' :'));
+ });
+ }
-var LOG_LEVEL = process.env['LOG_LEVEL'] || levels[3]; //DEFAULT level is 1:INFO;
-var levelLogger = {
- "FATAL":console.error.bind(null, 'FATAL :'),
- "ERROR":console.error.bind(null, 'ERROR :'),
- "WARN":console.warn.bind(null, 'WARN :'),
- "INFO":console.log.bind(null, 'INFO :'),
- "DEBUG":console.log.bind(null, 'DEBUG :')
-};
+ var innerLog = {};
+ bindLogMethod(innerLog, innerLevel);
-/**
- * real log function;
- * this value for log level: this.level
- */
-var log = function () {
- switch (arguments.length) {
- case 0:
- levels[this.level] >= 3 && console.log();
- break;
- default:
- var level = LOG_LEVEL;
- var arg0 = arguments[0];
- var sliceIndex = 0;
- if (typeof arg0 === 'string') {
- arg0 = arg0.trim().toUpperCase();
- if (levels.indexOf(arg0) !== -1) {
- sliceIndex = 1;
- level = arg0;
- }
- }
- if (levels.indexOf(this.level) >= levels.indexOf(level)) {
- levelLogger[level].apply(null, Array.prototype.slice.call(arguments, sliceIndex));
- }
+ if (levels.indexOf(process.env['LOG_LEVEL']) === -1) {
+ process.env['LOG_LEVEL'] = levels[2]; //DEFAULT level is 1:info;
+ innerLog.warn("process.env['LOG_LEVEL'] accepted values: ", levels.join(';'), 'Yours:', process.env['LOG_LEVEL']);
+ innerLog.warn('Using default value:', levels[2]);
}
+ var that = {};
+
+ options = options || {};
+ options.enabled = true;
+ options.color = false !== options.color;
+ options.level = options.level || process.env['LOG_LEVEL'];
+
+ innerLog.info('Process logger options:', options);
+ bindLogMethod(that, options);
+
+ return that;
};
-var logger = exports;
+var self = {level:process.env['LOG_LEVEL'], color:true};
+var _logger = logger(self);
+_logger.info('Inner logger is working...');
/**
* process level log
* set log level use process.env['LOG_LEVEL'], default is level:INFO
*/
-logger.log = function () { log.apply({level:LOG_LEVEL}, arguments)};
+Object.keys(_logger).forEach(function (level) {
+ exports[level] = function () {_logger[level].apply(self, Array.prototype.slice.call(arguments))};
+});
/**
* Custom level for debug is usfule.
* @param level Custom level bind to the logger.default is process.env['LOG_LEVEL']
*/
-logger.getLogger = function (level) {
+exports.getLogger = function (level) {
+ level = level || process.env['LOG_LEVEL'];
console.assert(typeof level === 'string');
- level = level.trim().toUpperCase();
+ level = level.trim().toLowerCase();
+ // Must set level correct
console.assert(levels.indexOf(level) !== -1);
- level = level || LOG_LEVEL;
- return {log:log.bind({level:level})};
-};
+ return logger({level:level, color:true});
+};
View
24 lib/request.pipe.js
@@ -4,9 +4,10 @@
* CreateTime: 2012-01-04 12:29
* Description:
* Description of request.pipe.js
- * use: muti-thread download/crawler/mini memory usage
+ * use: muti-thread download/crawler/smaller memory usage
* A request module that use pipe().pie() style codes, and router
* like connect depends on request urls and response headers.
+ *
*/
var http = require('http');
@@ -20,18 +21,25 @@ var headers = require('./headers.js');
var statusCode = require('./statusCode.js');
var pipedRequest = function () {
- globalAgent.maxSockets = 1; //set maxSockets=2 in dev mod.
+ var config = {
+ cookie:false,
+ enableGzip:true,
+ inputEncoding:'UTF-8'//default input encoding;
+ };
+
+ globalAgent.maxSockets = 2; //set maxSockets=2 in dev mod.
//var reqType = ['link', 'js', 'css', 'image', 'ota/stream'];
var routers = [];//{type:'in reqType',options:optons}
-
+ // TODO(Inacton):Every thing befor request and after response is a router.
+ // Router see: https://github.com/visionmedia/express/tree/master/lib/router
var that = {};
//every request is an cpmpleate session.
that.get = function (options, callback) {
- logger.log('debug', 'request Headers:\n', util.inspect(options.headers));
+ logger.debug('reqsiduest Headers:\n', util.inspect(options.headers));
headers.addDefaultHeaders(options);
//TODO(Inaction) Add cookies support.
- logger.log('warn', 'Cookies is not stetted.');
+ logger.warn('Cookies is not stetted.');
if (options.cookie) {
headers.setHeader.call(options.headers, 'cookie', options.cookie.getString());
}
@@ -43,11 +51,11 @@ var pipedRequest = function () {
//read statusCode and headers do something.
statusCode.doSomething(options, response, function () {
- logger.log('info', 'call statusCode module.');
+ logger.info('call statusCode module.');
//next() function. Add next to router.
});
- logger.log('info', 'response Headers:\n', util.inspect(response.headers, true, 3, true));
+ logger.info('response Headers:\n', util.inspect(response.headers, true, 3, true));
console.assert(Array.isArray(routers) && routers.length > 0);
var raw = response;
@@ -57,7 +65,7 @@ var pipedRequest = function () {
}
routers.forEach(function (router, index) {
- logger.log('debug', 'Add router:', index);
+ logger.debug('Add router:', index);
var _raw = router(options, response);
_raw && (raw = raw.pipe(_raw));
});
View
2 lib/statusCode.js
@@ -10,7 +10,7 @@ var statusCode = exports;
statusCode.doSomething = function (request, response, next) {
var code = response.statusCode;
- logger.log('debug', 'response statusCode:', code);
+ logger.debug('response statusCode:', code);
if (code === 200) {
next();
View
21 package.json
@@ -1,10 +1,17 @@
-{ "name":"request.pipe",
+{
+ "name":"request.pipe",
"description":"pipe().pipe() request and response and router like connect.",
- "tags":["request", "pipe", "http", "stream"],
- "version":"0.0.1", "author":"Inaction <Inaction.me@gmail.com> http://inaction.me",
+ "tags":[
+ "request",
+ "pipe",
+ "http",
+ "stream"
+ ],
+ "version":"0.0.2",
+ "author":"Inaction <Inaction.me@gmail.com> (http://inaction.me)",
"repository":{
"type":"git",
- "url":"https://github.com/mxfli/request.pipe.git"
+ "url":"git://github.com/mxfli/request.pipe.git"
},
"bugs":{
"url":"https://github.com/mxfli/request.pipe/issues"
@@ -15,5 +22,7 @@
"engines":{
"node":">= 0.6.x < 0.7.0"
},
- "main":"index"
-}
+ "main":"index",
+ "devDependencies":{},
+ "homepage":"https://github.com/mxfli/request.pipe"
+}
View
4 streamPlugins/streamIconv.js
@@ -27,7 +27,7 @@ StreamIconv.prototype.write = function (data) {
//TODO(Inaction) less memory useage mode,add real stream convert.
this.buffers.push(data);
this.length += data.length;
- logger.log('debug', 'Total data lenth:', this.length);
+ logger.debug('Total data lenth:', this.length);
};
StreamIconv.prototype.end = function () {
@@ -45,7 +45,7 @@ StreamIconv.prototype.end = function () {
};
exports.createIconvStream = function (inputEncoding, outputEncoding) {
- logger.log('Create Iconv Stream.', inputEncoding, 'to', outputEncoding);
+ logger.info('Create Iconv Stream.', inputEncoding, 'to', outputEncoding);
var iconv = new Iconv(inputEncoding, outputEncoding + '//IGNORE');
return new StreamIconv(iconv.convert.bind(iconv));
};

0 comments on commit 8b5263f

Please sign in to comment.
Something went wrong with that request. Please try again.