Skip to content

Commit

Permalink
better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Tahler committed Aug 26, 2012
1 parent b628516 commit 2a8e990
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 20 deletions.
1 change: 0 additions & 1 deletion initializers/initActions.js
Expand Up @@ -53,7 +53,6 @@ var initActions = function(api, next)
api.processAction = function(api, connection, messageID, next){
if(connection.params.limit == null){ connection.params.limit = api.configData.general.defaultLimit; }else{ connection.params.limit = parseFloat(connection.params.limit); }
if(connection.params.offset == null){ connection.params.offset = api.configData.general.defaultOffset; }else{ connection.params.offset = parseFloat(connection.params.offset); }
if(api.configData.log.logRequests){api.log("action @ " + connection.remoteIP + " | params: " + JSON.stringify(connection.params));}

if (connection.error === false){
connection.action = connection.params["action"];
Expand Down
22 changes: 22 additions & 0 deletions initializers/initFileServer.js
Expand Up @@ -64,6 +64,28 @@ var initFileServer = function(api, next){
connection.messageCount++;
}catch(e){}
}
if(api.configData.log.logRequests){
var full_url = null;
var duration = null;
var type = null;
if(connection.req != null && connection.req.headers != null){
full_url = connection.req.headers.host + connection.req.url
duration = new Date().getTime() - connection.timer.startTime;
type = "web";
}else{
type = "socket";
full_url = type;
duration = type;
}
api.logJSON({
label: "file @ " + type,
to: connection.remoteIP,
file: file,
request: full_url,
size: data.length,
duration: duration
}, "grey");
}
}
process.nextTick(function() { next(connection, false); });
});
Expand Down
17 changes: 17 additions & 0 deletions initializers/initLog.js
Expand Up @@ -61,6 +61,23 @@ var initLog = function(api, next){
});
}
};

api.logJSON = function(J, color){
var str = "";
if(J.label != null){
str += "[" + J.label + "] ";
delete J.label;
}
var need_bar = false;
for (var i in J){
if(need_bar == true){
str += " | "
}
str += i + ": " + J[i];
need_bar = true;
}
api.log(str, color);
}

