Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Less vapor, more ware

  • Loading branch information...
commit a12976864d70ac7bf12db861e8250a273a673cfe 1 parent fe3ebe4
@saikat authored
Showing with 32 additions and 8 deletions.
  1. +15 −8 README.md
  2. +1 −0  index.js
  3. +16 −0 lib/socket-logger.js
View
23 README.md
@@ -1,11 +1,11 @@
Socket Logger: Real-time, JSON-parsable logging for [Socket.IO](http://github.com/learnboost/socket.io-node)
===============================================================
-By default, socket logger is a simple logger that you can use with your [Socket.IO](http://github.com/learnboost/socket.io-node)-based node server.
+The code is really simple and fairly trivial right now, but is enough for my immediate needs. See notes below for where I want to take it.
-This is very very early alpha software with an API that I definitely want to change. Eventually, you won't have to make any explicit log calls at all - socket-logger will just act as a middleware for your Socket.IO server and automatically log incomming connections and messages (something like how Connect or JSGI works). However, you should still have the ability to log whatever custom calls you want too.
+By default, socket logger is a simple logger that you can use with your [Socket.IO](http://github.com/learnboost/socket.io-node)-based node server.
-It's a little different from other loggers in that you can log not just connections and disconnections, but also each message. Also, the log format is not Apache Common Log Format, but instead a JSON-parsable format.
+It's a little different from other loggers in that you can log not just connections and disconnections, but also each message. Also, the log format is not Apache Common Log Format, but instead JSON-parsable strings. So, you log JS objects, not strings.
The secret sauce of socket-logger is how easy it makes it to set up a web client that gets information about your server. Simply create a Socket.IO client that connects to your server with a specified auth token, and now your client gets JSON messages about all the socket connections and messages your server is handling. I'll have an example client up soon.
@@ -15,7 +15,7 @@ How to use
You will need to install [Socket.IO-node](http://github.com/learnboost/socket.io-node) and have a socket-io server running. Then, clone this repo. Now put this in your server and smoke it:
var http = require('http'),
- log = require('./path/to/socket-logger'),
+ logger = require('./path/to/socket-logger'),
io = require('./path/to/socket.io'),
server = http.createServer(function(req, res){
@@ -26,17 +26,24 @@ You will need to install [Socket.IO-node](http://github.com/learnboost/socket.io
var AUTH_TOKEN = 'my_secret_token_for_the_dashboard_client';
socket.on('connection', function(client){
- log({'action' : 'connection', 'client' : client.sessionId});
+ logger.log({'action' : 'connection', 'client' : client.sessionId});
client.on('message', function(message) {
- log({'action' : 'message', 'client' : client.sessionId, 'body' : message});
+ logger.log({'action' : 'message', 'client' : client.sessionId, 'body' : message});
var parsedMsg = JSON.parse(message);
if (parsedMsg.auth_token === AUTH_TOKEN)
- log.socketLogger = client;
+ logger.socketLogger = client;
});
client.on('disconnect', function() {
- log({'action' : 'disconnect', 'client' : client.sessionId});
+ logger.log({'action' : 'disconnect', 'client' : client.sessionId});
});
});
+
+Caveats and Notes
+=================
+
+This is very very early alpha software with an API that I definitely want to change. Eventually, you won't have to make any explicit log calls at all - socket-logger will just act as a middleware for your Socket.IO server and automatically log incomming connections and messages (something like how Connect or JSGI works). However, you should still have the ability to log whatever custom calls you want too.
+
+Even more eventually, it'd be kind of cool to have the client-side be some kind of web service. Real-time socket-based analytics for all?
License
=======
View
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/socket-logger');
View
16 lib/socket-logger.js
@@ -0,0 +1,16 @@
+function makeLogger(options) {
+ var logger = {};
+ logger.options = options || {};
+ logger.socketLogger = null;
+ logger.log = function(msg, logOptions) {
+ var serializedMsg = JSON.stringify(msg),
+ stream = logger.options.stream || process.stdout;
+ stream.write(serializedMsg, 'utf8');
+ if (logger.socketLogger && logger.socketLogger.send) {
+ logger.socketLogger.send(serializedMsg);
+ }
+ }
+ return logger;
+}
+
+module.exports = makeLogger();
Please sign in to comment.
Something went wrong with that request. Please try again.