How to get console.log line numbers shown in Nodejs?

后端 未结 5 603
小蘑菇
小蘑菇 2020-12-02 20:54

Got an old application, that prints out quite a lot of messages using console.log, but I just can not find in which files and lines console.log is

5条回答
  •  不知归路
    2020-12-02 21:26

    For a temporary hack to find the log statements that you want to get rid of, it's not too difficult to override console.log yourself.

    var log = console.log;
    console.log = function() {
        log.apply(console, arguments);
        // Print the stack trace
        console.trace();
    };
    
    
    // Somewhere else...
    function foo(){
        console.log('Foobar');
    }
    foo();

    That will print something like

    Foobar
    Trace
    at Console.console.log (index.js:4:13)
    at foo (index.js:10:13)
    at Object. (index.js:12:1)
    ...
    

    A lot of noise in there but the second line in the call stack, at foo (index.js:10:13), should point you to the right place.

提交回复
热议问题