Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add querying and streaming #1

Merged
merged 6 commits into from

2 participants

@chjj
Collaborator

This has been moved out of my winston branch. Adds querying and streaming, with the corresponding tests, among other things.

@mmalecki mmalecki merged commit 0993ea5 into from
@mmalecki
Owner

Merged, thanks Christopher!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 140 additions and 20 deletions.
  1. +110 −4 lib/winston-nssocket.js
  2. +30 −16 test/winston-nssocket-test.js
View
114 lib/winston-nssocket.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;
+};
View
46 test/winston-nssocket-test.js
@@ -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);
Something went wrong with that request. Please try again.