使用Raygun在Node.js应用程序中自动查找和记录错误

Writing good code is hard. Diagnosing code related issues is even harder. Sometimes the issue is obvious but other times it can drive you insane trying to figure out why the code isn’t functioning properly. That’s when we dive into server logs, commit history and jump on 2am Skype calls to try and recreate the issue and fix it.

编写好的代码很难。 诊断与代码相关的问题更加困难。 有时问题很明显,但有时却使您发疯,试图弄清代码为什么无法正常运行。 那时我们进入服务器日志,提交历史记录并跳入凌晨2点的Skype呼叫,尝试重新创建问题并修复它。

Raygun is a company and app that aims to simplify the diagnostics of errors and crashes in your applications.

Raygun是一家公司和应用程序,旨在简化应用程序中错误和崩溃的诊断。

Raygun detects and diagnoses errors and crashes in your application and makes them available on your Raygun dashboard. Included in these reports are full diagnostics information and stack traces to help you pinpoint the root cause of issues and fix them.

Raygun可检测并诊断应用程序中的错误和崩溃,并将其显示在Raygun仪表板上。 这些报告中包含完整的诊断信息和堆栈跟踪,以帮助您查明问题的根本原因并加以解决。

Raygun_Logo_600px_Flat_Blue

In this tutorial, we’ll walk through the process of setting up Raygun with a simple application. Raygun supports all major web and mobile languages and platforms ranging from Android to Unity 3D and can be plugged in easily into any of their many supported languages like PHP, Node, Angular, Ruby, and more.

在本教程中,我们将逐步介绍通过一个简单的应用程序设置Raygun的过程。 Raygun支持从Android到Unity 3D的所有主要的网络和移动语言和平台,并且可以轻松地插入到其许多受支持的语言(如PHP,Node,Angular,Ruby等)中。

我们的应用 (Our Application)

For our tutorial we will build a small, buggy and simple Node.js application that will showcase how you can integrate Raygun into your application. Here we will be focusing on triggering errors and handling them with Raygun so we will not be developing a UI for our app.

在我们的教程中,我们将构建一个小的,有错误的 ,简单的Node.js应用程序,该应用程序将展示如何将Raygun集成到您的应用程序中。 在这里,我们将专注于触发错误并使用Raygun处理错误,因此我们不会为我们的应用开发UI。

To get started we will first create an account on Raygun. Raygun has a free 30 day trial that anyone can sign up for and evaluate the software and then plans start at $49/month.

首先,我们将首先在Raygun上创建一个帐户 。 Raygun有30天的免费试用期,任何人都可以注册并评估该软件,然后计划每月49美元起。

Raygun入门 (Getting Started with Raygun)

The onboarding process for setting up Raygun is very simple.

设置Raygun的入门过程非常简单。

  1. Create your account

    创建您的帐户
  2. Choose a name for your first application

    为您的第一个应用程序选择一个名称
  3. Select the platform/language

    选择平台/语言
  4. Integrate

    整合

1.启动您的Raygun应用程序 (1. Start Your Raygun App)

signup-1

The key takeaway on the first step is to configure how often you would like to be alerted when Raygun receives errors. You can select to receive an update each time an error comes in, but that may be overkill, so going for something like a daily summary may be more suitable.

第一步的主要内容是配置当Raygun收到错误时希望多久收到一次警报 。 您可以选择每次出现错误时都接收更新,但这可能会过大,因此进行每日摘要之类的操作可能更合适。

For our tutorial we will enable all settings so that we are alerted any time an error is thrown in our application.

在我们的教程中,我们将启用所有设置,以便在应用程序中抛出任何错误时均会收到警报。

2.选择语言 (2. Select the Language)

signup-2

Step 2 is very straightforward, simply select the language your application is written in. The language you select will determine the instructions you receive on step 3.

步骤2非常简单,只需选择应用程序编写的语言即可。选择的语言将确定您在步骤3上收到的说明。

