Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add support for format strings. #1

Closed
wants to merge 1 commit into from

2 participants

@Kami
log.error("Accepts format strings too ${SOME_VAR}", {SOME_VAR: "myvalue"});

Accepts format strings too ${SOME_VAR} -> Accepts format strings too myvalue

Should mostly work :)

@pquerna pquerna closed this in cbff993
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 1, 2011
  1. @Kami

    Add support for format strings.

    Kami authored
This page is out of date. Refresh to see the latest.
Showing with 16 additions and 2 deletions.
  1. +16 −2 lib/logmagic.js
View
18 lib/logmagic.js
@@ -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);
Something went wrong with that request. Please try again.