Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Loggly + Express.js/Connect middleware see full article: [http://wp.me/pfl8Y-7X]
var OBJECT, env, logglyConfig, logglyKey, logglyMiddleware, services;
logglyKey = "[SUPPRESS]";
env = process.env.API_ENV;
logglyConfig = {
subdomain: "fabian......",
json: true
};
services = {};
services.loggly = require('loggly').createClient(logglyConfig);
OBJECT = 'object';
logglyMiddleware = function(req, res, next) {
var end, resData, send;
if (!req._startTime) {
req._startTime = new Date();
}
send = res.send;
resData = null;
res.send = function(_status, _body) {
var body, k, v, _i, _len;
res.send = send;
if (typeof _status === OBJECT) {
body = _status;
} else {
body = _body;
}
if (body) {
if (body instanceof Array) {
resData = [];
for (_i = 0, _len = body.length; _i < _len; _i++) {
v = body[_i];
if (v === null || typeof v !== OBJECT) {
resData.push(v);
} else {
resData.push(v.toString && v.toString());
}
}
} else {
resData = {};
for (k in body) {
v = body[k];
if (v === null || typeof v !== OBJECT) {
resData[k] = v;
} else {
resData[k] = v.toString && v.toString();
}
}
}
}
if (_body) {
return res.send(_status, _body);
} else {
return res.send(_status);
}
};
end = res.end;
res.end = function(c, e) {
var ip, len, obj;
res.end = end;
res.end(c, e);
ip;
if (req.ip) {
ip = req.ip;
} else if (req.socket && req.socket.socket) {
ip = req.socket.socket.remoteAddress;
}
len = parseInt(res.getHeader('Content-Length') || '0', 10) / 1024;
obj = {
type: 'request',
at: req._startTime,
duration: (new Date()) - req._startTime,
status: res.statusCode,
ip: ip,
path: req.path || req.originalUrl || req.url,
env: env,
method: req.method,
respData: resData,
respKb: Math.round(len * 10) / 10
};
if (req.headers) {
obj.headers = req.headers;
}
if (req.body) {
obj.body = req.body;
}
if (req.query) {
obj.qs = req.query;
}
if (req.params) {
obj.params = req.params;
}
if (req.files) {
obj.files = Object.keys(req.files);
}
if (env === 'development') {
return console.log(obj);
} else {
return services.loggly.log(logglyKey, obj);
}
};
return next();
};
logglyKey = "[SUPPRESS]"
env = process.env.API_ENV # or whatever makes more sense to you
logglyConfig =
subdomain: "fabian......" # configure it right ;)
json: true
services = {}
services.loggly = require('loggly').createClient(logglyConfig);
OBJECT = 'object'
logglyMiddleware = (req,res,next) ->
unless req._startTime
req._startTime = new Date()
# asking for trouble!
send = res.send
resData = null
res.send = (_status,_body) ->
res.send = send
if typeof _status == OBJECT
body = _status
else
body = _body
if body
if body instanceof Array
resData = []
for v in body
if v == null || typeof v != OBJECT
resData.push v
else
resData.push v.toString && v.toString()
else
resData = {}
# check 1 level
for k,v of body
if v == null || typeof v != OBJECT
resData[k] = v
else
resData[k] = v.toString && v.toString()
if _body
res.send(_status,_body)
else
res.send(_status)
# /asking for trouble!
end = res.end
res.end = (c,e) ->
#console.log res
res.end = end
res.end c, e
ip
if (req.ip)
ip = req.ip
else if req.socket && req.socket.socket
ip = req.socket.socket.remoteAddress
len = parseInt(res.getHeader('Content-Length') || '0', 10)/1024
obj =
type: 'request'
at: req._startTime
duration: (new Date()) - req._startTime
status: res.statusCode
ip: ip
path: req.path || req.originalUrl || req.url
env: env
method: req.method
respData: resData
respKb: Math.round(len*10)/10
if req.headers
obj.headers = req.headers
if req.body
obj.body = req.body
if req.query
obj.qs = req.query
if req.params
obj.params = req.params
if req.files
obj.files = Object.keys req.files
if env == 'development'
console.log obj
else
services.loggly.log(logglyKey, obj)
next()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment