Permalink
Browse files

Add support for format strings.

  • Loading branch information...
1 parent eee5928 commit 4dd07e3b8fbf74bbc8f4d0af23237c06bea4ea19 @Kami committed May 1, 2011
Showing with 16 additions and 2 deletions.
  1. +16 −2 lib/logmagic.js
View
@@ -71,6 +71,20 @@ function buildLogMethod(level, callback) {
}
}
+function applyFormatting(message, obj) {
+ function replaceFunction(str, p1) {
+ if (obj.hasOwnProperty(p1)) {
+ return obj[p1];
+ }
+
+ return p1;
+ }
+
+ var regex = new RegExp(/\$\{(.*?)\}/g);
+ message = message.replace(regex, replaceFunction);
+ return message;
+}
+
function nullLogger() {
/* Intentionally blank. */
}
@@ -170,8 +184,8 @@ exports.route = function(match, loglevel, sinkname) {
/* This is just here for initial dev work, REMOVE ME */
exports.registerSink("console", function(level, message, obj) {
if (obj) {
- /* TODO: improve */
- console.log(message + " " + JSON.stringify(obj));
+ message = applyFormatting(message, obj);
+ console.log(message);
}
else {
console.log(message);

0 comments on commit 4dd07e3

Please sign in to comment.