Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1 from chjj/master

Add querying and streaming
  • Loading branch information...
commit 0993ea5fb3c7926e181a3e51865556b584a50d77 2 parents 235cc19 + a010e75
Maciej Małecki authored

Showing 2 changed files with 140 additions and 20 deletions. Show diff stats Hide diff stats

  1. +110 4 lib/winston-nssocket.js
  2. +30 16 test/winston-nssocket-test.js
114 lib/winston-nssocket.js
... ... @@ -1,8 +1,24 @@
  1 +/*
  2 + * winston-nssocket.js: Transport for logging to Nssocket
  3 + *
  4 + * (C) 2011 Maciej Małecki
  5 + * (C) 2012 Christopher Jeffrey
  6 + * MIT LICENSE
  7 + *
  8 + */
  9 +
1 10 var util = require('util'),
2 11 winston = require('winston'),
3   - nssocket = require('nssocket');
  12 + nssocket = require('nssocket'),
  13 + Stream = require('stream').Stream;
4 14
5   -var NsSocket = exports.NsSocket = function (options) {
  15 +//
  16 +// ### function NsSocket (options)
  17 +// #### @options {Object} Options for this instance.
  18 +// Constructor function for the NsSocket transport object responsible
  19 +// for persisting log messages and metadata to a terminal or TTY.
  20 +//
  21 +var NsSocket = exports.Nssocket = function (options) {
6 22 options = options || {};
7 23
8 24 this.name = 'nssocket';
@@ -11,10 +27,49 @@ var NsSocket = exports.NsSocket = function (options) {
11 27 this.socket = new nssocket.NsSocket(options.socket || {
12 28 reconnect: true
13 29 });
  30 +
14 31 this.socket.connect(options.host, options.port);
15 32 };
  33 +
16 34 util.inherits(NsSocket, winston.Transport);
17 35
  36 +//
  37 +// Expose the name of this Transport on the prototype
  38 +//
  39 +NsSocket.prototype.name = 'nssocket';
  40 +
  41 +//
  42 +// Legacy Name
  43 +//
  44 +exports.NsSocket = NsSocket;
  45 +
  46 +//
  47 +// Define a getter so that `winston.transports.Nssocket`
  48 +// is available and thus backwards compatible.
  49 +//
  50 +winston.transports.Nssocket = NsSocket;
  51 +
  52 +//
  53 +// Keep uid counter for query requests.
  54 +//
  55 +NsSocket.uid = 0;
  56 +
  57 +//
  58 +// ### function close ()
  59 +// Closes the stream associated with this instance.
  60 +//
  61 +NsSocket.prototype.close = function () {
  62 + this.socket.destroy();
  63 +};
  64 +
  65 +//
  66 +// ### function log (level, msg, [meta], callback)
  67 +// #### @level {string} Level at which to log the message.
  68 +// #### @msg {string} Message to log
  69 +// #### @meta {Object} **Optional** Additional metadata to attach
  70 +// #### @callback {function} Continuation to respond to when complete.
  71 +// Core logging method exposed to Winston. Metadata is optional.
  72 +//
18 73 NsSocket.prototype.log = function (level, msg, meta, callback) {
19 74 var self = this;
20 75
@@ -31,9 +86,60 @@ NsSocket.prototype.log = function (level, msg, meta, callback) {
31 86 if (err) {
32 87 return self.emit('error', err);
33 88 }
34   -
  89 +
35 90 self.emit('logged');
36   - callback && callback();
  91 + if (callback) callback(null, true);
  92 + });
  93 +};
  94 +
  95 +//
  96 +// ### function query (options, callback)
  97 +// #### @options {Object} Loggly-like query options for this instance.
  98 +// #### @callback {function} Continuation to respond to when complete.
  99 +// Query the transport. Options object is optional.
  100 +//
  101 +NsSocket.prototype.query = function (options, callback) {
  102 + if (typeof options === 'function') {
  103 + callback = options;
  104 + options = {};
  105 + }
  106 +
  107 + var self = this,
  108 + options = this.normalizeQuery(options),
  109 + id = NsSocket.uid++ + '';
  110 +
  111 + this.socket.send(['query', id], options);
  112 +
  113 + this.socket.dataOnce(['result', id], function (log) {
  114 + if (callback) callback(null, log);
  115 + });
  116 +
  117 + this.socket.dataOnce(['error', id], function (err) {
  118 + if (callback) callback(err);
37 119 });
38 120 };
39 121
  122 +//
  123 +// ### function stream (options)
  124 +// #### @options {Object} Stream options for this instance.
  125 +// Returns a log stream for this transport. Options object is optional.
  126 +//
  127 +NsSocket.prototype.stream = function (options) {
  128 + var self = this,
  129 + options = options || {},
  130 + stream = new Stream;
  131 +
  132 + stream.destroy = function () {
  133 + self.socket.undata(['log'], onData);
  134 + };
  135 +
  136 + this.socket.send(['stream'], options);
  137 +
  138 + this.socket.data(['log'], onData);
  139 +
  140 + function onData(log) {
  141 + stream.emit('log', log);
  142 + }
  143 +
  144 + return stream;
  145 +};
46 test/winston-nssocket-test.js
... ... @@ -1,24 +1,38 @@
  1 +/**
  2 + * winston-nssocket tests
  3 + */
  4 +
1 5 var assert = require('assert'),
2 6 vows = require('vows'),
3 7 winston = require('winston'),
4 8 nssocket = require('nssocket'),
5 9 macros = require('./helpers/macros'),
6   - ns = require('../');
  10 + ns = require('../'),
  11 + transport = require('winston/test/transports/transport'),
  12 + winstond = require('winstond');
  13 +
  14 +//
  15 +// winstond nssocket server
  16 +//
  17 +
  18 +var server = winstond.nssocket.createServer({
  19 + services: ['collect', 'query', 'stream'],
  20 + port: 1234
  21 +});
  22 +
  23 +server.add(winstond.transports.File, {
  24 + filename: __dirname + '/test.log'
  25 +});
  26 +
  27 +server.listen();
7 28
8   -vows.describe('winston-nssocket').addBatch({
9   - 'When using `winston-nssocket`': {
10   - topic: function () {
11   - var logger = new winston.Logger({
12   - transports: [
13   - new ns.NsSocket({ host: '127.0.0.1', port: 1234 })
14   - ]
15   - });
  29 +//
  30 +// test
  31 +//
16 32
17   - nssocket.createServer(this.callback.bind(this, null, logger)).listen(1234);
18   - },
19   - 'it should send logs to `nssocket` server': {
20   - 'with no meta': macros.log('info', 'hello world!'),
21   - 'with meta': macros.log('error', 'hello world!', { chill: 'winston!' })
22   - }
23   - }
  33 +vows.describe('winston/transports/nssocket').addBatch({
  34 + 'An instance of the Nssocket Transport': transport(ns.Nssocket, {
  35 + host: 'localhost',
  36 + port: 1234
  37 + })
24 38 }).export(module);

0 comments on commit 0993ea5

Please sign in to comment.
Something went wrong with that request. Please try again.