Skip to content

Commit

Permalink
Custom Producer setting (#82)
Browse files Browse the repository at this point in the history
* added possibility to customize syslog producer

* upd README

* Update README.md

* Update README.md
  • Loading branch information
AlexMost authored and DABH committed Jul 26, 2019
1 parent 8291f7b commit f609ef5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
npm-debug.log
.idea
*.sw*
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ In addition to the options accepted by the syslog (compliant with [RFC 3164][1]

*Metadata:* Logged as string compiled by [glossy][3].

By default, syslog messages are produced by [glossy][3], but you can override that behavior by providing a
custom **Producer** instance via the **customProducer** setting.

## Log Levels
Because syslog only allows a subset of the levels available in [winston][0], levels that do not match will be ignored. Therefore, in order to use `winston-syslog` effectively, you should indicate to [winston][0] that you want to use the syslog levels:

Expand Down
9 changes: 5 additions & 4 deletions lib/winston-syslog.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ class Syslog extends Transport {
// Setup our Syslog and network members for later use.
//
this.socket = null;
this.producer = new glossy.Produce({
type: this.type,
appName: this.appName,
pid: this.pid,
var Producer = options.customProducer || glossy.Produce;
this.producer = new Producer({
type: this.type,
appName: this.appName,
pid: this.pid,
facility: this.facility
});
}
Expand Down
41 changes: 41 additions & 0 deletions test/format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,45 @@ vows.describe('syslog messages').addBatch({
server.close();
}
}
}).addBatch({
'opening fake syslog server': {
topic: function () {
var self = this;
server = dgram.createSocket('udp4');
server.on('listening', function () {
self.callback();
});

server.bind(PORT);
},
'Custom producer': {
topic: function () {
var self = this;
server.once('message', function (msg) {
self.callback(undefined, msg.toString());
});

function CustomProducer() {}
CustomProducer.prototype.produce = function (opts) {
return 'test ' + opts.message;
};

transport = new winston.transports.Syslog({
port: PORT,
customProducer: CustomProducer
});

transport.log('debug', 'ping', null, function (err) {
assert.ifError(err);
});
},
'should apply custom syslog format': function (msg) {
assert.equal(msg, 'test debug: ping');
transport.close();
}
},
teardown: function () {
server.close();
}
}
}).export(module);

0 comments on commit f609ef5

Please sign in to comment.