我使用以下内容来获取JavaScript调用者函数名称:
var callerFunc = arguments.callee.caller.toString();
callerFuncName = (callerFunc.substring(callerFunc.indexOf("function") + 8, callerFunc.indexOf("(")) || "anoynmous")
Run Code Online (Sandbox Code Playgroud)
有没有办法发现调用该方法的行号?
另外,有没有办法获取调用该方法的JavaScript文件的名称?还是源URL?
可能重复:
在Chrome中拦截对console.log的调用
我可以在javascript中扩展控制台对象(用于重新路由日志记录)吗?
当我的JS应用程序写入console.log时,我想捕获该日志消息,以便我可以将AJAX记录输出到服务器.我怎么做?
写入日志的代码来自外部服务,这就是我不能直接使用它的原因.
如何获得调用当前函数的函数的名称和行?我希望有一个像这样的基本调试函数(使用npmlog定义log.debug):
function debug() {
var callee, line;
/* MAGIC */
log.debug(callee + ":" + line, arguments)
}
Run Code Online (Sandbox Code Playgroud)
当从另一个函数调用时,它将是这样的:
function hello() {
debug("world!")
}
// outputs something like:
// "hello:2 'world!'"
Run Code Online (Sandbox Code Playgroud)
为清楚起见,我想要的基本上类似于Python:
import inspect
def caller():
return inspect.stack()[2][3]
// line no from getframeinfo().lineno
Run Code Online (Sandbox Code Playgroud)
是否有一个节点等效于完成此操作?
我想扩展'console.log'函数以向其输出添加其他信息 - 但我不想影响浏览器在控制台窗口中生成的脚本名称/行号信息.看看如果我创建自己的实现,我得到无用的跟踪信息,我是否应该找到代码区域...(它们都链接到日志实现,而不是导致日志消息的实际脚本)

基本上,我的应用程序是一个非常可插拔的基础架构,任何日志输出都可能出现在任意数量的帧中.因此,我希望每条日志消息都在日志消息的开头包含一个特殊的唯一标识符.
我试过用自己的方法替换console.log方法,但是chrome抱怨
Uncaught TypeError: Illegal invocation
这就是我覆盖它的方式
var orig = console.log;
console.log = function( message )
{
orig( (window == top ? '[root]' : '[' + window.name + ']') + ': ' + message );
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
[编辑]注意:修复"非法调用"问题后,覆盖文件中的文件名/编号仍然被"污染"...
[编辑]看起来一般的答案是 - 不 - 尽管有一些令人困惑的鹅追逐,但在当前版本的浏览器中无法实现所需的功能.
如何编写控制台日志包装器:
由于登录Java Script是如此不一致,因此必须有一些解决方案.自己实现它有点乏味,但似乎没有好的库.
我目前发现这个记录器提供了所有功能,但它确实搞乱了行号.http://benalman.com/projects/javascript-debug-console-log/
有没有办法做这样的事情:
console.log("hello world", '#FF0000')
Run Code Online (Sandbox Code Playgroud)
在Chrome/Safari或Firefox中?
我正在尝试在javascript中为未捕获的异常和浏览器警告编写处理程序.应将所有错误和警告发送到服务器以供日后查看.
可以捕获处理的异常并轻松记录
console.error("Error: ...");
Run Code Online (Sandbox Code Playgroud)
要么
console.warn("Warning: ...");
Run Code Online (Sandbox Code Playgroud)
因此,如果从javascript代码调用它们就不会有问题,甚至更多,未处理的异常可以通过这种代码安静来捕获:
window.onerror = function(){
// add to errors Stack trace etc.
});
}
Run Code Online (Sandbox Code Playgroud)
因此异常被覆盖,但我一直坚持浏览器发送到控制台的警告.例如安全性或html验证警告.以下示例来自Google Chrome控制台
https://domainname.com/上的页面从http://domainname.com/javascripts/codex/MANIFEST.js运行了不安全的内容 .
如果有像window.onerror这样的事件但是警告会很棒.有什么想法吗?
function log( msgOrObj ){
if(dev_mode){
console.log({
'message': msgOrObj,
'caller': arguments.callee.caller.toString()
});
}
}
Run Code Online (Sandbox Code Playgroud)
所以,我试图编写一个简单的自定义控制台日志功能(如上所述).但是我很难找到调用者来自哪个文件和行.我能看到的最多就是调用它的函数.
有没有人做过类似的事情?或者这甚至可能吗?
第70行的somescript.js中使用的示例:
log('some very important message!')
Run Code Online (Sandbox Code Playgroud) 在Python中工作时,我总是有这个简单的实用程序函数,它返回调用函数的文件名和行号:
from inspect import getframeinfo, stack
def d():
""" d stands for Debug. It returns the file name and line number from where this function is called."""
caller = getframeinfo(stack()[1][0])
return "%s:%d -" % (caller.filename, caller.lineno)
Run Code Online (Sandbox Code Playgroud)
所以在我的代码中我只是简单地放了几个这样的调试行来看看在发生错误之前我们得到了多少:
print d()
# Some buggy code here
print d()
# More buggy code here
print d(), 'here is the result of some var: ', someVar
Run Code Online (Sandbox Code Playgroud)
这对我来说非常有用,因为它确实有助于快速调试.
我现在正在寻找节点后端脚本中的等价物.我在寻找,但我找不到任何有用的东西(也许我在寻找错误的词?).
有谁知道如何创建一个Javascript/nodejs函数,它输出调用函数的文件名和行号?欢迎所有提示!
我正在寻找一种简单的方法来制作日志功能.
我正在调用一个函数logSuc("return from Prom"),该函数在第30行.
因此代码将始终指向该函数的第30行.在控制台中:
所以说有这个代码:
const logSuc = (msg) => {
console.log(`%c ${msg}`, 'background: green; color: white; display: block;');
};
Run Code Online (Sandbox Code Playgroud)
另一种选择可能是:
const log = console.log;
function red(msg) {
return `%c ${msg}`, 'background: red; color: white; display: block;';
}
log(red('its red');
Run Code Online (Sandbox Code Playgroud)
但现在我有两个功能,我想保持简短
所以问题是我logSuc("") 总是指向第30行.
但我希望它指向我称之为logSuc的行("有效").