2

How can we format a value into a string to be consistent with how console.log(value) does it?

I need to append extra details to each value I am printing, without breaking the default formatting provided by console.log. It is easy for simple values, but for such types as Date and Object it is more complicated.

Is there a way I can make it consistent with console.log? In other words, how to format a value to be the same string as we get from console.log(value)?


I'm trying to implement a simple library manakin. Here's its complete source code:

'use strict';

(function () {

    var error = console.error, warn = console.warn;

    console.error = function () {
        var a = arguments;
        error.apply(this, Object.keys(a).map(function (key) {
            return "\u001b[31m" + a[key] + "\u001b[0m";
        }));
    };

    console.warn = function () {
        var a = arguments;
        warn.apply(this, Object.keys(a).map(function (key) {
            return "\u001b[33m" + a[key] + "\u001b[0m";
        }));
    };

})();

The problem I am having is that for types Date and Object the output is different:

// without manakin:
console.warn({value: 1});
//=> { value: 1 }

// with manakin:
console.warn({value: 1});
//=> [object Object]

And I want to make it consistent.

3
  • can you show an example output? Commented Jun 19, 2016 at 23:22
  • @JordanHendrix I have added the complete code ;) Commented Jun 19, 2016 at 23:33
  • Simply put; if console.log is like a printf, then OP wants a sprintf. Commented Jun 19, 2016 at 23:36

1 Answer 1

1

I believe if you modify the code in warn to be:

console.warn = function () {
    var a = arguments;
    warn.apply(this, Object.keys(a).map(function (key) {
      if (a[key] instanceof Date) {
        return "\u001b[33m" + a[key].toString() + "\u001b[0m";
      }
      return "\u001b[33m" + JSON.stringify(a[key]) + "\u001b[0m";
    }));
};

you have what you're looking for. I tested it out by just pasting the new code in the console and logging the examples you have, hope that works for you!

Sign up to request clarification or add additional context in comments.

4 Comments

I thought this approach too, but for dates, JSON.stringify returns them in RFC3339 format, while console.log prints them in what seems to be RFC1123. Maybe checking for a[key] instanceof Date and, if true, use a[key].toString() instead.
But the output from JSON.stringify is still very different from console.log formatting.
I think I will make it for Node.js only, and then use this: stackoverflow.com/questions/10729276/…. Because otherwise there seems to be no way to provide consistent formatting.
In the end I got it all working everywhere - see the update ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.