Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pad milliseconds with three zeroes #1

Merged
merged 9 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Source: https://github.com/actions/starter-workflows/blob/main/ci/node.js.yml

name: CI

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [0.10.x, 0.12.x, 4.x, 6.x, 8.x, 10.x, 12.x, 14.x, 16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm test
37 changes: 17 additions & 20 deletions lib/glossy/produce.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ function generateBSDDate(dateObject) {

/*
* Generate date in RFC 3339 format. If no date is supplied, the default is
* the current time in GMT + 0.
* the current time in local timezone.
* @param {Date} dateObject optional Date object
* @returns {String} formatted date
*/
Expand All @@ -364,11 +364,8 @@ function generateDate(dateObject) {
// Calcutate the offset
var timeOffset;
var minutes = Math.abs(dateObject.getTimezoneOffset());
var hours = 0;
while(minutes >= 60) {
hours++;
minutes -= 60;
}
var hours = Math.floor(Math.abs(minutes) / 60);
minutes = Math.abs(minutes) % 60;

if(dateObject.getTimezoneOffset() < 0) {
// Ahead of UTC
Expand All @@ -383,20 +380,20 @@ function generateDate(dateObject) {


// Date
var formattedDate = dateObject.getUTCFullYear() + '-' +
// N.B. Javascript Date objects return months of the year indexed from
// zero, while the RFC 5424 syslog standard expects months indexed from
// one.
leadZero(dateObject.getMonth() + 1) + '-' +
// N.B. Javascript Date objects return days of the month indexed from one
// (unlike months of year), so this does not need any correction.
leadZero(dateObject.getDate()) + 'T' +
// Time
leadZero(dateObject.getHours()) + ':' +
leadZero(dateObject.getMinutes()) + ':' +
leadZero(dateObject.getSeconds()) + '.' +
leadZero(dateObject.getMilliseconds()) +
timeOffset;
var formattedDate = dateObject.getFullYear() + '-' +
// N.B. Javascript Date objects return months of the year indexed from
// zero, while the RFC 5424 syslog standard expects months indexed from
// one.
leadZero(dateObject.getMonth() + 1) + '-' +
// N.B. Javascript Date objects return days of the month indexed from one
// (unlike months of year), so this does not need any correction.
leadZero(dateObject.getDate()) + 'T' +
// Time
leadZero(dateObject.getHours()) + ':' +
leadZero(dateObject.getMinutes()) + ':' +
leadZero(dateObject.getSeconds()) + '.' +
(dateObject.getMilliseconds() / 1000).toFixed(3).slice(2, 5) +
timeOffset;

return formattedDate;

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@myndzi/glossy",
"version": "0.1.10",
"version": "0.1.11",
"description": "Syslog parser and producer",
"keywords": [
"syslog",
"logging"
],
"url": "http://github.com/squeeks/glossy",
"url": "http://github.com/myndzi/glossy",
"main": "./index.js",
"author": "Squeeks <[email protected]>",
"maintainers": [
Expand Down Expand Up @@ -58,10 +58,7 @@
},
"repository": {
"type": "git",
"url": "http://github.com/squeeks/glossy.git"
},
"bugs": {
"url": "http://github.com/squeeks/glossy/issues"
"url": "http://github.com/myndzi/glossy.git"
},
"licenses": [
{
Expand All @@ -70,6 +67,6 @@
}
],
"engines": {
"node": ">= 0.2.5"
"node": ">= 0.10.0"
}
}
16 changes: 8 additions & 8 deletions test/produce.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ var msg = syslogProducer.produce({
date: new Date(1234567890000),
message: 'Test Message'
});
assert.equal(msg, "<163>1 2009-02-14T00:31:30.00+01:00 localhost sudo 123 - - Test Message",'Valid message returned');
assert.equal(msg, "<163>1 2009-02-14T00:31:30.000+01:00 localhost sudo 123 - - Test Message",'Valid message returned');

syslogProducer.produce({
facility: 'audit',
severity: 'error',
host: '127.0.0.1',
appName: 'sudo',
pid: '419',
date: new Date(1234567890000),
date: new Date(1234567890001),
message: 'Test Message'
}, function(cbMsg) {
assert.equal(cbMsg, '<107>1 2009-02-14T00:31:30.00+01:00 127.0.0.1 sudo 419 - - Test Message', 'Valid message in callback returned');
assert.equal(cbMsg, '<107>1 2009-02-14T00:31:30.001+01:00 127.0.0.1 sudo 419 - - Test Message', 'Valid message in callback returned');
});

BSDProducer.produce({
Expand Down Expand Up @@ -137,7 +137,7 @@ var structuredMsg = syslogProducer.produce({
host: 'mymachine.example.com',
appName: 'evntslog',
msgID: 'ID47',
date: new Date(1234567890000),
date: new Date(1234567890010),
structuredData: {
'exampleSDID@32473': {
'iut': "3",
Expand All @@ -150,25 +150,25 @@ var structuredMsg = syslogProducer.produce({
});

assert.ok(structuredMsg);
assert.equal(structuredMsg, '<163>1 2009-02-14T00:31:30.00+01:00 mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011" seqNo="1"] BOMAn application event log entry...');
assert.equal(structuredMsg, '<163>1 2009-02-14T00:31:30.010+01:00 mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011" seqNo="1"] BOMAn application event log entry...');

var structuredWithArray = syslogProducer.produce({
facility: 'local4',
severity: 'error',
host: 'mymachine.example.com',
appName: 'evntslog',
msgID: 'ID47',
date: new Date(1234567890000),
date: new Date(1234567890100),
structuredData: {
'origin': {
'ip': ['127.0.1.1', '127.0.0.1']
}
},
message: 'BOMAn application event log entry...'
message: 'BOMAn application event log entry...'
});

assert.ok(structuredWithArray);
assert.equal(structuredWithArray, '<163>1 2009-02-14T00:31:30.00+01:00 mymachine.example.com evntslog - ID47 [origin ip="127.0.1.1" ip="127.0.0.1"] BOMAn application event log entry...');
assert.equal(structuredWithArray, '<163>1 2009-02-14T00:31:30.100+01:00 mymachine.example.com evntslog - ID47 [origin ip="127.0.1.1" ip="127.0.0.1"] BOMAn application event log entry...');

var messageWithOneDigitDate = presetProducer.emergency({
facility: 'news',
Expand Down