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..8b4de75 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) { + switch (format) { + case 'simple': + return (rec.component ? rec.component + ': ' : '') + + rec.msg + + ((rec.err && rec.err instanceof Error && rec.msg !== rec.err.message) ? rec.err.toString() : ''); + + case 'json': + /* jsl:fall-thru */ + + default: + return JSON.stringify(rec, bunyan.safeCycles()); + } +}