From 808eb92a2a302aaa2169f4105e658f7e250eed26 Mon Sep 17 00:00:00 2001 From: Nico Kaiser Date: Fri, 29 May 2015 11:26:49 +0200 Subject: [PATCH 1/4] Implement an optional "simple" format that only logs the message instead of the stringified JSON object. --- README.md | 7 +++++++ lib/sys.js | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 33d2bba..1e49204 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,13 @@ This module maps bunyan levels to syslog levels as follows: +--------+--------+ ``` +## Format + +The output format can be set through the `format` option in the constructor. Available levels are: + +- "json": Default. Log entries are sent as JSON. +- "simple": Only the `message` is logged as string. + # License MIT. diff --git a/lib/sys.js b/lib/sys.js index 5b691f2..8939581 100644 --- a/lib/sys.js +++ b/lib/sys.js @@ -100,12 +100,14 @@ function SyslogStream(opts) { assert.object(opts, 'options'); assert.optionalNumber(opts.facility, 'options.facility'); assert.optionalString(opts.name, 'options.name'); + assert.optionalString(opts.format, 'options.format'); Stream.call(this); this.facility = opts.facility || 1; this.name = opts.name || process.title || process.argv[0]; this.writable = true; + this.format = opts.format || 'json'; if (this.constructor.name === 'SyslogStream') { binding.openlog(this.name, binding.LOG_CONS, 0); @@ -152,8 +154,8 @@ SyslogStream.prototype.write = function write(r) { } else if (typeof (r) === 'object') { h = r.hostname; l = level(r.level); - m = JSON.stringify(r, bunyan.safeCycles()); t = time(r.time); + m = formatRecord(r, this.format); } else if (typeof (r) === 'string') { m = r; } else { @@ -190,3 +192,19 @@ SyslogStream.prototype.toString = function toString() { return (str); }; + + +function formatRecord(rec, format) { + var skip = ['hostname', 'level', 'msg', 'name', 'pid', 'time', 'v']; + + switch (format) { + case 'simple': + return rec.msg; + + case 'json': + /* jsl:fall-thru */ + + default: + return JSON.stringify(rec, bunyan.safeCycles()); + } +} From 92dbc339743d2c3812658404a60934bcb9632f8f Mon Sep 17 00:00:00 2001 From: Nico Kaiser Date: Fri, 29 May 2015 14:01:57 +0200 Subject: [PATCH 2/4] Support components in "simple" format. --- lib/sys.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/sys.js b/lib/sys.js index 8939581..6ced22d 100644 --- a/lib/sys.js +++ b/lib/sys.js @@ -199,7 +199,8 @@ function formatRecord(rec, format) { switch (format) { case 'simple': - return rec.msg; + return (rec.component ? rec.component + ': ' : '') + + rec.msg; case 'json': /* jsl:fall-thru */ From 2916dc3fff6859c57bd987f0331389981b281dc0 Mon Sep 17 00:00:00 2001 From: Nico Kaiser Date: Fri, 29 May 2015 14:02:43 +0200 Subject: [PATCH 3/4] Remove unused "skip" variable --- lib/sys.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/sys.js b/lib/sys.js index 6ced22d..906d507 100644 --- a/lib/sys.js +++ b/lib/sys.js @@ -195,8 +195,6 @@ SyslogStream.prototype.toString = function toString() { function formatRecord(rec, format) { - var skip = ['hostname', 'level', 'msg', 'name', 'pid', 'time', 'v']; - switch (format) { case 'simple': return (rec.component ? rec.component + ': ' : '') + From c1d4451c50262b3892331d00098ce5944eba8c5f Mon Sep 17 00:00:00 2001 From: Nico Kaiser Date: Tue, 28 Jul 2015 11:15:44 +0200 Subject: [PATCH 4/4] Use Error.message when using "simple" format --- lib/sys.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/sys.js b/lib/sys.js index 906d507..8b4de75 100644 --- a/lib/sys.js +++ b/lib/sys.js @@ -198,7 +198,8 @@ function formatRecord(rec, format) { switch (format) { case 'simple': return (rec.component ? rec.component + ': ' : '') + - rec.msg; + rec.msg + + ((rec.err && rec.err instanceof Error && rec.msg !== rec.err.message) ? rec.err.toString() : ''); case 'json': /* jsl:fall-thru */