Javascript提供哪些调试日志工具?

时间:2021-10-14 17:33:44

I'd like to create a "universal" debug logging function that inspects the JS namespace for well-known logging libraries.

我想创建一个“通用”调试日志记录功能,检查着名的日志记录库的JS命名空间。

For example, currently it supports Firebug's console.log:

例如,目前它支持Firebug的console.log:

var console = window['console'];
if (console && console.log) {
  console.log(message);
}

Obviously, this only works in Firefox if Firebug is installed/enabled (it'll also work on other browsers with Firebug Lite). Basically, I will be providing a JS library which I don't know what environment it will be pulled into, and I'd like to be able to figure out if there is a way to report debug output to the user.

显然,这只适用于Firefox,如果安装/启用了Firebug(它也适用于其他使用Firebug Lite的浏览器)。基本上,我将提供一个JS库,我不知道它将被引入什么环境,我希望能够弄清楚是否有办法向用户报告调试输出。

So, perhaps jQuery provides something - I'd check that jQuery is present and use it. Or maybe there are well-known IE plugins that work that I can sniff for. But it has to be a fairly well-established and used mechanism. I can't check for every obscure log function that people create.

所以,也许jQuery提供了一些东西 - 我会检查jQuery是否存在并使用它。或者也许有着名的IE插件可以工作,我可以嗅探。但它必须是一个相当完善和使用的机制。我无法检查人们创建的每个模糊日志功能。

Please, only one library/technology per answer, so they can get vote-ranked. Also, using alert() is a good short-term solution but breaks down if you want robust debug logging or if blocking the execution is a problem.

请每个答案只有一个图书馆/技术,所以他们可以获得投票排名。此外,使用alert()是一个很好的短期解决方案,但如果您需要强大的调试日志记录或阻止执行是一个问题,则会出现故障。

8 个解决方案

#1


7  

I personally use Firebug/Firebug Lite and on IE let Visual Studio do the debugging. None of these do any good when a visitor is using some insane browser though. You really need to get your client side javascript to log its errors to your server. Take a look at the power point presentation I've linked to below. It has some pretty neat ideas on how to get your javascript to log stuff on your server.

我个人使用Firebug / Firebug Lite,在IE上让Visual Studio进行调试。当访问者使用一些疯狂的浏览器时,这些都没有任何好处。您真的需要让客户端javascript将其错误记录到您的服务器。看一下我在下面链接的功率点演示。它有一些非常巧妙的想法,如何让您的JavaScript在您的服务器上记录东西。

Basically, you hook window.onerror and your try {} catch(){} blocks with a function that makes a request back to your server with useful debug info.

基本上,您使用一个函数挂钩window.onerror和try {} catch(){}块,该函数使用有用的调试信息向您的服务器发出请求。

I've just implemented such a process on my own web application. I've got every catch(){} block calling a function that sends a JSON encoded message back to the server, which in turn uses my existing logging infrastructure (in my case log4perl). The presentation I'm linking to also suggests loading an image in your javascript in including the errors as part of the GET request. The only problem is if you want to include stack traces (which IE doesn't generate for you at all), the request will be too large.

我刚刚在自己的Web应用程序上实现了这样一个过程。我有每个catch(){}块调用一个函数将一个JSON编码的消息发送回服务器,然后服务器使用我现有的日志记录基础结构(在我的例子中是log4perl)。我正在链接的演示文稿还建议在javascript中加载图像,将错误包含在GET请求中。唯一的问题是如果你想要包含堆栈跟踪(IE根本不为你生成),请求将会太大。

Tracking ClientSide Errors, by Eric Pascarello

由Eric Pascarello跟踪ClientSide错误

PS: I wanted to add that I dont think it is a good idea to use any kind of library like jQuery for "hardcore" logging because maybe the reason for the error you are logging is jQuery or Firebug Lite! Maybe the error is that the browser (cough IE6) did some crazy loading order and is throwing some kind of Null Reference error because it was too stupid to load the library correctly.

PS:我想补充一点,我不认为使用任何类型的库(如jQuery)进行“硬核”日志记录是一个好主意,因为您记录的错误的原因可能是jQuery或Firebug Lite!也许错误是浏览器(咳嗽IE6)做了一些疯狂的加载顺序,并抛出某种Null Reference错误,因为它太愚蠢,无法正确加载库。

