From 339bc1c5f62a3ca0caee6babc2a0ff80dd57a527 Mon Sep 17 00:00:00 2001 From: Zoltan Olah Date: Fri, 19 Jan 2018 17:42:03 -0800 Subject: [PATCH 1/2] Adds support for the RFC5424 structured data field --- Readme.md | 4 +++- lib/rfc5424.js | 2 +- psyslog.js | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 29a73d5..9e54a47 100644 --- a/Readme.md +++ b/Readme.md @@ -95,7 +95,8 @@ There is only one argument available: `--config` (`-c`). This argument is used t "includeProperties": [], "messageOnly": false, "tz": "Etc/UTC", - "newline": false + "newline": false, + "structuredData": "none" } ``` @@ -111,6 +112,7 @@ There is only one argument available: `--config` (`-c`). This argument is used t + `tz` (string): any [valid timezone string][tzstring] that [moment][moment] will recognize. The timestamp field of the syslog header will be sent according to this setting. + `newline` (boolean): terminate with a newline ++ `structuredData` (string): structured data to send with an RFC5424 message. [facility]: https://tools.ietf.org/html/rfc3164#section-4.1.1 [tzstring]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones diff --git a/lib/rfc5424.js b/lib/rfc5424.js index 55c1cb8..2553a0c 100644 --- a/lib/rfc5424.js +++ b/lib/rfc5424.js @@ -35,7 +35,7 @@ module.exports = function ($options) { const hostname = data.hostname const appname = (options.appname !== 'none') ? options.appname : '-' const msgid = (data.req && data.req.id) ? data.req.id : '-' - const structuredData = '-' + const structuredData = (options.structuredData !== 'none') ? options.structuredData : '-' const header = `<${pri}>${version} ${tstamp} ${hostname} ${appname} ${data.pid} ${msgid} ${structuredData} ` const message = buildMessage(data) let output = (options.cee && !options.messageOnly) ? `${header}@cee: ${message}` : `${header}${message}` diff --git a/psyslog.js b/psyslog.js index 532b1a0..6527c00 100644 --- a/psyslog.js +++ b/psyslog.js @@ -33,7 +33,8 @@ const defaults = { includeProperties: [], messageOnly: false, tz: 'Etc/UTC', - newline: false + newline: false, + structuredData: 'none' } const options = Object.assign(defaults, userOptions) From 3fa3ed3dfb4583ca8339f917cc99e9954ca77c2f Mon Sep 17 00:00:00 2001 From: Zoltan Olah Date: Mon, 22 Jan 2018 09:28:43 -0800 Subject: [PATCH 2/2] Adds test for structured data --- Readme.md | 4 ++-- lib/rfc5424.js | 2 +- psyslog.js | 3 +-- test/5424.test.js | 14 ++++++++++++++ test/fixtures/configs/5424/structuredData.json | 3 +++ 5 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/configs/5424/structuredData.json diff --git a/Readme.md b/Readme.md index 9e54a47..ade4460 100644 --- a/Readme.md +++ b/Readme.md @@ -96,7 +96,7 @@ There is only one argument available: `--config` (`-c`). This argument is used t "messageOnly": false, "tz": "Etc/UTC", "newline": false, - "structuredData": "none" + "structuredData": "-" } ``` @@ -112,7 +112,7 @@ There is only one argument available: `--config` (`-c`). This argument is used t + `tz` (string): any [valid timezone string][tzstring] that [moment][moment] will recognize. The timestamp field of the syslog header will be sent according to this setting. + `newline` (boolean): terminate with a newline -+ `structuredData` (string): structured data to send with an RFC5424 message. ++ `structuredData` (string): [structured data](https://tools.ietf.org/html/rfc5424#section-6.3) to send with an RFC5424 message. [facility]: https://tools.ietf.org/html/rfc3164#section-4.1.1 [tzstring]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones diff --git a/lib/rfc5424.js b/lib/rfc5424.js index 2553a0c..4ae86f3 100644 --- a/lib/rfc5424.js +++ b/lib/rfc5424.js @@ -35,7 +35,7 @@ module.exports = function ($options) { const hostname = data.hostname const appname = (options.appname !== 'none') ? options.appname : '-' const msgid = (data.req && data.req.id) ? data.req.id : '-' - const structuredData = (options.structuredData !== 'none') ? options.structuredData : '-' + const structuredData = options.structuredData || '-' const header = `<${pri}>${version} ${tstamp} ${hostname} ${appname} ${data.pid} ${msgid} ${structuredData} ` const message = buildMessage(data) let output = (options.cee && !options.messageOnly) ? `${header}@cee: ${message}` : `${header}${message}` diff --git a/psyslog.js b/psyslog.js index 6527c00..532b1a0 100644 --- a/psyslog.js +++ b/psyslog.js @@ -33,8 +33,7 @@ const defaults = { includeProperties: [], messageOnly: false, tz: 'Etc/UTC', - newline: false, - structuredData: 'none' + newline: false } const options = Object.assign(defaults, userOptions) diff --git a/test/5424.test.js b/test/5424.test.js index ec54e43..f6ea5f0 100644 --- a/test/5424.test.js +++ b/test/5424.test.js @@ -137,3 +137,17 @@ test('appends newline', (t) => { psyslog.stdin.write(messages.helloWorld + '\n') }) + +test('uses structured data', (t) => { + t.plan(1) + const expected = '<134>1 2016-04-01T16:44:58Z MacBook-Pro-3 - 94473 - [a@b x="y"] ' + messages.helloWorld + const psyslog = spawn('node', [ psyslogPath, '-c', configPath('5424', 'structuredData.json') ]) + + psyslog.stdout.on('data', (data) => { + const msg = data.toString() + t.is(msg, expected) + psyslog.kill() + }) + + psyslog.stdin.write(messages.helloWorld + '\n') +}) diff --git a/test/fixtures/configs/5424/structuredData.json b/test/fixtures/configs/5424/structuredData.json new file mode 100644 index 0000000..36676a0 --- /dev/null +++ b/test/fixtures/configs/5424/structuredData.json @@ -0,0 +1,3 @@ +{ + "structuredData": "[a@b x=\"y\"]" +}