next();
}
Expand Down
96 changes: 84 additions & 12 deletions initializers/initSocketServer.js
Expand Up @@ -33,7 +33,12 @@ var initSocketServer = function(api, next){

connection.on("connect", function () {
api.stats.incrament(api, "numberOfActiveSocketClients");
api.log("socket connection "+connection.remoteIP+" | connected");
if(api.configData.log.logRequests){
api.logJSON({
label: "connect @ socket",
to: connection.remoteIP,
});
}
api.chatRoom.roomAddMember(api, connection);
process.nextTick(function(){
api.socketServer.sendSocketMessage(connection, {welcome: api.configData.general.welcomeMessage, room: connection.room, context: "api"});
Expand All @@ -54,7 +59,12 @@ var initSocketServer = function(api, next){
if(line.indexOf("\u0004") > -1){ } // trap for break chars; do nothing
else if(words[0] == "quit" || words[0] == "exit" || words[0] == "close" ){
try{
if(api.configData.log.logRequests){api.log(" > socket request from " + connection.remoteIP + " | requesting disconnect", "white");}
if(api.configData.log.logRequests){
api.logJSON({
label: "quit @ socket",
to: connection.remoteIP,
});
}
api.socketServer.sendSocketMessage(connection, {status: "Bye!", context: "response"});
connection.end();
}catch(e){ }
Expand All @@ -66,41 +76,89 @@ var initSocketServer = function(api, next){
}else{
api.socketServer.sendSocketMessage(connection, {status: "Cannot set null", context: "response"});
}
if(api.configData.log.logRequests){api.log(" > socket request from " + connection.remoteIP + " | "+line, "grey");}
if(api.configData.log.logRequests){
api.logJSON({
label: "paramAdd @ socket",
to: connection.remoteIP,
params: JSON.stringify(words),
}, "grey");
}
}else if(words[0] == "paramDelete"){
connection.params[words[1]] = null;
api.socketServer.sendSocketMessage(connection, {status: "OK", context: "response"});
if(api.configData.log.logRequests){api.log(" > socket request from " + connection.remoteIP + " | "+line, "grey");}
if(api.configData.log.logRequests){
api.logJSON({
label: "paramDelete @ socket",
to: connection.remoteIP,
params: JSON.stringify(words),
}, "grey");
}
}else if(words[0] == "paramView"){
var q = words[1];
var params = {}
params[q] = connection.params[q];
api.socketServer.sendSocketMessage(connection, {context: "response", params: params});
if(api.configData.log.logRequests){api.log(" > socket request from " + connection.remoteIP + " | "+line, "grey");}
if(api.configData.log.logRequests){
api.logJSON({
label: "paramView @ socket",
to: connection.remoteIP,
params: JSON.stringify(words),
}, "grey");
}
}else if(words[0] == "paramsView"){
api.socketServer.sendSocketMessage(connection, {context: "response", params: connection.params});
if(api.configData.log.logRequests){api.log(" > socket request from " + connection.remoteIP + " | "+line, "grey");}
if(api.configData.log.logRequests){
api.logJSON({
label: "paramsView @ socket",
to: connection.remoteIP,
params: JSON.stringify(words),
}, "grey");
}
}else if(words[0] == "paramsDelete"){
connection.params = {};
api.socketServer.sendSocketMessage(connection, {context: "response", status: "OK"});
if(api.configData.log.logRequests){api.log(" > socket request from " + connection.remoteIP + " | "+line, "grey");}
if(api.configData.log.logRequests){
api.logJSON({
label: "paramsDelete @ socket",
to: connection.remoteIP,
params: JSON.stringify(words),
}, "grey");
}
}else if(words[0] == "roomChange"){
api.chatRoom.roomRemoveMember(api, connection, function(){
connection.room = words[1];
api.chatRoom.roomAddMember(api, connection);
api.socketServer.sendSocketMessage(connection, {context: "response", status: "OK", room: connection.room});
if(api.configData.log.logRequests){api.log(" > socket request from " + connection.remoteIP + " | "+line, "grey");}
if(api.configData.log.logRequests){
api.logJSON({
label: "roomChange @ socket",
to: connection.remoteIP,
params: JSON.stringify(words),
}, "grey");
}
});
}else if(words[0] == "roomView"){
api.chatRoom.socketRoomStatus(api, connection.room, function(roomStatus){
api.socketServer.sendSocketMessage(connection, {context: "response", status: "OK", room: connection.room, roomStatus: roomStatus});
if(api.configData.log.logRequests){api.log(" > socket request from " + connection.remoteIP + " | "+line, "grey");}
if(api.configData.log.logRequests){
api.logJSON({
label: "roomView @ socket",
to: connection.remoteIP,
params: JSON.stringify(words),
}, "grey");
}
});
}else if(words[0] == "say"){
var message = line.substr(4);
api.chatRoom.socketRoomBroadcast(api, connection, message);
api.socketServer.sendSocketMessage(connection, {context: "response", status: "OK"});
if(api.configData.log.logRequests){api.log(" > socket request from " + connection.remoteIP + " | "+line, "grey");}
if(api.configData.log.logRequests){
api.logJSON({
label: "say @ socket",
to: connection.remoteIP,
params: JSON.stringify(words),
}, "grey");
}
}else{
connection.error = false;
connection.actionStartTime = new Date().getTime();
Expand All @@ -109,7 +167,15 @@ var initSocketServer = function(api, next){
connection.params["action"] = words[0];
api.processAction(api, connection, connection.messageCount, function(connection, cont){
var delta = new Date().getTime() - connection.actionStartTime;
if(api.configData.log.logRequests){api.log(" > socket request from " + connection.remoteIP + " | "+line + " | responded in "+delta+"ms" , "grey");}
if(api.configData.log.logRequests && connection.action != "file"){
api.logJSON({
label: "action @ socket",
to: connection.remoteIP,
action: connection.action,
params: JSON.stringify(connection.params),
duration: delta,
});
}
api.socketServer.respondToSocketClient(connection, cont);
});
}
Expand All @@ -126,7 +192,13 @@ var initSocketServer = function(api, next){
try{ connection.end(); }catch(e){
//
}
if(api.configData.log.logRequests){api.log(" > socket connection " + connection.remoteIP + " disconnected", "white");}
// if(api.configData.log.logRequests){api.log(" > socket connection " + connection.remoteIP + " disconnected", "white");}
if(api.configData.log.logRequests){
api.logJSON({
label: "disconnect @ socket",
to: connection.remoteIP,
});
}
});
});

Expand Down
14 changes: 13 additions & 1 deletion initializers/initWebServer.js
Expand Up @@ -176,7 +176,19 @@ var initWebServer = function(api, next)
connection.res.writeHead(connection.responseHttpCode, connection.responseHeaders);
connection.res.end(stringResponse);
}
if(api.configData.log.logRequests){api.log(" > web request from " + connection.remoteIP + " | responded in : " + connection.response.serverInformation.requestDuration + "ms", "grey");}
if(api.configData.log.logRequests){
var full_url = connection.req.headers.host + connection.req.url;
if(connection.action != null && connection.action != "file"){
api.logJSON({
label: "action @ web",
to: connection.remoteIP,
action: connection.action,
request: full_url,
params: JSON.stringify(connection.params),
duration: connection.response.serverInformation.requestDuration
});
}
}
});
};