In my instance, I made sure all my javascript log code is in the <head> and not pulled in as a .js file. This way, I can be reasonably sure that no matter what kinds of curve balls the browser throws, odds are good I am able to log it.

在我的实例中,我确保我的所有javascript日志代码都在中,而不是作为.js文件引入。这样,我可以合理地确定无论浏览器抛出什么样的曲线球,赔率都很好我能够记录它。

#2


7  

You could try log4javascript.

你可以试试log4javascript。

Disclosure: I wrote it.

披露:我写了。

#3


4  

Firebug lite is a cross browser, lite version of Firefbug that'll at least give you console.log capabilities on most browsers.

Firebug lite是一个跨浏览器,精简版的Firefbug,它至少可以为大多数浏览器提供console.log功能。

#4


3  

MochiKit has the following functions (included here with full namespace resolution):

MochiKit具有以下功能(此处包含完整的命名空间分辨率):

MochiKit.Logging.logDebug() // prefaces value with "DEBUG: "
MochiKit.Logging.log() // prefaces value with "INFO: "
MochiKit.Logging.logError() // prefaces value with "ERROR: "
MochiKit.Logging.logFatal() // prefaces value with "FATAL: "
MochiKit.Logging.logWarning() // prefaces value with "WARNING: "

There is a lot more to the MochiKit.Logging namespace than this, but these are the basics.

MochiKit.Logging命名空间还有很多,但这些是基础知识。

#5


1  

If you are already using jQuery, I can heartily recommend the jQuery Debug plugin (a.k.a., jquery.debug.js). See http://trainofthoughts.org/blog/2007/03/16/jquery-plugin-debug/.

如果您已经在使用jQuery,我可以衷心地推荐jQuery Debug插件(a.k.a.,jquery.debug.js)。见http://trainofthoughts.org/blog/2007/03/16/jquery-plugin-debug/。

This plugin allows you to switch debug logging off or on via a global switch. Logging looks like this:

此插件允许您通过全局开关关闭或打开调试日志记录。记录看起来像这样:

$.log('My value is: ' + val);

Output is sent to console.log under Firefox and is written to a div block inserted at the bottom of the page on other browsers.

输出发送到Firefox下的console.log,并写入在其他浏览器页面底部插入的div块。

#6


0  

What about Firebug Lite (for those non-Firefox browsers)? I haven't used it much except when debugging Dojo code in IE. But it tries as best it can to put a Firebug console in IE, Safari, and Opera.

那么Firebug Lite(非Firefox浏览器)呢?除了在IE中调试Dojo代码时,我没有太多使用它。但它尽可能地将一个Firebug控制台放在IE,Safari和Opera中。

Of course there is always the ever reliable 'alert (err_msg);' :D

当然总有可靠的'警报(err_msg);' :d

#7


0  

There is JQuery Logging, which looks promising.

有JQuery Logging,看起来很有前途。

#8


-1  

Myself, I am a firm believer in the following:

我自己,我坚信以下几点:

alert('Some message/variables');

#1


7  

I personally use Firebug/Firebug Lite and on IE let Visual Studio do the debugging. None of these do any good when a visitor is using some insane browser though. You really need to get your client side javascript to log its errors to your server. Take a look at the power point presentation I've linked to below. It has some pretty neat ideas on how to get your javascript to log stuff on your server.

我个人使用Firebug / Firebug Lite,在IE上让Visual Studio进行调试。当访问者使用一些疯狂的浏览器时,这些都没有任何好处。您真的需要让客户端javascript将其错误记录到您的服务器。看一下我在下面链接的功率点演示。它有一些非常巧妙的想法,如何让您的JavaScript在您的服务器上记录东西。

Basically, you hook window.onerror and your try {} catch(){} blocks with a function that makes a request back to your server with useful debug info.

基本上,您使用一个函数挂钩window.onerror和try {} catch(){}块,该函数使用有用的调试信息向您的服务器发出请求。

I've just implemented such a process on my own web application. I've got every catch(){} block calling a function that sends a JSON encoded message back to the server, which in turn uses my existing logging infrastructure (in my case log4perl). The presentation I'm linking to also suggests loading an image in your javascript in including the errors as part of the GET request. The only problem is if you want to include stack traces (which IE doesn't generate for you at all), the request will be too large.

