Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added convenience method to track response time

  • Loading branch information...
commit 70a505d3d98a482bcc337cf18647781f402a3416 1 parent 33d373d
Chris Blanchard authored
Showing with 55 additions and 16 deletions.
  1. +17 −6 README.md
  2. +13 −6 index.js
  3. +1 −1  package.json
  4. +24 −3 tests/unit.js
23 README.md
View
@@ -8,12 +8,19 @@ Getting my head around Bunyan. Relies on module caching to retrieve the same ins
```
var bunyan = require("bunyan"),
- Logger = {
- logger: null,
- init: function (config) {
- this.logger = new bunyan.createLogger(config);
- }
- }
+ Logger = {};
+
+Logger.logger = null;
+
+Logger.init = function (config) {
+ Logger.logger = new bunyan.createLogger(config);
+};
+
+Logger.logTime = function (t, message) {
+ message = message || {};
+ message.time = process.hrtime(t)[1] / 1000000;
+ Logger.logger.debug(message);
+}
module.exports = Logger;
```
@@ -29,6 +36,10 @@ logger = require("../../../lib/logger");
...
```
+## Other things
+
+Added a convenience method to log time given an instance of process.hrtime
+
## License
MIT
19 index.js
View
@@ -1,9 +1,16 @@
var bunyan = require("bunyan"),
- Logger = {
- logger: null,
- init: function (config) {
- this.logger = new bunyan.createLogger(config);
- }
- }
+ Logger = {};
+
+Logger.logger = null;
+
+Logger.init = function (config) {
+ Logger.logger = new bunyan.createLogger(config);
+};
+
+Logger.logTime = function (t, message) {
+ message = message || {};
+ message.time = process.hrtime(t)[1] / 1000000;
+ Logger.logger.debug(message);
+}
module.exports = Logger;
2  package.json
View
@@ -1,6 +1,6 @@
{
"name": "commonlog-bunyan",
- "version": "0.2.1",
+ "version": "0.3.0",
"description": "Naive common log for bunyan",
"main": "index.js",
"directories": {
27 tests/unit.js
View
@@ -3,7 +3,8 @@ var assert = require("chai").assert,
fs = require("fs"),
Tail = require("tail").Tail,
randomstring = require("randomstring"),
- TIMEOUT = 100;
+ TIMEOUT = 100,
+ time;
// Configure Logger
var logPath = path.join(__dirname, "logs/test.log");
@@ -16,7 +17,9 @@ var logPath = path.join(__dirname, "logs/test.log");
};
require("../index.js").init(config);
-var log = require("../index.js").logger;
+var commonlog = require("../index.js"),
+ log = commonlog.logger,
+ logTime = commonlog.logTime;
// Helper method
var emptyLog = function (callback) {
@@ -121,4 +124,22 @@ describe("Writing to log path", function () {
log.fatal(testMessage);
}, TIMEOUT);
});
-});
+
+ it ("should log time", function (done) {
+ time = process.hrtime();
+ tail = new Tail(logPath);
+ tail.on("line", function (data) {
+ var logLine = JSON.parse(data);
+ assert.equal(logLine.level, 20);
+ assert.equal(logLine.message, testMessage);
+ assert.equal(logLine.secondMessage, testMessage);
+ assert.isNumber(logLine.time);
+ done();
+ });
+ setTimeout(function () {
+ logTime(time, {message: testMessage, secondMessage: testMessage});
+ }, TIMEOUT);
+ });
+});
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.