diff --git a/README.md b/README.md index 1f8ea6b..8e60602 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,10 @@ different in this case. The messages will still be sent separately, but the logg sending out all the messages. If you want each message to be sent out immediately, then set `bufferSize = 1`. -## Update log +## Update log +**0.4.4** +- `@timestamp` and `@timestamp_nano` will no longer be overriden given a custom value by the user. + **0.4.3** - Add the `@timestamp` field to the logs on the client's machine (and not when it reaches the server) diff --git a/lib/logzio-nodejs.js b/lib/logzio-nodejs.js index bc03cfd..6991674 100755 --- a/lib/logzio-nodejs.js +++ b/lib/logzio-nodejs.js @@ -140,6 +140,22 @@ LogzioLogger.prototype.close = function () { this.closed = true; }; +/** + * Attach a timestamp to the log record. If @timestamp already exists, use it. Else, use current time. + * The same goes for @timestamp_nano + * @param msg - The message (Object) to append the timestamp to. + * @private + */ +LogzioLogger.prototype._addTimestamp = function(msg) { + var now = (new Date()).toISOString(); + msg['@timestamp'] = msg['@timestamp'] || now; + + if (this.addTimestampWithNanoSecs) { + var time = process.hrtime(); + msg['@timestamp_nano'] = msg['@timestamp_nano'] || [now, time[0].toString(), time[1].toString()].join('-'); + } +}; + LogzioLogger.prototype.log = function(msg) { if (this.closed === true) { throw new Error('Logging into a logger that has been closed!'); @@ -151,13 +167,7 @@ LogzioLogger.prototype.log = function(msg) { msg = _assign(msg, this.extraFields); msg.type = this.type; - var now = (new Date()).toISOString(); - msg['@timestamp'] = now; - - if (this.addTimestampWithNanoSecs) { - var time = process.hrtime(); - msg['@timestamp_nano'] = [now, time[0].toString(), time[1].toString()].join('-'); - } + this._addTimestamp(msg); this.messages.push(msg); if (this.messages.length >= this.bufferSize) { diff --git a/package.json b/package.json index 8261ac8..88cdebc 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,22 @@ { "name": "logzio-nodejs", "description": "A nodejs implementation for sending logs to Logz.IO cloud service", - "version": "0.4.3", + "version": "0.4.4", "author": "Gilly Barr ", - "contributors": [{ - "name": "Gilly Barr", - "email": "gillyb@gmail.com" - }, { - "name": "Asaf Mesika", - "email": "asaf.mesika@gmail.com" - }], + "contributors": [ + { + "name": "Gilly Barr", + "email": "gillyb@gmail.com" + }, + { + "name": "Asaf Mesika", + "email": "asaf.mesika@gmail.com" + }, + { + "name": "Amir Tugendhaft", + "email": "Amir.Tugi@gmail.com" + } + ], "repository": { "type": "git", "url": "https://github.com/logzio/logzio-nodejs.git" @@ -24,6 +31,7 @@ "dependencies": { "json-stringify-safe": "5.0.x", "lodash.assign": "4.2.0", + "moment": "^2.18.1", "request": "2.75.0" }, "devDependencies": { diff --git a/test/test-logger.js b/test/test-logger.js index 9136070..b3687bf 100644 --- a/test/test-logger.js +++ b/test/test-logger.js @@ -3,6 +3,7 @@ var logzioLogger = require('../lib/logzio-nodejs.js'); var request = require('request'); var nock = require('nock'); var assert = require('assert'); +var moment = require('moment'); var dummyHost = 'logz.io'; var nockHttpAddress = 'http://' + dummyHost + ':8070'; @@ -129,6 +130,37 @@ describe('logger', function() { logger._createBulk.restore(); logger.close(); }); + it('writes a log message without @timestamp', function(done) { + var logger = createLogger({ + // buffer is 2 so we could access the log before we send it, to analyze it + bufferSize:2, + callback: done + }); + + var fakeTime = moment("2011-09-01").valueOf(); + + // Fake the current time, so we could test on it + var clock = sinon.useFakeTimers(fakeTime); + logger.log({ message: 'hello there from test' }); + clock.restore(); + + assert.equal(fakeTime, moment(logger.messages[logger.messages.length-1]['@timestamp'].valueOf())); + logger.close(); + }); + it.only('writes a log message with a custom @timestamp', function(done) { + var logger = createLogger({ + // buffer is 2 so we could access the log before we send it, to analyze it + bufferSize:2, + callback: done + }); + + var fakeTime = moment("2011-09-01"); + + logger.log({ message: 'hello there from test', '@timestamp': fakeTime.format()}); + + assert.equal(fakeTime.format(), logger.messages[logger.messages.length-1]['@timestamp']); + logger.close(); + }); }); describe('logs multiple lines', function () {