扩展console.log而不影响日志行 [英] Extending console.log without affecting log line

查看:109
本文介绍了扩展console.log而不影响日志行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展'console.log'函数以在其输出中添加附加信息 - 但我不想在控制台窗口中影响浏览器生成的脚本名称/行号信息。看看我是如何创建我自己的实现,我得到无用的跟踪信息,我是否应该找到该代码区域......(它们都链接到日志实现,而不是引起日志消息的实际脚本)





基本上,我的应用程序是一个非常可插拔的基础架构,任何日志输出都可能发生在任意数量的帧中。
因此,我希望每条日志消息都在日志​​消息的开始处包含一个特殊的唯一标识符。



我尝试将console.log方法与我自己的,但铬的抱怨
Uncaught TypeError:非法调用



这是我如何覆盖它

  var orig = console.log; 
console.log = function(message)
{
orig((window == top?'[root]':'['+ window.name +']')+': '+消息);

$ / code>

任何想法?


注意:修复'非法调用'问题后,似乎文件名/ linenumber仍然被覆盖'污染'...




它看起来像一般的答案是 - 没有 - 尽管一些令人困惑的鹅追逐,目前的版本的浏览器无法实现所需的功能。

解决方案

是的,可以添加信息而不会搞乱日志调用的起始行号。这里的其他一些答案接近了,但诀窍是让您的自定义日志记录方法返回修改的记录器。下面是一个简单的例子,只是使用上下文变体进行了适度测试。

  log = function(){
var context =我的描述性记录器前缀:;
return Function.prototype.bind.call(console.log,console,context);
}();

这可以用于:

  log(A log message ...); 

这是一个jsfiddle: http://jsfiddle.net/qprro98v/



人们可以轻松获得创意并传递上下文变量,并移除自动执行的parens来自函数定义。即日志(DEBUG:)(一个调试信息),日志(信息:)(这是一些信息),等等。只有真正导入关于函数的部分(关于行号)是它返回记录器。


I would like to extend the 'console.log' function to add additional information to its output - but I dont want to affect the script name/line number information generated by the browser in the console window. See how if I create my own implementation, I get useless trace information, should I want to locate that region of code... (they all link to the log implementation, not the actual script that caused the log message)

Basically, my application is a very pluggable infrastructure, were any log output may occur within any number of frames. As such, I want every log message to include a special unique identifier at the beginning of the log message.

I have tried replacing the console.log method with my own, but chrome complains with Uncaught TypeError: Illegal invocation

this is how I override it

var orig = console.log;
console.log = function( message )
{
    orig( (window == top ? '[root]' : '[' + window.name + ']') + ': ' + message );
}

Any ideas?

[EDIT] Note: After fixing the 'illegal invocation' problem, it seems the filename/linenumber is still 'polluted' by the override...

[EDIT] It looks like the general answer is - NO - despite some confusing goose chases, the desired functionality is NOT achievable in the current versions of browsers.

解决方案

Yes, it is possible to add information without messing up the originating line numbers of the log invocation. Some of the other answers here came close, but the trick is to have your custom logging method return the modified logger. Below is a simple example that was only moderately tested that uses the context variant.

log = function() {
    var context = "My Descriptive Logger Prefix:";
    return Function.prototype.bind.call(console.log, console, context);
}();

This can be used with:

log("A log message..."); 

Here is a jsfiddle: http://jsfiddle.net/qprro98v/

One could get easily get creative and pass the context variable in, and remove the auto-executing parens from the function definition. i.e. log("DEBUG:")("A debug message"), log("INFO:")("Here is some info"), etc.

The only really import part about the function (in regards to line numbers) is that it returns the logger.

这篇关于扩展console.log而不影响日志行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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