一个记录到我们后端的 JavaScript 前端日志系统?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5033987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:44:09  来源:igfitidea点击:

A JavaScript frontend logging system that logs to our backend?

javascriptjqueryloggingxmlhttprequest

提问by a paid nerd

We have an established logging system for our server-side services. Specifically, our Django project makes heavy use of the Python logging module, so call calls to logger.info(), logger.warn()and logger.error()get picked up by our centralized logging system.

我们为我们的服务器端服务建立了一个日志系统。具体来说,我们的Django项目大量使用Python记录模块,所以来电通话logger.info()logger.warn()logger.error()得到由我们的集中式日志记录系统接走。

I would like an equivalent on our frontend, and I've got a few ideas:

我想在我们的前端有一个等价的东西,我有一些想法:

  1. There would be some sort of custom logging object exposed via JavaScript that would send messages to backend via an XmlHttpRequest.

  2. I'd like to have equivalent logging levels on the client-side: debug, info, warningand error.

  3. When we're developing locally (debug mode), I'd like those logging messages to be logged to the browser/Firebug console via console.log().

  4. In production, debugmessages should be dropped completely.

  5. I recall seeing a way to capture all uncaught JavaScript exceptions, so these should be logged at the errorlevel.

  6. We're already using Google Analytics event tracking, and it'd be nice for whatever system we create to tie into that somehow.

  1. 将有某种通过 JavaScript 公开的自定义日志对象,这些对象将通过 XmlHttpRequest 向后端发送消息。

  2. 我想对客户端等效的日志记录级别:debuginfowarningerror

  3. 当我们在本地开发(调试模式)时,我希望这些日志消息通过console.log().

  4. 在生产中,debug应完全删除消息。

  5. 我记得看到过一种捕获所有未捕获的 JavaScript 异常的方法,因此应该在error级别记录这些异常。

  6. 我们已经在使用Google Analytics 事件跟踪,我们创建的任何系统都可以以某种方式与之相关联。

Is this a good idea? How would you do this? Are there existing solutions?

这是一个好主意吗?你会怎么做?是否有现有的解决方案?

(FWIW, we're using jQuery on the frontend.)

(FWIW,我们在前端使用 jQuery。)

Update:Simplified question here: https://stackoverflow.com/questions/1423267/are-there-any-logging-frameworks-for-javascript

更新:这里的简化问题:https: //stackoverflow.com/questions/1423267/are-there-any-logging-frameworks-for-javascript

回答by Tim Down

First, I wrote and maintain log4javascript, so I'm declaring my interest up front. I also use it every day in my work, so I have some experience of it as a user. Here's how I would deal with your questions, specifically relating to log4javascript:

首先,我编写并维护了log4javascript,所以我预先声明了我的兴趣。我也每天在工作中使用它,所以我作为一个用户有一些使用它的经验。以下是我将如何处理您的问题,特别是与 log4javascript 相关的问题:

  1. Use log4javascript's AjaxAppenderfor server logging;

  2. debug, info, warningand errorare all supported, as well as traceand fatal;

  3. Use a BrowserConsoleAppenderto log to FireBug or the native browser console;

  4. If you don't want to remove all debug logging calls from you production code, you can either adjust your logger's threshold (using log.setLevel(log4javascript.Level.ERROR), for example, which will suppress all log calls with priority less than ERROR). If you want to suppress all logging calls, you can drop in a stub version of log4javascriptin your production code.

  5. You'll need to write a bit of code to do this using window.onerror. Something like window.onerror = function(msg, file, line) { log.error("Error in " + file + " on line " + line + ": " + msg); }

  6. I'm not sure how you want to tie in with Google Analytics. log4javascript has no particular support for it.

  1. 使用 log4javascriptAjaxAppender进行服务器日志记录;

  2. debuginfowarning并且error都支持,以及tracefatal;

  3. 使用 aBrowserConsoleAppender登录到 FireBug 或本机浏览器控制台;

  4. 如果您不想从生产代码中删除所有调试日志记录调用,您可以调整记录器的阈值(log.setLevel(log4javascript.Level.ERROR)例如,使用 将抑制所有优先级小于 的日志调用ERROR)。如果要禁止所有日志记录调用,可以在生产代码中放入 log4javascript存根版本

  5. 您需要编写一些代码才能使用window.onerror. 就像是window.onerror = function(msg, file, line) { log.error("Error in " + file + " on line " + line + ": " + msg); }

  6. 我不确定您想如何与 Google Analytics(分析)配合使用。log4javascript 没有特别支持它。

回答by Terrance

The idea sounds good here. Just be aware of what exactly it is you are looking to log client-side and have at it.

这个想法在这里听起来不错。请注意您希望在客户端登录并拥有它的确切内容。

I would recommend using log4javascriptfor logging. The log4 api is pretty straight foward.

我建议使用log4javascript进行日志记录。log4 api 非常简单。

回答by wageoghe

Here is another questionfrom here on SO about this very issue.

这是关于这个问题的另一个问题。

Some recommendations are: log4js, log4javascript, and Blackbird.

一些建议是:log4jslog4javascriptBlackbird

FWIW, log4js was the accepted answer there. I don't have experience with any of these platforms, so I can't really recommend one over the other.

FWIW,log4js 是那里接受的答案。我没有使用任何这些平台的经验,所以我不能真正推荐一个。