【问题标题】:How to only enable error logging if in production environment?如果在生产环境中,如何仅启用错误日志记录?
【发布时间】:2018-12-05 12:08:37
【问题描述】:

我有一个使用 raven.js 的 sentry.io 进行错误记录的 Angular 5 应用程序。

我让这一切正常工作,但不想在开发中运行时记录错误。如何仅在启用生产模式时启用错误日志记录?

我的 app.module.ts 中有以下内容

import * as Raven from 'raven-js';

Raven
  .config('https://xxx@sentry.io/xxx')
  .install();

export class RavenErrorHandler implements ErrorHandler {
  handleError(err: any): void {
    Raven.captureException(err);
  }
}

【问题讨论】:

  • 如果你使用 angular-cli,他们有 environment 文件,你可以简单地做 if (environment.production)
  • @penleychan 效果很好,谢谢朋友!想要将此作为答案发布,以便我将其标记为正确的答案?

标签: angular sentry


【解决方案1】:

我认为值得分享,因为我有一个类似的问题我想...

  • 仅在生产中使用我的 Sentry.io errorHandler
  • 在开发中使用 Angular 默认的 ErrorHandler
  • 将依赖项注入处理程序

app.error-handler.ts

第一步是实现一个工厂提供程序函数,以根据environment.production 属性返回SentryErrorHanddler 或Angular 默认ErrorHandler,并接收所需的依赖项作为参数。

import { ErrorHandler } from '@angular/core';
import * as Sentry from '@sentry/browser';
import { environment } from '../environments/environment';

export function errorHandlerFactory(toastService: ToastService) {
  if (environment.production) {
    return new SentryErrorHandler(toastService);
  }
  return new ErrorHandler();
}

第二步是实现SentryErrorHandler 类,它将接收到构造函数的依赖项(请注意,我没有在类前面加上@Injectable 装饰器,因为它将由上面的工厂实例化,而不是由DI).

export class SentryErrorHandler implements ErrorHandler {
  constructor(public toastService: ToastService) {
    Sentry.init({
      dsn: 'https://<YOUR-SENTRY-KEY>@sentry.io/<YOUR-PROJECT-ID>',
    });
  }

  handleError(error: any) {
    Sentry.captureException(error.originalError || error);
    this.toastService.show('An error occurs, please try again later.');
    throw error;
  }
}

app.module.ts

最后,在AppModule 中提供ErrorHandler 与实现的工厂提供程序errorHandlerFactory 并通过deps 属性指定它需要一些注入依赖项,该属性将作为参数传递给工厂提供程序函数

import { ErrorHandler } from '@angular/core';
import { errorHandlerFactory } from './app.error-handler';

@NgModule({
  //...
  providers: [
    { provide: ErrorHandler, useFactory: errorHandlerFactory, deps: [ToastService] },
  ],
})
export class AppModule { }

请注意,与 ToastService 相关的所有内容仅作为示例,表示您希望访问自定义处理程序的外部依赖项。

【讨论】:

  • 感谢您的回复!我使用的是旧的 Sentry.io SDK,并且最近才升级到您的答案中描述的方法。这个问题主要是围绕 Angular 环境而不是 sentry.io 但感谢您的回复!
  • 我的用例是使用 Sentry.io,但它可以应用于与提供自定义 ErrorHandler 或回退到 Angular ErrorHandler 相关的任何事情,具体取决于环境配置......如果其他人最终我把它放在那里在这里,不希望成为公认的答案:)
  • 非常好的答案!我试图仅在生产中加载 Airbrake,但不知道您可以在 AppModule 中使用工厂。谢谢
【解决方案2】:

如果你使用 angular-cli,他们有环境文件,你可以简单地做

if (environment.production) { ... }

【讨论】:

    【解决方案3】:

    你应该使用函数isDevMode

    import { isDevMode } from '@angular/core';
    
    export class AppComponent { 
      constructor() {
        console.log(isDevMode());
      }
    }
    

    如果它没有返回 true,它应该是 prod

    【讨论】:

    • 与 penleychan 的解决方案相比,这有什么好处吗?
    • 嗯,这是来自角度核心,而环境来自角度 cli。推荐第一个而不是后者
    • 虽然可能是这种情况,但另一个答案更适合我的用例,因为我使用的是 angular-cli,但感谢您的帮助!
    猜你喜欢
    • 2015-10-30
    • 2012-03-05
    • 2012-05-03
    • 2019-07-21
    • 1970-01-01
    • 2021-09-27
    • 2013-04-22
    • 2012-07-25
    • 2020-09-13
    相关资源
    最近更新 更多