3.安装说明 (3. Setup Instructions)

signup-3

Once you get the final step you will be given instructions on how to integrate Raygun into your application. Since we’re working with Node for this tutorial, we will be following the directions to setup Raygun with Node - and specifically the Express framework. S

完成最后一步后,您将获得有关如何将Raygun集成到应用程序中的说明。 由于我们正在使用Node处理本教程,因此我们将按照说明用Node-特别是Express框架设置Raygun。 小号

Raygun和Node / Express (Raygun and Node/Express)

Setting up Raygun with express requires only three steps.

使用Express设置Raygun仅需三个步骤。

  • Install the Raygun dependency by running npm install raygun

    通过运行npm install raygun安装Raygun依赖项
  • Include the Raygun module in your application - in our example all of the code is stored in a single server.js so we can include the Raygun module by just including var raygun = require("raygun");

    在您的应用程序中包含Raygun模块-在我们的示例中,所有代码都存储在单个server.js中,因此我们可以通过仅包含var raygun = require("raygun");来包含Raygun模块var raygun = require("raygun");
  • Finally we create an instance and initiate Raygun as well as store it in a variable so that we can use it throughout the application.

    最后,我们创建一个实例并启动Raygun并将其存储在变量中,以便我们可以在整个应用程序中使用它。

Let’s take a look at our code so far:

到目前为止,让我们看一下我们的代码:

// grab the modules we need
var express = require("express"); 
var raygun = require("raygun");

// initiate a raygun client
var raygunClient = new raygun.Client().init({apiKey: "API-KEY-HERE"});

// get an instance of express for our application
var app = express();

// we will define routes here

// IMPORTANT: per Express and Raygun documentation, for Raygun to properly capture application based errors it's middleware will need to be called before the app.listen function
app.use(raygunClient.expressHandler); 

// start our app on port 8080
app.listen("8080");

If you’ve used Node.js in the past, you should feel right at home. If you are new to NodeJs, what we did here was include the express and Raygun modules in our application on lines 1 and 2, then initiate a Raygun client on line 4 with the minimum required settings as well as initiated the express application on line 8.

如果您过去曾经使用过Node.js,应该会感到宾至如归。 如果您不熟悉NodeJ,那么我们要做的就是在第1行和第2行的应用程序中包含express和Raygun模块,然后在第4行以最低要求的设置启动Raygun客户端,并在第8行启动Express应用程序。

On line 9 we are declaring a middleware that will be used with our app called raygunClient.expressHandler.

在第9行,我们声明了一种中间件,该中间件将与名为raygunClient.expressHandler的应用程序一起使用。

Finally, we made our express app listen on port 8080. Since we haven’t setup any routes we won’t see anything if we navigate to localhost:8080 just yet.

最后,我们使Express应用程序侦听端口8080。由于尚未设置任何路由,因此,如果我们现在导航到localhost:8080,将看不到任何内容。

将简单错误记录到Raygun (Logging a Simple Error to Raygun)

Now that we have our simple skeleton application built we are ready to start capturing errors with Raygun. The following code samples will be inserted between the var app = express() and app.use(raygunClient.expressHandler) statements.

现在,我们已经构建了简单的框架应用程序,我们准备开始使用Raygun捕获错误。 以下代码示例将插入var app = express()app.use(raygunClient.expressHandler)语句之间。

We will setup a couple of routes that will be configured to throw different types of errors and then we will investigate these errors in detail in the Raygun dashboard.

我们将设置几个路由,这些路由将配置为引发不同类型的错误,然后在Raygun仪表板中详细研究这些错误。

Let's start by throwing the simplest error possible. When the user navigates to the /simple-error route, this error will be logged.

让我们从抛出最简单的错误开始。 当用户导航到/ simple-error路由时,将记录此错误。

We will not actually throw the error to stop the application flow, but will instead display a message of what type of error was thrown.

