Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add rewriters to logmagic, letting you pass in a generic thing like t…

…he request object, and pick out things to log on it, rather than the whole object
  • Loading branch information...
commit e7c6374edc6175ce17cb9e5230df2fccfe7f1226 1 parent a7ffa3c
@pquerna authored
Showing with 34 additions and 1 deletion.
  1. +22 −0 lib/logmagic.js
  2. +12 −1 tests/t.js
View
22 lib/logmagic.js
@@ -66,6 +66,15 @@ var log_aliases = {"WARN": "WARNING",
var known_sinks = {};
var known_loggers = [];
var known_routes = [];
+var rewriters = [];
+
+function applyRewrites(modulename, level, msg, extra) {
+ var i;
+ for (i = 0; i < rewriters.length; i++) {
+ extra = rewriters[i](modulename, level, msg, extra);
+ }
+ return extra;
+}
function buildLogMethod(modulename, level, callback) {
if (level >= exports.TRACE1) {
@@ -74,11 +83,16 @@ function buildLogMethod(modulename, level, callback) {
extra = {};
}
extra['full_message'] = new Error('Backtrace').stack;
+ extra = applyRewrites(modulename, level, msg, extra);
callback(modulename, level, msg, extra)
}
}
else {
return function (msg, extra) {
+ if (!extra) {
+ extra = {};
+ }
+ extra = applyRewrites(modulename, level, msg, extra);
callback(modulename, level, msg, extra)
}
}
@@ -177,6 +191,14 @@ exports.route = function(match, loglevel, sinkname) {
}
};
+exports.addRewriter = function(func) {
+ rewriters.push(func);
+};
+
+exports.clearRewriter = function(func) {
+ rewriters.pop(func);
+};
+
(function() {
/* Default Sinks */
View
13 tests/t.js
@@ -6,11 +6,22 @@ log.error("more stuff", {SOME_VAR: "myvalue"});
log.trace("testing trace v0");
logmagic.route("__root__", logmagic.TRACE1, "console");
-//logmagic.route("__root__", logmagic.TRACE1, "graylog2-stderr");
+logmagic.route("__root__", logmagic.TRACE1, "graylog2-stderr");
log.trace("testing trace v1", {slug: 1});
log = logmagic.local('mylib.foo.cars');
log.trace("hello world", {counter: 33, account_id: 42, txnid: "fxxxxx"});
+logmagic.addRewriter(function(modulename, level, msg, extra) {
+ if (extra.request) {
+ extra.accountId = extra.request.account.id;
+ extra.txnId = extra.request.txtId;
+ delete extra.request;
+ }
+ return extra;
+});
+
log.trace("hello baksdfnsdf", {special: 'aaa', account_id: 42, txnid: "fxxxxx", full_message: "loooong message"});
+log.dbg("hello xxxx", {request: {account: {id: 45}, txtId: "XXXXXXXXXXXXX"}});
+
//console.log(log);
Please sign in to comment.
Something went wrong with that request. Please try again.