我刚刚在自己的Web应用程序上实现了这样一个过程。我有每个catch(){}块调用一个函数将一个JSON编码的消息发送回服务器,然后服务器使用我现有的日志记录基础结构(在我的例子中是log4perl)。我正在链接的演示文稿还建议在javascript中加载图像,将错误包含在GET请求中。唯一的问题是如果你想要包含堆栈跟踪(IE根本不为你生成),请求将会太大。

Tracking ClientSide Errors, by Eric Pascarello

由Eric Pascarello跟踪ClientSide错误

PS: I wanted to add that I dont think it is a good idea to use any kind of library like jQuery for "hardcore" logging because maybe the reason for the error you are logging is jQuery or Firebug Lite! Maybe the error is that the browser (cough IE6) did some crazy loading order and is throwing some kind of Null Reference error because it was too stupid to load the library correctly.

PS:我想补充一点,我不认为使用任何类型的库(如jQuery)进行“硬核”日志记录是一个好主意,因为您记录的错误的原因可能是jQuery或Firebug Lite!也许错误是浏览器(咳嗽IE6)做了一些疯狂的加载顺序,并抛出某种Null Reference错误,因为它太愚蠢,无法正确加载库。

In my instance, I made sure all my javascript log code is in the <head> and not pulled in as a .js file. This way, I can be reasonably sure that no matter what kinds of curve balls the browser throws, odds are good I am able to log it.

在我的实例中,我确保我的所有javascript日志代码都在中,而不是作为.js文件引入。这样,我可以合理地确定无论浏览器抛出什么样的曲线球,赔率都很好我能够记录它。

#2


7  

You could try log4javascript.

你可以试试log4javascript。

Disclosure: I wrote it.

披露:我写了。

#3


4  

Firebug lite is a cross browser, lite version of Firefbug that'll at least give you console.log capabilities on most browsers.

Firebug lite是一个跨浏览器,精简版的Firefbug,它至少可以为大多数浏览器提供console.log功能。

#4


3  

MochiKit has the following functions (included here with full namespace resolution):

MochiKit具有以下功能(此处包含完整的命名空间分辨率):

MochiKit.Logging.logDebug() // prefaces value with "DEBUG: "
MochiKit.Logging.log() // prefaces value with "INFO: "
MochiKit.Logging.logError() // prefaces value with "ERROR: "
MochiKit.Logging.logFatal() // prefaces value with "FATAL: "
MochiKit.Logging.logWarning() // prefaces value with "WARNING: "

There is a lot more to the MochiKit.Logging namespace than this, but these are the basics.

MochiKit.Logging命名空间还有很多,但这些是基础知识。

#5


1  

If you are already using jQuery, I can heartily recommend the jQuery Debug plugin (a.k.a., jquery.debug.js). See http://trainofthoughts.org/blog/2007/03/16/jquery-plugin-debug/.

如果您已经在使用jQuery,我可以衷心地推荐jQuery Debug插件(a.k.a.,jquery.debug.js)。见http://trainofthoughts.org/blog/2007/03/16/jquery-plugin-debug/。

This plugin allows you to switch debug logging off or on via a global switch. Logging looks like this:

此插件允许您通过全局开关关闭或打开调试日志记录。记录看起来像这样:

$.log('My value is: ' + val);

Output is sent to console.log under Firefox and is written to a div block inserted at the bottom of the page on other browsers.

输出发送到Firefox下的console.log,并写入在其他浏览器页面底部插入的div块。

#6


0  

What about Firebug Lite (for those non-Firefox browsers)? I haven't used it much except when debugging Dojo code in IE. But it tries as best it can to put a Firebug console in IE, Safari, and Opera.

那么Firebug Lite(非Firefox浏览器)呢?除了在IE中调试Dojo代码时,我没有太多使用它。但它尽可能地将一个Firebug控制台放在IE,Safari和Opera中。

Of course there is always the ever reliable 'alert (err_msg);' :D

当然总有可靠的'警报(err_msg);' :d

#7


0  

There is JQuery Logging, which looks promising.

有JQuery Logging,看起来很有前途。

#8


-1  

Myself, I am a firm believer in the following:

我自己,我坚信以下几点:

alert('Some message/variables');