Merge pull request #5 from zeit/set-hostname-application-name

Allow to set hostname and applicationName on buildMessage
This commit is contained in:
Naoyuki Kanezawa 2019-10-08 13:31:03 +09:00 committed by GitHub
commit ed421ca167
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 8 deletions

View File

@ -669,8 +669,10 @@ class RFC3164 extends RFC {
* @param {number} [options.severity=7] - An array of structure * @param {number} [options.severity=7] - An array of structure
* @param {number} [options.colorCode=36] - The ANSI color code to use if * @param {number} [options.colorCode=36] - The ANSI color code to use if
* message coloration is selected * message coloration is selected
* @param {Date} [options.timestamp=null] - The timestamp to use * @param {Date} [options.timestamp] - The timestamp to use
* @returns {Promise} A Syslog formatted string according to the selected RFC * @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 * @throws {Error} A standard error object
*/ */
buildMessage(msg, options) { buildMessage(msg, options) {
@ -713,11 +715,13 @@ class RFC3164 extends RFC {
const timestamp = moment(options.timestamp) const timestamp = moment(options.timestamp)
.format('MMM DD hh:mm:ss') .format('MMM DD hh:mm:ss')
.replace(rfc3164DateRegEx, '$1 $5'); .replace(rfc3164DateRegEx, '$1 $5');
const hostname = options.hostname || this.hostname;
const applicationName = options.applicationName || this.applicationName;
// Build message // Build message
fmtMsg = '<' + pri + '>'; fmtMsg = '<' + pri + '>';
fmtMsg += timestamp; fmtMsg += timestamp;
fmtMsg += ' ' + this.hostname; fmtMsg += ' ' + hostname;
fmtMsg += ' ' + this.applicationName; fmtMsg += ' ' + applicationName;
fmtMsg += ' ' + msg; fmtMsg += ' ' + msg;
fmtMsg += newLine; fmtMsg += newLine;
return fmtMsg; return fmtMsg;
@ -1103,7 +1107,9 @@ class RFC5424 extends RFC {
* [name@<private enterprise number> parameter=value] * [name@<private enterprise number> parameter=value]
* @param {number} [options.colorCode=36] - The ANSI color code to use if * @param {number} [options.colorCode=36] - The ANSI color code to use if
* message coloration is selected * 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 * @returns {string} A Syslog formatted string according to the selected RFC
* @throws {Error} A standard error object * @throws {Error} A standard error object
*/ */
@ -1117,6 +1123,8 @@ class RFC5424 extends RFC {
throw new Error(errMsg); throw new Error(errMsg);
} }
let facility = options.facility || 23; let facility = options.facility || 23;
let hostname = options.hostname || this.hostname;
let applicationName = options.applicationName || this.applicationName;
let pid = options.pid || '-'; let pid = options.pid || '-';
let id = options.id || '-'; let id = options.id || '-';
let msgStructuredData = options.msgStructuredData || []; let msgStructuredData = options.msgStructuredData || [];
@ -1223,8 +1231,8 @@ class RFC5424 extends RFC {
fmtMsg = '<' + pri + '>'; fmtMsg = '<' + pri + '>';
fmtMsg += '1'; // Version number fmtMsg += '1'; // Version number
fmtMsg += ' ' + timestamp; fmtMsg += ' ' + timestamp;
fmtMsg += ' ' + this.hostname; fmtMsg += ' ' + hostname;
fmtMsg += ' ' + this.applicationName; fmtMsg += ' ' + applicationName;
fmtMsg += ' ' + pid; fmtMsg += ' ' + pid;
fmtMsg += ' ' + id; fmtMsg += ' ' + id;
fmtMsg += ' ' + structuredData; fmtMsg += ' ' + structuredData;

View File

@ -411,6 +411,14 @@ describe('RFC5424 Class Tests', () => {
const result = rfc5424.buildMessage('hello', { timestamp }); const result = rfc5424.buildMessage('hello', { timestamp });
expect(result.startsWith('<190>1 2020-01-01T01:23:45.678000+00:00 ')).toBe(true); 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', () => { test('RFC5424 SetColors', () => {
let rfc5424 = new SyslogPro.RFC5424(); let rfc5424 = new SyslogPro.RFC5424();
const result = rfc5424.setColor({ const result = rfc5424.setColor({
@ -718,9 +726,16 @@ describe('RFC3164 Class Tests', () => {
const rfc3164 = new SyslogPro.RFC3164(); const rfc3164 = new SyslogPro.RFC3164();
const timestamp = new Date(2020, 0, 1, 1, 23, 45); const timestamp = new Date(2020, 0, 1, 1, 23, 45);
const result = rfc3164.buildMessage('hello', { timestamp }); const result = rfc3164.buildMessage('hello', { timestamp });
console.log(result);
expect(result.startsWith('<190>Jan 1 01:23:45 ')).toBe(true); 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 // Base Syslog Class Test