From 0b9d4df3b5e01bc572f89287719ee75ce9ecea84 Mon Sep 17 00:00:00 2001 From: Unknown Moon Date: Thu, 20 Oct 2016 22:50:10 +0800 Subject: [PATCH 1/3] Support naming the connection in log. --- README.md | 11 +++++++++++ client-demo/index.html | 21 +++++++++++++++++++-- lib/client-script.js | 16 ++++++++++------ lib/logger.js | 10 ++++++++-- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3d056d7..b212b24 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,15 @@ In the example below, `__debug` and `__log` are registered to the logger, with d __debug(location); __log(location); +``` + +Newly add `from` in query, for providing an id other than the random socket id. (Note that socket id will be used if `from` is ommitted.) + +```html + + + // how to use the client logging functions + __debug(location); + __log(location); + ``` \ No newline at end of file diff --git a/client-demo/index.html b/client-demo/index.html index 3218a9e..9fc8d88 100644 --- a/client-demo/index.html +++ b/client-demo/index.html @@ -37,8 +37,25 @@ - + + + \ No newline at end of file diff --git a/lib/client-script.js b/lib/client-script.js index 385fcbe..f367699 100644 --- a/lib/client-script.js +++ b/lib/client-script.js @@ -18,32 +18,36 @@ function genClientInjectScript(opts) { const host = opts.host || 'localhost'; const func = opts.func; const funcSnippets = _.map(func, function(fnName) { - return genFuncSnippets(fnName); + return genFuncSnippets(fnName, opts); }).join(';'); return ` - ${genSetupSnippets(host, func)} + ${genSetupSnippets(host, func, opts)} ${funcSnippets} `; } -function genSetupSnippets(host, func) { +function genSetupSnippets(host, func, opts = {}) { + const who = opts['from'] ? '\'' + opts['from'] + '\'' : 'socket.id'; return ` var socket = io('${host}'); socket.on('connect', function() { socket.emit('setup', { - func: JSON.parse('${JSON.stringify(func)}') + func: JSON.parse('${JSON.stringify(func)}'), + 'from': ${who}, + time: Date.now() }); }); `; } -function genFuncSnippets(funcName) { +function genFuncSnippets(funcName, opts = {}) { + const who = opts['from'] ? '\'' + opts['from'] + '\'' : 'socket.id'; return ` window['${funcName}'] = function (msgOrObj) { socket.emit('${funcName}', { - 'from': socket.id, + 'from': ${who}, time: Date.now(), msg: JSON.stringify(msgOrObj) }); diff --git a/lib/logger.js b/lib/logger.js index 151447c..d1bbbe3 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -58,7 +58,8 @@ function apiHander(req, res) { res.writeHead(200); res.end(clientScript.genScript({ host, - func: [].concat(parsedQuery.func || '__debug') + func: [].concat(parsedQuery.func || '__debug'), + 'from': parsedQuery['from'] === undefined ? parsedQuery['from'] : '' + parsedQuery['from'] })); } else { @@ -76,7 +77,12 @@ function ioConnectionHandler(socket) { socket.emit('who r u'); socket.on('setup', function(data) { - console.log(chalk.underline(`[${_u.getTimeStr(Date.now())}][${socket.id}] on setup: `)); + // colourful hightlight the `from` + let render = _cr.getRender(socket.id); + let who = data['from'] ? data['from'] : socket.id; + let time = _u.getTimeStr(data.time || Date.now()); + console.log(chalk.underline(`[${time}][${chalk.bold(render(who))}] on setup: `)); + render.destroy(); if (data && _.isArray(data.func)) { _.each(data.func, function(funcName) { From 749bc0db423eb1fa8b57e3270ed7a85072316ade Mon Sep 17 00:00:00 2001 From: Unknown Moon Date: Thu, 20 Oct 2016 22:53:46 +0800 Subject: [PATCH 2/3] Print primitive values as it is, and only apply 'util.inspect' to `object` type data. --- lib/logger.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/logger.js b/lib/logger.js index d1bbbe3..e5d2088 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -121,7 +121,11 @@ function setupSocketHandler(funcName, socket, renderFn) { parsedMsg = msg; } console.log(renderFn(`[${_u.getTimeStr(time)}][${who}][${funcName}]:`)); - _u.inspect(parsedMsg); + if (typeof parsedMsg === 'object') { + _u.inspect(parsedMsg); + } else { + console.log(renderFn(parsedMsg)); + } }); } From 6237013a7f596bf0e1f859f2e0dc3bd8b9c4803e Mon Sep 17 00:00:00 2001 From: Unknown Moon Date: Thu, 20 Oct 2016 22:58:42 +0800 Subject: [PATCH 3/3] Bump version to 0.0.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6f194e9..5ff88c1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tool-socketio-logger", - "version": "0.0.1", + "version": "0.0.2", "description": "A simple as-it-is message logger powered by socket.io", "main": "index.js", "scripts": {