Skip to content

Commit

Permalink
Use the version number provided by ecs-helpers (#13)
Browse files Browse the repository at this point in the history
* Use the version number provided by ecs-helpers

* Updated test
  • Loading branch information
delvedor authored Mar 16, 2020
1 parent fbe3078 commit 1b77d40
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 11 deletions.
5 changes: 2 additions & 3 deletions loggers/morgan/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const morgan = require('morgan')
const {
version,
stringify,
formatHttpRequest,
formatHttpResponse
Expand All @@ -23,9 +24,7 @@ function ecsFormat (format = morgan.combined) {
logger: 'morgan'
},
message: messageFormat(token, req, res),
ecs: {
version: '1.4.0'
}
ecs: { version }
}

formatHttpRequest(ecs, req)
Expand Down
2 changes: 1 addition & 1 deletion loggers/morgan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"node": ">=10"
},
"dependencies": {
"@elastic/ecs-helpers": "^0.1.0"
"@elastic/ecs-helpers": "^0.2.0"
},
"devDependencies": {
"ajv": "^6.11.0",
Expand Down
4 changes: 3 additions & 1 deletion loggers/morgan/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const express = require('express')
const morgan = require('morgan')
const stoppable = require('stoppable')
const split = require('split2')
const { version } = require('@elastic/ecs-helpers')

const ecsFormat = require('./')

const ajv = Ajv({
Expand Down Expand Up @@ -57,7 +59,7 @@ test.cb('Keys order', t => {

const stream = split().on('data', line => {
const log = JSON.parse(line)
t.is(line, `{"@timestamp":"${log['@timestamp']}","log":{"level":"info","logger":"morgan"},"message":"${JSON.stringify(log.message).slice(1, -1)}","ecs":{"version":"1.4.0"},"http":{"version":"1.1","request":{"method":"post","headers":{"accept-encoding":"gzip, deflate","content-type":"application/json","host":"${log.http.request.headers.host}","connection":"close"},"body":{"bytes":17}},"response":{"status_code":200,"headers":{"x-powered-by":"Express"}}},"url":{"path":"/","domain":"localhost","query":"foo=bar","full":"/?foo=bar"},"user_agent":{"original":"cool-agent"}}`)
t.is(line, `{"@timestamp":"${log['@timestamp']}","log":{"level":"info","logger":"morgan"},"message":"${JSON.stringify(log.message).slice(1, -1)}","ecs":{"version":"${version}"},"http":{"version":"1.1","request":{"method":"post","headers":{"accept-encoding":"gzip, deflate","content-type":"application/json","host":"${log.http.request.headers.host}","connection":"close"},"body":{"bytes":17}},"response":{"status_code":200,"headers":{"x-powered-by":"Express"}}},"url":{"path":"/","domain":"localhost","query":"foo=bar","full":"/?foo=bar"},"user_agent":{"original":"cool-agent"}}`)
})

const app = express()
Expand Down
5 changes: 2 additions & 3 deletions loggers/winston/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const { MESSAGE } = require('triple-beam')
const { format } = require('winston')
const {
version,
stringify,
formatHttpRequest,
formatHttpResponse
Expand All @@ -32,9 +33,7 @@ function ecsFormat (log) {
logger: 'winston'
},
message: log.message,
ecs: {
version: '1.4.0'
}
ecs: { version }
}

if (log.req || log.request) {
Expand Down
2 changes: 1 addition & 1 deletion loggers/winston/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"node": ">=10"
},
"dependencies": {
"@elastic/ecs-helpers": "^0.1.0"
"@elastic/ecs-helpers": "^0.2.0"
},
"devDependencies": {
"ajv": "^6.11.0",
Expand Down
6 changes: 4 additions & 2 deletions loggers/winston/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const winston = require('winston')
const Transport = require('winston-transport')
const { MESSAGE } = require('triple-beam')
const Ajv = require('ajv')
const { version } = require('@elastic/ecs-helpers')

const ecsFormat = require('./')

const ajv = Ajv({
Expand Down Expand Up @@ -223,12 +225,12 @@ test('Keys order', t => {
if (count++ === 0) {
t.is(
info[MESSAGE],
`{"@timestamp":"${line['@timestamp']}","log":{"level":"info","logger":"winston"},"message":"ecs is cool!","ecs":{"version":"1.4.0"}}`
`{"@timestamp":"${line['@timestamp']}","log":{"level":"info","logger":"winston"},"message":"ecs is cool!","ecs":{"version":"${version}"}}`
)
} else {
t.is(
info[MESSAGE],
`{"@timestamp":"${line['@timestamp']}","log":{"level":"error","logger":"winston"},"message":"ecs is cool!","ecs":{"version":"1.4.0"},"hello":"world"}`
`{"@timestamp":"${line['@timestamp']}","log":{"level":"error","logger":"winston"},"message":"ecs is cool!","ecs":{"version":"${version}"},"hello":"world"}`
)
}
callback()
Expand Down

0 comments on commit 1b77d40

Please sign in to comment.