From 8e16e920bc31d0acfbc7ba09c2e163df76046957 Mon Sep 17 00:00:00 2001 From: Jimmie Hansson Date: Mon, 22 Oct 2018 13:55:56 +0200 Subject: [PATCH 1/3] Update pino-elasticsearch.js --- pino-elasticsearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pino-elasticsearch.js b/pino-elasticsearch.js index 394b4e8..aa4b257 100644 --- a/pino-elasticsearch.js +++ b/pino-elasticsearch.js @@ -25,7 +25,7 @@ function pinoElasticSearch (opts) { time: (new Date()).toISOString() } } else { - value.time = (new Date(value.time)).toISOString() + value.time = (value && value.time) ? (new Date(value.time)).toISOString() : new Date(); } return value From a9f247a86ba5b37ad81b94f153e33ac898513828 Mon Sep 17 00:00:00 2001 From: Jimmie Hansson Date: Mon, 22 Oct 2018 13:56:56 +0200 Subject: [PATCH 2/3] Update pino-elasticsearch.js --- pino-elasticsearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pino-elasticsearch.js b/pino-elasticsearch.js index aa4b257..397f833 100644 --- a/pino-elasticsearch.js +++ b/pino-elasticsearch.js @@ -25,7 +25,7 @@ function pinoElasticSearch (opts) { time: (new Date()).toISOString() } } else { - value.time = (value && value.time) ? (new Date(value.time)).toISOString() : new Date(); + value.time = (value && value.time) ? (new Date(value.time)).toISOString() : new Date() } return value From 96d0bfa7999fc6f71324c15a29ac29dc221eaa9d Mon Sep 17 00:00:00 2001 From: Jimmie Hansson Date: Tue, 23 Oct 2018 12:49:15 +0200 Subject: [PATCH 3/3] ISSUE-19 add unit test, drykeeping date time function to reuse --- pino-elasticsearch.js | 10 ++++++++-- test/fixtures.js | 8 ++++++++ test/unit.test.js | 15 ++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 test/fixtures.js diff --git a/pino-elasticsearch.js b/pino-elasticsearch.js index 397f833..892dbc8 100644 --- a/pino-elasticsearch.js +++ b/pino-elasticsearch.js @@ -22,12 +22,18 @@ function pinoElasticSearch (opts) { if (typeof value === 'string') { value = { data: value, - time: (new Date()).toISOString() + time: setDateTimeString(value) } } else { - value.time = (value && value.time) ? (new Date(value.time)).toISOString() : new Date() + value.time = setDateTimeString(value) } + function setDateTimeString (value) { + if (typeof value === 'object' && value.hasOwnProperty('time')) { + return (value.time.length > 0) ? new Date(value.time).toISOString() : new Date().toISOString() + } + return new Date().toISOString() + } return value }) const client = new elasticsearch.Client({ diff --git a/test/fixtures.js b/test/fixtures.js new file mode 100644 index 0000000..de6368d --- /dev/null +++ b/test/fixtures.js @@ -0,0 +1,8 @@ +'use strict' +const ctx = module.exports + +ctx.datetime = { + object: new Date('2018-10-22T18:09:48.110Z').toISOString(), + string: '2018-10-22T18:09:48.110Z' +} +module.exports = ctx diff --git a/test/unit.test.js b/test/unit.test.js index 016b8e9..dcfdc0b 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -1,10 +1,11 @@ 'use strict' const pino = require('pino') - const proxyquire = require('proxyquire') - const test = require('tap').test +const fix = require('./fixtures') + +const matchISOString = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/ const options = { index: 'pinotest', type: 'log', @@ -12,13 +13,21 @@ const options = { host: 'localhost', port: 9200 } + +test('make sure date format is valid', (t) => { + t.type(fix.datetime.object, 'string') + t.equal(fix.datetime.object, fix.datetime.string) + t.end() +}) test('make sure log is a valid json', (t) => { - t.plan(2) + t.plan(4) const Client = function (config) { t.equal(config.host, `${options.host}:${options.port}`) } Client.prototype.index = (obj, cb) => { t.ok(obj, true) + t.type(obj.body.time, 'string') + t.match(obj.body.time, matchISOString) cb(null, {}) } const elastic = proxyquire('../', {