我们实际上不会抛出错误来停止应用程序流,而是将显示一条消息,指出发生了哪种类型的错误。

// create the simple-error route
app.get('/simple-error', function(req, res){
    // create a new error object
    var err = new Error('Simple Error');

    // send that error off to raygun
    raygunClient.send(err);

    // send a message back to the browser
    res.send('Simple Error Thrown');
});

With this code in place, let's run our app and navigate to the /simple-error route. Once you see the message "Simple Error Thrown" in your browser. You can also navigate to your Raygun dashboard to confirm that error we just created has successfully fired.

使用此代码后,让我们运行我们的应用程序并导航到/ simple-error路由。 一旦在浏览器中看到消息“抛出简单错误” 。 您也可以导航到Raygun仪表板以确认我们刚刚创建的错误已成功触发。

If you had set your email settings to receive alerts any time an error is triggered you should have also received an email telling you that your application has triggered an error.

如果您已将电子邮件设置设置为在触发错误时随时接收警报,则您还应该收到一封电子邮件,告知您您的应用程序已触发错误。

The email you receive will also have links to take quick actions such as mark the error resolved, ignore the error or view additional details on your Raygun dashboard.

您收到的电子邮件中还将包含用于采取快速措施的链接,例如在Raygun仪表板上标记错误已解决,忽略错误或查看其他详细信息。

Before we create additional errors and go deeper into the code - I want to familiarize you with the Raygun dashboard. The next section will give you an overview of the major sections of the Raygun dashboard webapp and then we'll get back to triggering and handling errors.

在我们创建其他错误并深入研究代码之前,我想让您熟悉Raygun仪表板。 下一部分将为您概述Raygun仪表板Web应用程序的主要部分,然后我们将回到触发和处理错误。

了解Raygun仪表板 (Understanding the Raygun Dashboard)

Now that we have our simple application set up and sending errors, let’s take a closer look at the Raygun Dashboard and understand how to best utilize it.

现在我们已经设置了简单的应用程序并发送了错误,让我们仔细看一下Raygun仪表板并了解如何最好地利用它。

仪表板视图 (Dashboard View)

railgun-dashboard

The dashboard view gives us an overall summary of what is going on in our application. The two key elements displayed here are a graph displaying the number of errors and when they occurred and a summary of errors below it. We can click into each individual error to find see additional details.

仪表板视图为我们提供了应用程序中正在发生的事情的总体摘要。 此处显示的两个关键元素是一个图形,该图形显示错误的数量和发生的时间,以及下面的错误摘要。 我们可以单击每个单独的错误以查找其他详细信息。

Errors in Raygun are are grouped into four categories: active, resolved, ignored and permanently ignored.

Raygun中的错误分为四类: 活动 ,已解决 ,已忽略永久忽略

  • Active - errors that have been reported into Raygun but have not had an action taken yet. These errors are pending interaction and further classification

    活动 -已向Raygun报告但尚未采取措施的错误。 这些错误有待互动和进一步分类
  • Resolved - if an error is marked resolved it will go into this category. If the error is raised again it will move from resolved to active. Additionally, there is a Resolved in Version category that will suppress an error for that version of the software

    已解决 -如果将错误标记为已解决,它将归入此类别。 如果错误再次出现,它将从已解决变为有效。 此外,存在“已解决的版本”类别,该类别将消除该软件版本的错误
  • Ignored - errors placed in this category are non-critical - thus being ignored. You may still want to know that these errors are occurring so you will be notified but these types of errors should not cause major issues with your application

    被忽略 -放置在此类别中的错误不严重-因此被忽略。 您可能仍想知道发生了这些错误,因此将通知您,但这些类型的错误不应导致应用程序出现重大问题
  • Permanently Ignored - you will not be directly notified of errors that fall in this category and they will not be placed in the “active” category if they reoccur.

    永久忽略 -如果发生此类错误,则不会直接通知您,并且如果发生此类错误,也不会将它们置于“活动”类别中。