Expand Down
41 changes: 36 additions & 5 deletions initializers/initWebSockets.js
Expand Up @@ -103,7 +103,12 @@ var initWebSockets = function(api, next)
api.chatRoom.roomAddMember(api, connection);

api.stats.incrament(api, "numberOfActiveWebSocketClients");
api.log("webSocket connection "+connection.remoteIP+" | connected");
if(api.configData.log.logRequests){
api.logJSON({
label: "connect @ webSocket",
to: connection.remoteIP,
});
}

api.webSockets.connections.push(connection);

Expand All @@ -117,22 +122,40 @@ var initWebSockets = function(api, next)
connection.on('roomView', function(data){
api.chatRoom.socketRoomStatus(api, connection.room, function(roomStatus){
connection.emit("response", {context: "response", status: "OK", room: connection.room, roomStatus: roomStatus});
if(api.configData.log.logRequests){api.log(" > webSocket request from " + connection.remoteIP);}
if(api.configData.log.logRequests){
api.logJSON({
label: "roomView @ webSocket",
to: connection.remoteIP,
params: JSON.stringify(data),
}, "grey");
}
});
});
connection.on('roomChange', function(data){
api.chatRoom.roomRemoveMember(api, connection, function(){
connection.room = data.room;
api.chatRoom.roomAddMember(api, connection);
connection.emit("response", {context: "response", status: "OK", room: connection.room});
if(api.configData.log.logRequests){api.log(" > socket request from " + connection.remoteIP);}
if(api.configData.log.logRequests){
api.logJSON({
label: "roomChange @ webSocket",
to: connection.remoteIP,
params: JSON.stringify(data),
}, "grey");
}
});
});
connection.on('say', function(data){
var message = data.message;
api.chatRoom.socketRoomBroadcast(api, connection, message);
connection.emit("response", {context: "response", status: "OK"});
if(api.configData.log.logRequests){api.log(" > socket request from " + connection.remoteIP);}
if(api.configData.log.logRequests){
api.logJSON({
label: "say @ webSocket",
to: connection.remoteIP,
params: JSON.stringify(data),
}, "grey");
}
});

connection.on('action', function(data){
Expand All @@ -144,7 +167,15 @@ var initWebSockets = function(api, next)
api.processAction(api, connection, connection.messageCount, function(connection, cont){
var delta = new Date().getTime() - connection.actionStartTime;
if (connection.response.error == null){ connection.response.error = connection.error; }
if(api.configData.log.logRequests){api.log(" > webSocket request from " + connection.remoteIP + " | "+ JSON.stringify(data) + " | responded in "+delta+"ms" , "grey");}
if(api.configData.log.logRequests){
api.logJSON({
label: "action @ webSocket",
to: connection.remoteIP,
action: connection.action,
params: JSON.stringify(data),
duration: delta,
});
}
api.webSockets.respondToWebSocketClient(connection, cont);
});
});
Expand Down
9 changes: 8 additions & 1 deletion tasks/runAction.js
Expand Up @@ -10,7 +10,7 @@ task.frequency = 0;
/////////////////////////////////////////////////////////////////////
// functional
task.run = function(api, params, next){
if(params == null){prams = {};}
if(params == null){params = {};}

var connection = {
type: "task",
Expand All @@ -24,6 +24,13 @@ task.run = function(api, params, next){
api.processAction(api, connection, null, function(connection, cont){
if(connection.error){
api.log("task error: "+connection.error, "red");
}else{
if(api.configData.log.logRequests){
api.logJSON({
label: "action @ task",
params: JSON.stringify(params),
}, "grey");
}
}
next(true, connection);
})
Expand Down
11 changes: 11 additions & 0 deletions versions.md
@@ -1,5 +1,16 @@
# Action Hero API Versions

## Version 3.0.5

**Better Logging**
- seperation between file and action logging
- better shared syntax for all types of connections
- files log the path they were accessed from and duration

**General Bug Fixes**
- better logic for including base config.json when you don't provide one
- updates for travis.ci

## Version 3.0.4

**General Bug Fixes**
Expand Down

0 comments on commit 2a8e990

Please sign in to comment.