简体   繁体   English

JavaScript console.log / trace等的策略

[英]Strategies to JavaScript console.log/trace, etc

I am writing a rather large JavaScript software. 我正在写一个相当大的JavaScript软件。 I need to trace calls, log events, debug actions, while maintain performance and portability across browsers. 我需要跟踪调用,记录事件,调试操作,同时保持跨浏览器的性能和可移植性。

Webkit and Firebug both offer console object with methods like trace(), log(), error(), warning(), etc. They are great, but what do I do when the browser is IE or Opera? Webkit和Firebug都提供了控制台对象,其中包括trace(),log(),error(),warning()等等。它们很棒,但是当浏览器是IE或Opera时我该怎么办?

Imagine having a big application, you surely want to see all initializations it is doing, events it is making, etc., thus I make it log those. 想象一下,拥有一个大型应用程序,你肯定希望看到它正在进行的所有初始化,它正在制作的事件等等,因此我将它记录下来。 However, if I just log those, the logging will not work in browsers that do not have console registered in the DOM. 但是,如果我只是记录这些日志,则日志记录将无法在没有在DOM中注册的控制台的浏览器中运行。 I could create a wrapper object: 我可以创建一个包装器对象:

MyNamespace.Console = {};
MyNamespace.Console.log = function(value) {
 if (console!==undefined) {
  console.log(value);
 }
 else {
  // What should I do to log events on other browsers?
 }
}

The above makes so no problems occur on IE / Opera, but how do I log with IE (one really needs logging with IE!). 以上使得IE / Opera上没有问题,但我如何用IE登录(一个真的需要用IE登录!)。

Also, if I plant logs everywhere in my application, does it slow me down when it is being run on a production environment? 另外,如果我在我的应用程序中随处可见日志,那么当它在生产环境中运行时,它会减慢我的速度吗? Should I have a switch DEBUG on/off and a simple check before logging that if DEBUG===true, then log? 如果DEBUG === true,我是否应该在开启/关闭开关DEBUG并进行简单检查,然后记录?

What about systems like Closure Compiler, can you make them to remove logging? 像Closure Compiler这样的系统怎么样,你可以让它们删除日志记录吗?

What if there is an error while running on production environment and logging has not happened, how do you debug/find the problem? 如果在生产环境中运行时出现错误并且没有发生日志记录,如何调试/查找问题该怎么办? And in fact, do you ever send JavaScript error logs to you (developer), to make sure your clients are not having problems? 事实上,您是否曾向您(开发人员)发送JavaScript错误日志,以确保您的客户没有问题? How does that work out? 这怎么办?

I appreciate any feedback/comments on debugging/logging with JavaScript, this is my first time writing a huge JavaScript application, and frankly, I am not sure what should I do about this... debugging and logging in JavaScript seems a bit unfinished. 我很感激有关使用JavaScript进行调试/日志记录的任何反馈/评论,这是我第一次编写一个巨大的JavaScript应用程序,坦率地说,我不知道我该怎么做...调试和登录JavaScript似乎有点未完成。

I wouldn't recommend having the application report log data back to the developer—at least not without disclosing this to users/clients first. 我不建议将应用程序报告日志数据反馈给开发人员 - 至少在没有向用户/客户首先公开的情况下。

It would be a safer practice to log errors/events/debugging data to an array initially as Anurag suggests, and then periodically send the log data to the web server where it can be stored locally and cycled. 如Anurag建议的那样,最初将错误/事件/调试数据记录到数组是一种更安全的做法,然后定期将日志数据发送到Web服务器,在那里它可以存储在本地并循环。 Then if the client is having problems , they can pull up the logs themselves for debugging and, either, send it to you manually, or explicitly tell the application to transfer the logs to you, the developer. 然后, 如果客户端遇到问题 ,他们可以自己提取日志以进行调试,或者手动将其发送给您,或者明确告诉应用程序将日志传输给开发人员。

Logging to the console is generally used during development anyway. 无论如何,通常在开发期间使用登录到控制台。 And a debug flag should be used for that so that it won't be turned on at all times. 并且应该使用调试标志,以便它不会一直打开。

You can just add all log messages to an array or object. 您只需将所有日志消息添加到数组或对象即可。 An object could look like: 对象可能如下所示:

var logMessages = {
    'log': [],
    'debug': [],
    'error': [],
    ...
};

where the array represents messages logged for that level, ordered chronologically or using some other criteria. 其中数组表示为该级别记录的消息,按时间顺序排序或使用其他一些条件。

You could simply define identical interfaces for browsers that lack a console to the ones that do have it. 您可以简单地为缺少console浏览器定义相同的接口。 For example, 例如,

if(!window.console) {
    var console = {};

    console.log = function(msg) {
        ..
    };

    console.debug = function(msg) {
        ..
    };

    ..
}

The list of messages could be synced with up a server, which I am guessing may only be necessary for errors, and possibly warnings. 消息列表可以与服务器同步,我猜这可能只是错误和警告的必要条件。 To catch all errors on the page, setup a callback on window : 要捕获页面上的所有错误,请在window上设置回调:

window.onerror = function() {
    ..
};

and inside the callback, report to server. 在回调中,向服务器报告。 This may not play very well with Opera. 这可能不适合Opera。 See javascript-global-error-handling 请参阅javascript-global-error-handling

Do a google search for accord redbird console. 做谷歌搜索协议redbird控制台。

Redbird allows remote logging directly from any standards compliant browser using pure JavaScript. Redbird允许使用纯JavaScript直接从任何符合标准的浏览器进行远程日志记录。 It does not impact your web service and it should work on Safari, Firefox, Chrome, Opera, MSIE, Smartphones, etc. 它不会影响您的Web服务,它应该适用于Safari,Firefox,Chrome,Opera,MSIE,智能手机等。

Redbird comes in handy for debugging code that works fine in one browser but does something different in another. Redbird可以很方便地调试在一个浏览器中工作正常的代码,但在另一个浏览器中执行不同的操作。 A common way to log and aggregate messages across different browsers helps in identifying differences. 跨不同浏览器记录和聚合消息的常用方法有助于识别差异。

Using a DEBUG flag as you suggested is a good idea. 如您所建议使用DEBUG标志是个好主意。 Lots of debugging messages can slow things down in a production environment. 大量调试消息可能会降低生产环境中的速度。 Another option for production environment is to replace the logging function with a null function, ie a function that does not do anything. 生产环境的另一个选择是使用null函数替换日志记录函数,即不执行任何操作的函数。 This avoids having lots of if DEBUG statements in the code. 这避免了代码中有大量的if DEBUG语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM