allow to set timestamp

This commit is contained in:
nkzawa 2019-10-07 22:18:50 +09:00
parent e3f850b923
commit 6a1d9a4cc1
2 changed files with 41 additions and 10 deletions

View File

@ -669,6 +669,7 @@ 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
* @throws {Error} A standard error object
*/
@ -709,7 +710,7 @@ class RFC3164 extends RFC {
// uses BSD timeformat
const rfc3164DateRegEx =
/((A|D|F|J|M|N|O|S)(a|c|e|p|o|u)(b|c|g|l|n|p|r|t|v|y)\s)0(\d\s\d\d:\d\d:\d\d)/;
const timestamp = moment()
const timestamp = moment(options.timestamp)
.format('MMM DD hh:mm:ss')
.replace(rfc3164DateRegEx, '$1 $5');
// Build message
@ -1102,6 +1103,7 @@ class RFC5424 extends RFC {
* [name@<private enterprise number> 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
* @returns {string} A Syslog formatted string according to the selected RFC
* @throws {Error} A standard error object
*/
@ -1144,21 +1146,29 @@ class RFC5424 extends RFC {
}
// RFC5424 timestamp formating
let timestamp = '-';
if (this.timestamp) {
if (this.timestamp || options.timestamp) {
let timeQuality = '[timeQuality';
if (this.timestampUTC) {
timeQuality += ' tzKnown=1';
if (this.timestampMS) {
if (this.timestampTZ) {
timestamp = moment().utc().format('YYYY-MM-DDThh:mm:ss.SSSSSSZ');
timestamp = moment(options.timestamp)
.utc()
.format('YYYY-MM-DDThh:mm:ss.SSSSSSZ');
} else {
timestamp = moment().utc().format('YYYY-MM-DDThh:mm:ss.SSSSSS');
timestamp = moment(options.timestamp)
.utc()
.format('YYYY-MM-DDThh:mm:ss.SSSSSS');
}
} else {
if (this.timestampTZ) {
timestamp = moment().utc().format('YYYY-MM-DDThh:mm:ssZ');
timestamp = moment(options.timestamp)
.utc()
.format('YYYY-MM-DDThh:mm:ssZ');
} else {
timestamp = moment().utc().format('YYYY-MM-DDThh:mm:ss');
timestamp = moment(options.timestamp)
.utc()
.format('YYYY-MM-DDThh:mm:ss');
}
}
} else {
@ -1167,18 +1177,21 @@ class RFC5424 extends RFC {
if (this.timestampMS) {
timeQuality += ' isSynced=1';
timeQuality += ' syncAccuracy=0';
timestamp = moment().format('YYYY-MM-DDThh:mm:ss.SSSSSSZ');
timestamp = moment(options.timestamp)
.format('YYYY-MM-DDThh:mm:ss.SSSSSSZ');
} else {
timestamp = moment().format('YYYY-MM-DDThh:mm:ssZ');
timestamp = moment(options.timestamp)
.format('YYYY-MM-DDThh:mm:ssZ');
}
} else {
timeQuality += ' tzKnown=0';
if (this.timestampMS) {
timeQuality += ' isSynced=1';
timeQuality += ' syncAccuracy=0';
timestamp = moment().format('YYYY-MM-DDThh:mm:ss.SSSSSS');
timestamp = moment(options.timestamp)
.format('YYYY-MM-DDThh:mm:ss.SSSSSS');
} else {
timestamp = moment().format('YYYY-MM-DDThh:mm:ss');
timestamp = moment(options.timestamp).format('YYYY-MM-DDThh:mm:ss');
}
}
}

View File

@ -400,6 +400,17 @@ describe('RFC5424 Class Tests', () => {
resultMsg = /<190>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+/;
expect(result).toMatch(resultMsg);
});
test('RFC5424 BuildMessage with Timestamp options to set date', () => {
const rfc5424 = new SyslogPro.RFC5424({
color: true,
timestampUTC: true,
timestampTZ: true,
timestampMS: true,
});
const timestamp = new Date('2020-01-01T01:23:45.678Z');
const result = rfc5424.buildMessage('hello', { timestamp });
expect(result.startsWith('<190>1 2020-01-01T01:23:45.678000+00:00 ')).toBe(true);
});
test('RFC5424 SetColors', () => {
let rfc5424 = new SyslogPro.RFC5424();
const result = rfc5424.setColor({
@ -703,6 +714,13 @@ describe('RFC3164 Class Tests', () => {
});
expect(result).toMatch(/<190>(J|F|M|A|S|O|N|D).+(\u001b\[36mtest\u001b\[0m\n)/);
});
test('RFC3164 BuildMessage with Timestamp options to set date', () => {
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);
});
});
// Base Syslog Class Test