如何获取Nodejs中显示的console.log行号? [英] How to get console.log line numbers shown in Nodejs?

查看:117
本文介绍了如何获取Nodejs中显示的console.log行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个旧的应用程序,它使用 console.log 打印出相当多的消息,但我找不到 console.log 在哪些文件和行中调用.

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 called.

有没有办法连接到应用程序并显示文件名和行号?

Is there a way to hook into the app and show file name and line numbers?

推荐答案

对于临时 hack 以找到您想要摆脱的日志语句,覆盖 console.log 并不难自己.

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();

这将打印类似

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

那里有很多噪音,但调用堆栈中的第二行 at foo (index.js:10:13) 应该将您指向正确的位置.

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.

这篇关于如何获取Nodejs中显示的console.log行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