用户视图 (Users View)

railgun-user

If you are using the user's functionality provided by Raygun - you will be able to see a list of users and what issues they are experiencing in this panel. You can also be able to contact these users via email or social media.

如果您使用的是Raygun提供的用户功能-您将可以在此面板中查看用户列表以及他们遇到的问题。 您也可以通过电子邮件或社交媒体与这些用户联系。

部署视图 (Deployments View)

railgun-deployment

If you have Raygun implemented for deployment tracking you will be able to see outcomes of deployments in this view. We will not be covering Raygun deployment tracking functionality in this tutorial.

如果已将Raygun实现用于部署跟踪,则可以在此视图中查看部署的结果。 在本教程中,我们将不介绍Raygun部署跟踪功能。

应用程序设置视图 (Application Settings View)

railgun-app-settings

In the application settings you can view your API Key, change app name, configure different integrations as well as delete your application if you are no longer using it.

在应用程序设置中,您可以查看API密钥,更改应用程序名称,配置其他集成以及删除不再使用的应用程序。

其他观点 (Other Views)

railgun-team

The remaining tabs allow you to provide feedback for the Raygun team and also manage your team. Raygun gives you the ability to manage individual users as well as create teams to best coordinate the bug squashing efforts for your organization.

其余选项卡使您可以为Raygun团队提供反馈并管理您的团队。 Raygun使您能够管理单个用户以及创建团队以最佳地协调组织的bug消除工作。

检查Raygun仪表板中的错误 (Examining Errors in the Raygun Dashboard)

railgun-error

Earlier in this tutorial we created and triggered a simple error via Raygun. Let's go into the Raygun dashboard and see what type of information Raygun has gathered for us. Clicking on the error takes us to a detailed view of the error.

在本教程的前面,我们通过Raygun创建并触发了一个简单的错误。 让我们进入Raygun仪表板,看看Raygun为我们收集了哪些信息。 单击错误将使我们进入该错误的详细视图。

Here we can see a summary of the error that includes vital statistics such as

在这里,我们可以看到错误的摘要,其中包括重要的统计信息,例如

  • When the error occurred

    错误发生时
  • Number of occurrences

    出现次数
  • The message we defined (in our case 'Simple Error')

    我们定义的消息(在我们的示例中为“简单错误”)
  • The stacktrace of the error

    错误的堆栈跟踪

用户机器信息 (User Machine Information)

If we navigate to the Environment tab we can see a detailed breakdown of the machine that triggered the error including:

如果导航到“ 环境”选项卡,我们可以看到触发错误的计算机的详细分类,包括:

  • Operating system

    操作系统
  • RAM

    内存
  • Processor architecture

    处理器架构
  • Processor core count

    处理器核心数

This environment information can be very useful for Android, iOS, and Unity3D based applications and also has it's uses for web applications as well.

此环境信息对于基于Android,iOS和Unity3D的应用程序非常有用,并且也可用于Web应用程序。

In the detailed bug view, you can see and leave comments for other members of your team to help the diagnosing and resolution of the error. Additionally, you can assign the Raygun state of the error and decide next steps to take in resolving and handling the error.

在详细的错误视图中,您可以看到团队其他成员并留下评论,以帮助诊断和解决错误。 此外,您可以分配错误的Raygun状态,并决定下一步以解决和处理该错误。

自动记录错误 (Automatically Logged Errors)

In our earlier example, we manually created and triggered an error by using the raygunClient.send() function. Raygun will also capture and handle any application generated errors as well.

在我们之前的示例中,我们使用raygunClient.send()函数手动创建并触发了错误。 Raygun还将捕获并处理任何应用程序生成的错误。

To showcase this functionality, we'll create a new route and call it '/app-error'. We will be triggering a JavaScript ReferenceError by trying to call an undefined variable. See the code sample below.

