├── .gitignore ├── Makefile ├── Readme.md ├── examples ├── client.js └── server.js ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: 3 | @NODE_ENV=test ./node_modules/.bin/mocha \ 4 | --require should \ 5 | --reporter dot \ 6 | --bail 7 | 8 | .PHONY: test -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # yal-server 3 | 4 | Extensible log server for [YAL](https://github.com/segmentio/yal). 5 | 6 | ## Installation 7 | 8 | ``` 9 | $ npm install yal-server 10 | ``` 11 | 12 | ## Example 13 | 14 | Running YAL with a simple stdout plugin: 15 | 16 | ```js 17 | var Server = require('yal-server'); 18 | var server = new Server; 19 | 20 | server.bind('tcp://localhost:5000'); 21 | server.use(stdout); 22 | 23 | function stdout(server){ 24 | server.on('message', function(msg){ 25 | console.log(msg); 26 | }); 27 | }; 28 | ``` 29 | 30 | ## Plugins 31 | 32 | Plugins are simply functions that accept the `server` instance, 33 | so you can listen on "message" events and do whatever you like. 34 | 35 | # License 36 | 37 | MIT -------------------------------------------------------------------------------- /examples/client.js: -------------------------------------------------------------------------------- 1 | 2 | var Logger = require('yal'); 3 | var log = new Logger('tcp://localhost:5000'); 4 | 5 | setInterval(function(){ 6 | log.info('login', { user: 'tobi' }); 7 | }, 150); 8 | 9 | setInterval(function(){ 10 | log.info('login', { user: 'jane' }); 11 | }, 1500); -------------------------------------------------------------------------------- /examples/server.js: -------------------------------------------------------------------------------- 1 | 2 | var Server = require('..'); 3 | var server = new Server; 4 | 5 | server.bind('tcp://localhost:5000'); 6 | server.use(stdout); 7 | 8 | function stdout(server){ 9 | server.on('message', function(msg){ 10 | console.log(msg); 11 | }); 12 | }; -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Module dependencies. 4 | */ 5 | 6 | var Emitter = require('events').EventEmitter; 7 | var assert = require('assert'); 8 | var axon = require('axon'); 9 | 10 | /** 11 | * Expose `Server`. 12 | */ 13 | 14 | module.exports = Server; 15 | 16 | /** 17 | * Initialize server. 18 | * 19 | * @api public 20 | */ 21 | 22 | function Server() { 23 | this.sock = axon.socket('pull'); 24 | this.onmessage = this.onmessage.bind(this); 25 | this.sock.on('message', this.onmessage); 26 | } 27 | 28 | /** 29 | * Inherit from `Emitter.prototype`. 30 | */ 31 | 32 | Server.prototype.__proto__ = Emitter.prototype; 33 | 34 | /** 35 | * Handle messages. 36 | */ 37 | 38 | Server.prototype.onmessage = function(msg){ 39 | this.emit('message', msg); 40 | }; 41 | 42 | /** 43 | * Use the given plugin `fn`. 44 | * 45 | * @param {Function} fn 46 | * @return {Server} self 47 | * @api public 48 | */ 49 | 50 | Server.prototype.use = function(fn){ 51 | fn(this); 52 | return this; 53 | }; 54 | 55 | /** 56 | * Bind to `addr`. 57 | * 58 | * @param {String} addr 59 | * @api public 60 | */ 61 | 62 | Server.prototype.bind = function(addr){ 63 | assert(addr, 'address required'); 64 | this.sock.bind(addr); 65 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yal-server", 3 | "version": "1.0.0", 4 | "repository": "segmentio/yal-server", 5 | "description": "extensible log server for YAL", 6 | "keywords": [ 7 | "yal", 8 | "logger", 9 | "log", 10 | "server" 11 | ], 12 | "dependencies": { 13 | "axon": "~1.0.0" 14 | }, 15 | "devDependencies": { 16 | "yal": "~1.0.0", 17 | "mocha": "*", 18 | "should": "*" 19 | }, 20 | "license": "MIT" 21 | } 22 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 2 | var Logger = require('yal'); 3 | var Server = require('..'); 4 | 5 | describe('Server#use(fn)', function(){ 6 | it('should registry a plugin', function(done){ 7 | var server = new Server; 8 | 9 | server.bind('tcp://localhost:5000'); 10 | 11 | server.use(function(server){ 12 | server.on('message', function(msg){ 13 | msg.should.have.property('timestamp'); 14 | msg.should.have.property('hostname'); 15 | msg.should.have.property('message'); 16 | msg.message.should.eql({ here: 'weee' }); 17 | done(); 18 | }); 19 | }); 20 | 21 | var log = new Logger('tcp://localhost:5000'); 22 | log.info('something', { here: 'weee' }); 23 | }) 24 | }) --------------------------------------------------------------------------------