From bfccc7a6ff5908752ff91e654b2130f82162b4d3 Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Thu, 12 Jun 2014 19:37:12 +0100 Subject: [PATCH] using stream-log-stats --- bin/ws.js | 36 ++++++++---------------------------- package.json | 2 ++ 2 files changed, 10 insertions(+), 28 deletions(-) diff --git a/bin/ws.js b/bin/ws.js index d534694..b1fe575 100755 --- a/bin/ws.js +++ b/bin/ws.js @@ -13,7 +13,9 @@ var dope = require("console-dope"), directory = require("serve-index"), compress = require("compression"), homePath = require("home-path"), - byteSize = require("byte-size"); + byteSize = require("byte-size"), + clf = require("common-log-format"), + logStats = require("stream-log-stats"); var usage = "usage: \n\ @@ -86,14 +88,13 @@ if (argv.config){ var app = connect(); - /* log using --log-format (if supplied), else output statics */ - if(argv["log-format"]){ + /* log using --log-format (if supplied) */ + if(argv["log-format"]) { app.use(morgan(argv["log-format"])); } else { - app.use(function(req, res, next){ - dope.column(1).write(++total.req); - next(); - }); + var statStream = clf(); + statStream.pipe(logStats()); + app.use(morgan({ stream: statStream })); } /* --compress enables compression */ @@ -120,25 +121,4 @@ if (argv.config){ } else { dope.error("serving %underline{%s} at %underline{%s}", argv.directory, "http://localhost:" + argv.port); } - - /* in stats mode, monitor connections and bytes transferred */ - if (!argv["log-format"]){ - dope.hideCursor(); - dope.log("%underline{Requests} %underline{Data} %underline{Connections}"); - server.on("connection", function(socket){ - var oldWrite = socket.write; - socket.write = function(data) { - if (!Buffer.isBuffer(data)) { - data = new Buffer(data); - } - oldWrite.call(this, data); - total.bytes += data.length; - dope.column(12).write(s.padRight(byteSize(total.bytes, 2), 12)); - }; - dope.column(24).write(++total.connections); - socket.on("close", function(){ - dope.column(24).write(s.padRight(--total.connections)); - }); - }); - } } diff --git a/package.json b/package.json index 6f23fe9..f69f8dd 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "dependencies": { "byte-size": "^0.1.0", "command-line-args": "^0.2.0", + "common-log-format": "^0.1.2", "compression": "^1.0.2", "config-master": "~0.1.0", "connect": "^3.0.0", @@ -25,6 +26,7 @@ "object-ting": "^1.0.0", "serve-index": "^1.0.2", "serve-static": "^1.2.2", + "stream-log-stats": "0.0.1", "string-ting": "0.0.1" } }