mirror of
https://github.com/misskey-dev/SyslogPro.git
synced 2025-04-29 02:37:18 +09:00
allow to set timestamp
This commit is contained in:
parent
e3f850b923
commit
6a1d9a4cc1
33
index.js
33
index.js
@ -669,6 +669,7 @@ 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
|
||||||
* @returns {Promise} A Syslog formatted string according to the selected RFC
|
* @returns {Promise} A Syslog formatted string according to the selected RFC
|
||||||
* @throws {Error} A standard error object
|
* @throws {Error} A standard error object
|
||||||
*/
|
*/
|
||||||
@ -709,7 +710,7 @@ class RFC3164 extends RFC {
|
|||||||
// uses BSD timeformat
|
// uses BSD timeformat
|
||||||
const rfc3164DateRegEx =
|
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)/;
|
/((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')
|
.format('MMM DD hh:mm:ss')
|
||||||
.replace(rfc3164DateRegEx, '$1 $5');
|
.replace(rfc3164DateRegEx, '$1 $5');
|
||||||
// Build message
|
// Build message
|
||||||
@ -1102,6 +1103,7 @@ 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
|
||||||
* @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
|
||||||
*/
|
*/
|
||||||
@ -1144,21 +1146,29 @@ class RFC5424 extends RFC {
|
|||||||
}
|
}
|
||||||
// RFC5424 timestamp formating
|
// RFC5424 timestamp formating
|
||||||
let timestamp = '-';
|
let timestamp = '-';
|
||||||
if (this.timestamp) {
|
if (this.timestamp || options.timestamp) {
|
||||||
let timeQuality = '[timeQuality';
|
let timeQuality = '[timeQuality';
|
||||||
if (this.timestampUTC) {
|
if (this.timestampUTC) {
|
||||||
timeQuality += ' tzKnown=1';
|
timeQuality += ' tzKnown=1';
|
||||||
if (this.timestampMS) {
|
if (this.timestampMS) {
|
||||||
if (this.timestampTZ) {
|
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 {
|
} else {
|
||||||
timestamp = moment().utc().format('YYYY-MM-DDThh:mm:ss.SSSSSS');
|
timestamp = moment(options.timestamp)
|
||||||
|
.utc()
|
||||||
|
.format('YYYY-MM-DDThh:mm:ss.SSSSSS');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (this.timestampTZ) {
|
if (this.timestampTZ) {
|
||||||
timestamp = moment().utc().format('YYYY-MM-DDThh:mm:ssZ');
|
timestamp = moment(options.timestamp)
|
||||||
|
.utc()
|
||||||
|
.format('YYYY-MM-DDThh:mm:ssZ');
|
||||||
} else {
|
} else {
|
||||||
timestamp = moment().utc().format('YYYY-MM-DDThh:mm:ss');
|
timestamp = moment(options.timestamp)
|
||||||
|
.utc()
|
||||||
|
.format('YYYY-MM-DDThh:mm:ss');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -1167,18 +1177,21 @@ class RFC5424 extends RFC {
|
|||||||
if (this.timestampMS) {
|
if (this.timestampMS) {
|
||||||
timeQuality += ' isSynced=1';
|
timeQuality += ' isSynced=1';
|
||||||
timeQuality += ' syncAccuracy=0';
|
timeQuality += ' syncAccuracy=0';
|
||||||
timestamp = moment().format('YYYY-MM-DDThh:mm:ss.SSSSSSZ');
|
timestamp = moment(options.timestamp)
|
||||||
|
.format('YYYY-MM-DDThh:mm:ss.SSSSSSZ');
|
||||||
} else {
|
} else {
|
||||||
timestamp = moment().format('YYYY-MM-DDThh:mm:ssZ');
|
timestamp = moment(options.timestamp)
|
||||||
|
.format('YYYY-MM-DDThh:mm:ssZ');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
timeQuality += ' tzKnown=0';
|
timeQuality += ' tzKnown=0';
|
||||||
if (this.timestampMS) {
|
if (this.timestampMS) {
|
||||||
timeQuality += ' isSynced=1';
|
timeQuality += ' isSynced=1';
|
||||||
timeQuality += ' syncAccuracy=0';
|
timeQuality += ' syncAccuracy=0';
|
||||||
timestamp = moment().format('YYYY-MM-DDThh:mm:ss.SSSSSS');
|
timestamp = moment(options.timestamp)
|
||||||
|
.format('YYYY-MM-DDThh:mm:ss.SSSSSS');
|
||||||
} else {
|
} else {
|
||||||
timestamp = moment().format('YYYY-MM-DDThh:mm:ss');
|
timestamp = moment(options.timestamp).format('YYYY-MM-DDThh:mm:ss');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -400,6 +400,17 @@ describe('RFC5424 Class Tests', () => {
|
|||||||
resultMsg = /<190>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+/;
|
resultMsg = /<190>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+/;
|
||||||
expect(result).toMatch(resultMsg);
|
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', () => {
|
test('RFC5424 SetColors', () => {
|
||||||
let rfc5424 = new SyslogPro.RFC5424();
|
let rfc5424 = new SyslogPro.RFC5424();
|
||||||
const result = rfc5424.setColor({
|
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)/);
|
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
|
// Base Syslog Class Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user