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

Solve bug leading zero on date format #7

Merged
merged 2 commits into from
Feb 18, 2019
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
8 changes: 5 additions & 3 deletions lib/rfc3164.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ function buildMessage (data, headerBytes) {

module.exports = function ($options) {
options = $options
const zeroReg = /0/

return through2.obj(function transport (data, enc, cb) {
const severity = utils.levelToSeverity(data.level)
const pri = (8 * options.facility) + severity
const tstamp = moment(data.time).tz(options.tz).format('MMM DD HH:mm:ss').replace(zeroReg, ' ')
let timestamp = moment(data.time).tz(options.tz).format('MMM DD HH:mm:ss')
if (timestamp[4] === '0') {
timestamp = timestamp.substr(0, 4) + ' ' + timestamp.substr(5)
}
const hostname = data.hostname.split('.')[ 0 ]
const header = `<${pri}>${tstamp} ${hostname} ${options.appname}[${data.pid}]: `
const header = `<${pri}>${timestamp} ${hostname} ${options.appname}[${data.pid}]: `
const message = buildMessage(data, header.length)
let output = (options.cee && !options.messageOnly) ? `${header}@cee: ${message}` : `${header}${message}`

Expand Down
28 changes: 28 additions & 0 deletions test/3164.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,34 @@ test('sets facility', (t) => {
psyslog.stdin.write(messages.helloWorld + '\n')
})

test('format timestamp with leading zero in days', (t) => {
t.plan(1)
const expected = '<134>Feb 3 02:20:00 MacBook-Pro-3 none[94473]: hello world'
const psyslog = spawn('node', [psyslogPath, '-c', configPath('3164', 'date.json')])

psyslog.stdout.on('data', (data) => {
const msg = data.toString()
t.is(msg, expected)
psyslog.kill()
})

psyslog.stdin.write(messages.leadingDay + '\n')
})

test('format timestamp with trailing zero in days', (t) => {
t.plan(1)
const expected = '<134>Feb 10 02:20:00 MacBook-Pro-3 none[94473]: hello world'
const psyslog = spawn('node', [psyslogPath, '-c', configPath('3164', 'date.json')])

psyslog.stdout.on('data', (data) => {
const msg = data.toString()
t.is(msg, expected)
psyslog.kill()
})

psyslog.stdin.write(messages.trailingDay + '\n')
})

test('sets timezone', (t) => {
t.plan(1)
const expected = '<134>Apr 1 12:44:58 MacBook-Pro-3 none[94473]: hello world'
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/configs/3164/date.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"modern": false,
"tz": "Europe/Zurich",
"messageOnly": true
}

2 changes: 2 additions & 0 deletions test/fixtures/messages.js

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