为了展示此功能,我们将创建一条新路由,并将其称为“ / app-error” 。 我们将通过尝试调用未定义的变量来触发JavaScript ReferenceError 。 请参见下面的代码示例。

// create the route to show a logged app error
app.get('/app-error', function(req, res){
    // sunday is an undeclared variable
    var message = "Hello world! Today is " + sunday; 

    // send this message back to the client
    res.send(message);
});

Running the application and navigating to the http://localhost:8080/app-error route will not produce the message "Hello World! Today is Sunday", but will instead give us ReferenceError telling us that sunday is not defined as well as the stacktrace for this error.

运行应用程序并导航到http:// localhost:8080 / app-error路线不会产生消息“ Hello World!今天是星期日”,但是会给我们ReferenceError告诉我们, sunday未定义,此错误的stacktrace。

If we dig deeper into this error on the Raygun dashboard we will be able to see the HTTP request information for this error including the HTTP method, hostname, header values and more.

如果我们在Raygun仪表板上更深入地研究此错误,我们将能够看到此错误的HTTP请求信息,包括HTTP方法,主机名,标头值等。

Raygun will automatically capture these application level errors and send you appropriate alerts which makes it very plug and play for our applications.

Raygun将自动捕获这些应用程序级别的错误,并向您发送适当的警报,这使其非常适合我们的应用程序即插即用。

用户追踪 (User Tracking)

This is one of the really neat features of Raygun. Raygun has the ability to track users that experience errors with your application. Additionally, Raygun gives you the ability to contact affected users via email or social media and let them know when an issue has been fixed or provide other types of updates.

这是Raygun的真正简洁功能之一。 Raygun能够跟踪遇到应用程序错误的用户。 此外,Raygun使您能够通过电子邮件或社交媒体与受影响的用户联系,并在问题已解决或提供其他类型的更新时告知他们。

This really helps automate our bug fix flow since we'll know the direct user that was affected and be able to follow up with them on their specific issue. No more asking for screenshots and logs when a user chimes in with a problem.

这确实有助于自动化我们的错误修复流程,因为我们将了解受影响的直接用户,并能够针对他们的特定问题进行跟进。 当用户遇到问题时,不再需要屏幕截图和日志了。

In our code example, we will be creating a static user to showcase this functionality, but in a real application you would get the actual logged in user information.

在我们的代码示例中,我们将创建一个静态用户来展示此功能,但是在实际应用程序中,您将获得实际登录的用户信息。

We will create a route /user-error that will generate a generic JavaScript Error as we did in our initial example, but here we will include the user's information using the raygunClient.user method. Take a look at the code sample below to see how we accomplish this.

我们将创建一个路由/user-error ,该路由将生成一个通用JavaScript错误,就像在最初的示例中一样,但是在这里,我们将使用raygunClient.user方法包含用户信息。 查看下面的代码示例,看看我们如何完成此任务。

// attach a user to a raygun error
app.get('/user-error', function(req, res){

    // create a raygun user
    raygunClient.user = function(req) {
        var user = {
            identifier: "<a href='mailto:ado@scotch.io'>ado@scotch.io</a>",
            fullName : "Ado Kukic",
            isAnonymous : false
        } 
        return user;
    }

    // create the error
    var err = new Error("User Tracking Error");

    // send the error
    raygunClient.send(err);

    // send info back to the browser
    res.send("User Tracking Error");

});

Running this code and navigating to the http://localhost:8080 will display the "User Tracking Error" message in your browser.

运行此代码并导航到http:// localhost:8080,将在浏览器中显示“用户跟踪错误”消息。

If you navigate to your Raygun dashboard though, and click into the Users tab, you will be able to see that our user "Ado Kukic" has experienced some issues using our app. We can further click into Ado Kukic and see what errors he has experienced and when the last occurrence of the errors were.

