Permalink
Browse files

Merge pull request #1 from chjj/master

Add querying and streaming
  • Loading branch information...
2 parents 235cc19 + a010e75 commit 0993ea5fb3c7926e181a3e51865556b584a50d77 @mmalecki committed May 10, 2012
Showing with 140 additions and 20 deletions.
  1. +110 −4 lib/winston-nssocket.js
  2. +30 −16 test/winston-nssocket-test.js
@@ -1,8 +1,24 @@
+/*
+ * winston-nssocket.js: Transport for logging to Nssocket
+ *
+ * (C) 2011 Maciej Małecki
+ * (C) 2012 Christopher Jeffrey
+ * MIT LICENSE
+ *
+ */
+
var util = require('util'),
winston = require('winston'),
- nssocket = require('nssocket');
+ nssocket = require('nssocket'),
+ Stream = require('stream').Stream;
-var NsSocket = exports.NsSocket = function (options) {
+//
+// ### function NsSocket (options)
+// #### @options {Object} Options for this instance.
+// Constructor function for the NsSocket transport object responsible
+// for persisting log messages and metadata to a terminal or TTY.
+//
+var NsSocket = exports.Nssocket = function (options) {
options = options || {};
this.name = 'nssocket';
@@ -11,10 +27,49 @@ var NsSocket = exports.NsSocket = function (options) {
this.socket = new nssocket.NsSocket(options.socket || {
reconnect: true
});
+
this.socket.connect(options.host, options.port);
};
+
util.inherits(NsSocket, winston.Transport);
+//
+// Expose the name of this Transport on the prototype
+//
+NsSocket.prototype.name = 'nssocket';
+
+//
+// Legacy Name
+//
+exports.NsSocket = NsSocket;
+
+//
+// Define a getter so that `winston.transports.Nssocket`
+// is available and thus backwards compatible.
+//
+winston.transports.Nssocket = NsSocket;
+
+//
+// Keep uid counter for query requests.
+//
+NsSocket.uid = 0;
+
+//
+// ### function close ()
+// Closes the stream associated with this instance.
+//
+NsSocket.prototype.close = function () {
+ this.socket.destroy();
+};
+
+//
+// ### function log (level, msg, [meta], callback)
+// #### @level {string} Level at which to log the message.
+// #### @msg {string} Message to log
+// #### @meta {Object} **Optional** Additional metadata to attach
+// #### @callback {function} Continuation to respond to when complete.
+// Core logging method exposed to Winston. Metadata is optional.
+//
NsSocket.prototype.log = function (level, msg, meta, callback) {
var self = this;
@@ -31,9 +86,60 @@ NsSocket.prototype.log = function (level, msg, meta, callback) {
if (err) {
return self.emit('error', err);
}
-
+
self.emit('logged');
- callback && callback();
+ if (callback) callback(null, true);
+ });
+};
+
+//
+// ### function query (options, callback)
+// #### @options {Object} Loggly-like query options for this instance.
+// #### @callback {function} Continuation to respond to when complete.
+// Query the transport. Options object is optional.
+//
+NsSocket.prototype.query = function (options, callback) {
+ if (typeof options === 'function') {
+ callback = options;
+ options = {};
+ }
+
+ var self = this,
+ options = this.normalizeQuery(options),
+ id = NsSocket.uid++ + '';
+
+ this.socket.send(['query', id], options);
+
+ this.socket.dataOnce(['result', id], function (log) {
+ if (callback) callback(null, log);
+ });
+
+ this.socket.dataOnce(['error', id], function (err) {
+ if (callback) callback(err);
});
};
+//
+// ### function stream (options)
+// #### @options {Object} Stream options for this instance.
+// Returns a log stream for this transport. Options object is optional.
+//
+NsSocket.prototype.stream = function (options) {
+ var self = this,
+ options = options || {},
+ stream = new Stream;
+
+ stream.destroy = function () {
+ self.socket.undata(['log'], onData);
+ };
+
+ this.socket.send(['stream'], options);
+
+ this.socket.data(['log'], onData);
+
+ function onData(log) {
+ stream.emit('log', log);
+ }
+
+ return stream;
+};
@@ -1,24 +1,38 @@
+/**
+ * winston-nssocket tests
+ */
+
var assert = require('assert'),
vows = require('vows'),
winston = require('winston'),
nssocket = require('nssocket'),
macros = require('./helpers/macros'),
- ns = require('../');
+ ns = require('../'),
+ transport = require('winston/test/transports/transport'),
+ winstond = require('winstond');
+
+//
+// winstond nssocket server
+//
+
+var server = winstond.nssocket.createServer({
+ services: ['collect', 'query', 'stream'],
+ port: 1234
+});
+
+server.add(winstond.transports.File, {
+ filename: __dirname + '/test.log'
+});
+
+server.listen();
-vows.describe('winston-nssocket').addBatch({
- 'When using `winston-nssocket`': {
- topic: function () {
- var logger = new winston.Logger({
- transports: [
- new ns.NsSocket({ host: '127.0.0.1', port: 1234 })
- ]
- });
+//
+// test
+//
- nssocket.createServer(this.callback.bind(this, null, logger)).listen(1234);
- },
- 'it should send logs to `nssocket` server': {
- 'with no meta': macros.log('info', 'hello world!'),
- 'with meta': macros.log('error', 'hello world!', { chill: 'winston!' })
- }
- }
+vows.describe('winston/transports/nssocket').addBatch({
+ 'An instance of the Nssocket Transport': transport(ns.Nssocket, {
+ host: 'localhost',
+ port: 1234
+ })
}).export(module);

0 comments on commit 0993ea5

Please sign in to comment.