From 576c9a17c1b27e1733476cc6e8455a6a8a98d150 Mon Sep 17 00:00:00 2001 From: nkzawa Date: Tue, 8 Oct 2019 13:30:00 +0900 Subject: [PATCH] allow to set hostname and applicationName on buildMessage --- index.js | 22 +++++++++++++++------- tests/syslog.test.js | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index df967a9..6fdcbe5 100644 --- a/index.js +++ b/index.js @@ -669,8 +669,10 @@ class RFC3164 extends RFC { * @param {number} [options.severity=7] - An array of structure * @param {number} [options.colorCode=36] - The ANSI color code to use if * message coloration is selected - * @param {Date} [options.timestamp=null] - The timestamp to use - * @returns {Promise} A Syslog formatted string according to the selected RFC + * @param {Date} [options.timestamp] - The timestamp to use + * @param {string} [options.hostname] - The hostname + * @param {string} [options.applicationName] - The application name + * @returns {string} A Syslog formatted string according to the selected RFC * @throws {Error} A standard error object */ buildMessage(msg, options) { @@ -713,11 +715,13 @@ class RFC3164 extends RFC { const timestamp = moment(options.timestamp) .format('MMM DD hh:mm:ss') .replace(rfc3164DateRegEx, '$1 $5'); + const hostname = options.hostname || this.hostname; + const applicationName = options.applicationName || this.applicationName; // Build message fmtMsg = '<' + pri + '>'; fmtMsg += timestamp; - fmtMsg += ' ' + this.hostname; - fmtMsg += ' ' + this.applicationName; + fmtMsg += ' ' + hostname; + fmtMsg += ' ' + applicationName; fmtMsg += ' ' + msg; fmtMsg += newLine; return fmtMsg; @@ -1103,7 +1107,9 @@ class RFC5424 extends RFC { * [name@ parameter=value] * @param {number} [options.colorCode=36] - The ANSI color code to use if * message coloration is selected - * @param {Date} [options.timestamp=null] - The timestamp to use + * @param {Date} [options.timestamp] - The timestamp to use + * @param {string} [options.hostname] - The hostname + * @param {string} [options.applicationName] - The application name * @returns {string} A Syslog formatted string according to the selected RFC * @throws {Error} A standard error object */ @@ -1117,6 +1123,8 @@ class RFC5424 extends RFC { throw new Error(errMsg); } let facility = options.facility || 23; + let hostname = options.hostname || this.hostname; + let applicationName = options.applicationName || this.applicationName; let pid = options.pid || '-'; let id = options.id || '-'; let msgStructuredData = options.msgStructuredData || []; @@ -1223,8 +1231,8 @@ class RFC5424 extends RFC { fmtMsg = '<' + pri + '>'; fmtMsg += '1'; // Version number fmtMsg += ' ' + timestamp; - fmtMsg += ' ' + this.hostname; - fmtMsg += ' ' + this.applicationName; + fmtMsg += ' ' + hostname; + fmtMsg += ' ' + applicationName; fmtMsg += ' ' + pid; fmtMsg += ' ' + id; fmtMsg += ' ' + structuredData; diff --git a/tests/syslog.test.js b/tests/syslog.test.js index fbf3167..53c1eed 100644 --- a/tests/syslog.test.js +++ b/tests/syslog.test.js @@ -411,6 +411,14 @@ describe('RFC5424 Class Tests', () => { const result = rfc5424.buildMessage('hello', { timestamp }); expect(result.startsWith('<190>1 2020-01-01T01:23:45.678000+00:00 ')).toBe(true); }); + test('RFC5424 BuildMessage with hostname and applicationName options', () => { + const rfc5424 = new SyslogPro.RFC5424(); + const result = rfc5424.buildMessage('hello', { + hostname: 'hostname', + applicationName: 'applicationName' + }); + expect(result).toMatch(/^<190>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2} hostname applicationName - - - BOMhello\n$/); + }); test('RFC5424 SetColors', () => { let rfc5424 = new SyslogPro.RFC5424(); const result = rfc5424.setColor({ @@ -718,9 +726,16 @@ describe('RFC3164 Class Tests', () => { const rfc3164 = new SyslogPro.RFC3164(); const timestamp = new Date(2020, 0, 1, 1, 23, 45); const result = rfc3164.buildMessage('hello', { timestamp }); - console.log(result); expect(result.startsWith('<190>Jan 1 01:23:45 ')).toBe(true); }); + test('RFC3164 BuildMessage with hostname and applicationName options', () => { + const rfc3164 = new SyslogPro.RFC3164(); + const result = rfc3164.buildMessage('hello', { + hostname: 'hostname', + applicationName: 'applicationName' + }); + expect(result).toMatch(/^<190>[A-Z][a-z]{2} [ \d]\d \d{2}:\d{2}:\d{2} hostname applicationName hello\n$/); + }); }); // Base Syslog Class Test