1

I'm trying to implement my own console.log-like thing, and it's very handy to be able to do something like : console.log('something',obj) and have it print both.

I'd like to recreate this functionality, but first I must understand it.

So how does console.log do this?

I have something like this:

save : function(level, userId, message){

        //handles objects
        for(var i=2;i<arguments.length;i++){
            switch(typeof arguments[i]){
                case 'object':
                    message += ' ' + JSON.stringify(arguments[i]);
                break;
                case 'string':
                    message += arguments[i];
                break;
                case 'number':
                    message += arguments[i].toString();
                break;
            }
        }

I'm looking for all the other arguments after message and then evaluating what they are converting them, and then appending them to message.

The goal is to be able to do
logger.save('info', ,'Started app',config.env, config.port, config.etc)
and have it handle all those objects intelligently.

  • 1
    what's the problem? you know that something can be a string, an object or an array, then you just render it as a "tree" and colorize the keys and values. for strings you do nothing. that's it – vsync Sep 21 '15 at 21:52
  • 3
    How does console.log do *what*? – aquinas Sep 21 '15 at 21:52
  • what does it matter, it's written probably in C++, and you want to do it in javascript then you work with what you have, and just do it, it's not hard at all. it seems like you should be asking more basic questions than the one you're asking. just implement some "tree" menu if it's an object or array, and colorize things. mix online scripts which does coloring and tree-rendering. don't forget to add a timestamp ;-) – vsync Sep 21 '15 at 21:53
  • @aquinas handle printing strings and objects, in combination –  Sep 21 '15 at 21:58
  • `console.log` is implementation dependent and is not even a part of ECMAScript's spec. It's not clear exactly what you're looking for. – Amit Sep 21 '15 at 22:02
  • 1
    Why not just call JSON.stringify(arguments) and call it a day? – aquinas Sep 21 '15 at 22:03
  • @aquinas because I didn't know that was an option until you suggested it :) –  Sep 21 '15 at 22:06
  • @Houseman - well, next time you should ask "to to print an object as a string", which seems like it's what you're after, rather than going all the way to your final goal, which seems irrelevant to what you're truly after, which is printing things on the screen. the purpose of those prints is irrelevant to the discussion. – vsync Sep 21 '15 at 22:09
  • dup - http://stackoverflow.com/q/957537/104380 – vsync Sep 21 '15 at 22:11
  • @vsync printing an object as a string is only one part of the solution. Handling a variable amount of parameters is the other –  Sep 21 '15 at 22:14
  • well there you go, ask that question. ask something as specifically as possible. asking how does `console.log` works is extremely vague wouldn't you agree? – vsync Sep 21 '15 at 22:26
  • @vsync Yes I would agree, but I don't consider that to be what I asked. –  Sep 21 '15 at 22:59

2 Answers2

1

I'm not sure of the implementation of console.log, but you want to look at the arguments object:

function myConsoleLog() {
    for (var arg in arguments) {
        console.log(arguments[arg]);
    }
}

myConsoleLog(1, 2, 3);
myConsoleLog({}, "hello");
// etc.
Adrian Lynch
  • 8,237
  • 2
  • 32
  • 40
1

you looking for arguments (this is like array).

function test () {
    var log = '';
    for (var i = 0; i < arguments.length; i++) {
        log += arguments[i] + ',';
    }
    console.log(log.substring(0, log.length - 1));
}

test(1); // 1
test(1,2,3,4) //1,2,3,4
Leibale Eidelman
  • 2,772
  • 1
  • 17
  • 28