不过,如果您导航到Raygun仪表板,然后单击“用户”选项卡,您将能够看到我们的用户“ Ado Kukic”在使用我们的应用程序时遇到了一些问题。 我们可以进一步单击Ado Kukic,查看他遇到了哪些错误以及上一次发生错误的时间。

Knowing which users were affected by errors in your application is invaluable for both diagnosis and public relations.

知道哪些用户受您的应用程序中的错误影响对于诊断和公共关系都是无价的。

The raygunClient.user method allows you to specify which information about the user you would like to capture. The only required field is an identifier, so if you would prefer not to send potentially sensitive user information to Raygun but still would like to have some type of attribution of errors to user accounts, you can devise your own unique identifier and use that instead.

raygunClient.user方法允许您指定要捕获的有关用户的信息。 唯一需要的字段是标识符 ,因此,如果您不想将可能敏感的用户信息发送给Raygun,但仍然希望将某种类型的错误归因于用户帐户,则可以设计自己的唯一标识符,而改用它。

结论 (Conclusion)

Raygun simplifies the process of tracking and diagnosing errors in your application. Today we integrated a simple Node.js application into Raygun. We showcased how Raygun can be an invaluable tool in diagnosing and tracking issues and errors with your application.

Raygun简化了跟踪和诊断应用程序中的错误的过程。 今天,我们将一个简单的Node.js应用程序集成到Raygun中。 我们展示了Raygun如何成为诊断和跟踪应用程序中的问题和错误的宝贵工具。

Raygun tracks both automatic and developer initiated errors. Additionally, Raygun's user tracking system allows you to attribute errors to users for better diagnosis. Below is the full application we built that showcases different types of error that Raygun can handle.

Raygun跟踪自动错误和开发人员引发的错误。 此外,Raygun的用户跟踪系统使您可以将错误归因于用户,以便更好地进行诊断。 下面是我们构建的完整应用程序,展示了Raygun可以处理的不同类型的错误。

// grab the modules we need
var express = require('express'),
    raygun = require('raygun');

// initiate a raygun client
var raygunClient = new raygun.Client().init({ apiKey: 'API-KEY-HERE' });

// get an instance of express for our application
var app = express();

// IMPORTANT: per Express and Raygun documentation, for Raygun to properly capture application based errors it's middleware will need to be called before the app.listen function
app.use(raygunClient.expressHandler);

// create the simple-error route
app.get('/simple-error', function(req, res){
    // create a new error object
    var err = new Error('Simple Error');

    // send that error off to raygun
    raygunClient.send(err);

    // send a message back to the browser
    res.send('Simple Error Thrown');
});

app.get('/app-error', function(req, res){
    // sunday is an undeclared variable
    var message = "Hello world! Today is " + sunday; 

    // send this message back to the client
    res.send(message);
});

// attach a user to a raygun error
app.get('/user-error', function(req, res){

    // create a raygun user
    raygunClient.user = function(req) {
        var user = {
            identifier: "<a href='mailto:ado@scotch.io'>ado@scotch.io</a>",
            fullName : "Ado Kukic",
            isAnonymous : false
        } 
        return user;
    }

    // create the error
    var err = new Error("User Tracking Error");

    // send the error
    raygunClient.send(err);

    // send info back to the browser
    res.send("User Tracking Error");

});

// start our app on port 8080
app.listen('8080');

Finding and fixing issues before users experience them is vital to a building an applications that users will come back to. Raygun will save you the headache of digging through hard to read server logs after an issue is reported by an unhappy user.

在用户遇到问题之前查找并修复问题对于构建用户将要使用的应用程序至关重要。 当不满意的用户报告问题后,Raygun将使您免于浏览难以读取的服务器日志的麻烦。

This post sponsored by via Syndicate Ads.

该帖子由Syndicate Ads 赞助

翻译自: https://scotch.io/tutorials/automatically-find-and-log-bugs-in-your-node-js-app-with-raygun

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值