Permalink
Browse files

[api] Basic `nssocket` logger

  • Loading branch information...
mmalecki committed Feb 2, 2012
1 parent ac9d57f commit b6182199555b643870dc46578b4c7e654f9de2b0
Showing with 39 additions and 0 deletions.
  1. +39 −0 lib/winston-nssocket.js
View
@@ -0,0 +1,39 @@
+var util = require('util'),
+ winston = require('winston'),
+ nssocket = require('nssocket');
+
+var NsSocket = exports.NsSocket = function (options) {
+ options = options || {};
+
+ this.name = 'nssocket';
+ this.logEvent = options.logEvent || ['log'];
+
+ this.socket = new nssocket.NsSocket(options.socket || {
+ reconnect: true
+ });
+ this.socket.connect(options.host, options.port);
+};
+util.inherits(NsSocket, winston.Transport);
+
+NsSocket.prototype.log = function (level, msg, meta, callback) {
+ var self = this;
+
+ if (typeof meta == 'function') {
+ callback = meta;
+ meta = {};
+ }
+
+ self.socket.send(self.logEvent, {
+ meta: meta,
+ level: level,
+ message: msg
+ }, function (err) {
+ if (err) {
+ return self.emit('error', err);
+ }
+
+ self.emit('logged');
+ callback && callback();
+ });
+};
+

0 comments on commit b618219

Please sign in to comment.