Skip to content
Open
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
20 changes: 19 additions & 1 deletion lib/sys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}
}