Skip to content

Commit

Permalink
Timestamp from log record (#24)
Browse files Browse the repository at this point in the history
* Added support for custom @timestamp & @timestamp_nano

* Added tests

* Bumped version & updated README.md
  • Loading branch information
Amir Tugendhaft authored Jul 18, 2017
1 parent 51383c8 commit 8027463
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
24 changes: 17 additions & 7 deletions lib/logzio-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand All @@ -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) {
Expand Down
24 changes: 16 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <gilly@logz.io>",
"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"
Expand All @@ -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": {
Expand Down
32 changes: 32 additions & 0 deletions test/test-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 8027463

Please sign in to comment.